prometheus監控springboot應用簡單使用介紹
採用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的監控資訊了。到此,整個搭建完成。