這篇文章給大家介紹使用prometheus怎么對springboot應用進行監控,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
對于springboot應用,需要以下幾個步驟
springboot應用開啟endpoint,添加actuator的以來和promethus的依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency>
在yml文件或者properties文件中,加入以下配置:
management: endpoints: jmx: exposure: include: "*" web: exposure: include: "*" metrics: export: datadog: application-key: ${spring.application.name}
這里需要注意是,*號是需要加雙引號的。
以上兩個步驟完畢后,剩下的就是要加registry了:
@Bean MeterRegistryCustomizer<MeterRegistry> configurer( @Value("${spring.application.name}") String applicationName) { return (registry) -> registry. config(). commonTags("application", applicationName); }
對于springboot的應用,到此基本完成了。接下來是啟動promethus。
配置prometheus
首先要下在prometheus":Prometheus - Monitoring system & time series database"
在下載頁面,選擇何時的版本下載,推薦下載tar.gz包。下載好后,進行解壓。在合適的路徑下即可。
這里介紹下prometheus的目錄和文件:
1、prometheus采用的都是yml文件的配置方式。
2、在根目錄下,有個prometheus.yml配置文件,文件初始化的內容如下:
global: scrape_interval: 15s # 這個是每次數據手機的頻率 evaluation_interval: 15s # 評估告警規則的頻率。 rule_files: # - "first.rules" # - "second.rules" scrape_configs: # 通過這里的配置控制prometheus監控的資源 - job_name: prometheus # prometheus自身默認的 static_configs: - targets: ['localhost:9090'] # 默認暴露的是9090端口服務
global是全局配置。具體見上面的注釋說明。
3、添加我們的應用,對springboot進行監控
- job_name: 'spring-sample' metrics_path: 'actuator/prometheus' # 這里我們springboot暴露出來的endpoint scrape_interval: 5s # 信息收集時間是間隔5秒 static_configs: - targets: ['localhost:8778'] # 這里是springboot暴露出來的地址和端口
4、這些配置完成后,可以啟動prometheus,./prometheus --config.file=prometheus.yml,服務即可啟動。具體訪問產檢官網。
配置grafana
下載grafana,直接啟動即可。
1、啟動命令參見官網:./grafana-server web
2、配置datasource,選擇prometheus。這個里面有個很重要的注意點,我看網上很多人在轉如何用prometheus監控springboot應用,估計自己沒去實際搭建,在interval這個時間上,默認是數字,比如15,代表是15秒。在添加dashboard的時候,會發現監控圖標左上角是個紅點,報錯:Invalid interval string, expecting a number followed by one of "Mwdhmsy" ,這個錯的解決方案就是在這些時間間隔后面加個"s"。問題解決。
3、選擇dashboard,import的里輸入一個模板,可以去dashboards去找你對應的模板,我們這里選用jvm的4701模板,然后就能看到你的springboot的監控信息了。到此,整個搭建完成。
關于使用prometheus怎么對springboot應用進行監控就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。