一、Spring Boot 應(yīng)用暴露監(jiān)控指標(biāo)【版本 1.5.7.RELEASE】
首先,添加以下依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>0.0.26</version>
</dependency>
然后车柠,在啟動類 Application.java 添加如下注解:
@SpringBootApplication
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
啟動應(yīng)用程序后,會看到如下一系列的 Mappings:
上面這張圖需要 idea2017.2版本以上才能看到哦~~
這時很開心以為這樣就成功了,接下來看遇到的第一個坑疤孕。。搔课。
坑一胰柑、訪問監(jiān)控地址報錯
打開瀏覽器訪問 http://localhost:8080/metrics ,不好意思報錯了哦爬泥,請看報錯原因:
相信使用過 springboot 的人看到這個頁面一定很熟悉,竟然沒有權(quán)限崩瓤,那咋辦呢袍啡,當(dāng)然是打開官方文檔瞄一眼咯~~~
敏感的訪問節(jié)點需要用戶名密碼才能訪問。
坑一却桶、解決方法
(1)放棄安全性境输,臨時暴露出來看一下的方法蔗牡。(不建議使用)
在application.properties配置文件增加如下選項
management.security.enabled=false
endpoints.health.sensitive=false
(2)設(shè)權(quán)限,安全查看:
第一步嗅剖,添加依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
第二步辩越,配置用戶名密碼
security.user.name=admin
security.user.password=123456
management.security.roles=SUPERUSER
這樣在瀏覽器訪問時會要求先輸入密碼才能訪問。
這里給出一個完整的 application.properties配置:
# 啟用基礎(chǔ)認(rèn)證
security.basic.enabled = true
# 安全路徑列表信粮,逗號分隔黔攒,此處只針對/admin路徑進(jìn)行認(rèn)證
security.basic.path = /admin
# 認(rèn)證使用的用戶名
security.user.name = admin
# 認(rèn)證使用的密碼。 默認(rèn)情況下强缘,啟動時會記錄隨機(jī)密碼督惰。
security.user.password = 123456
# 可以訪問管理端點的用戶角色列表,逗號分隔
management.security.roles = SUPERUSER
# actuator暴露接口使用的端口旅掂,為了和api接口使用的端口進(jìn)行分離
management.port = 8099
# actuator暴露接口的前綴
management.context-path = /admin
# actuator是否需要安全保證
management.security.enabled = true
# actuator的metrics接口是否需要安全保證
endpoints.metrics.sensitive = false
# actuator的metrics接口是否開啟
endpoints.metrics.enabled=true
# actuator的health接口是否需要安全保證
endpoints.health.sensitive=false
# actuator的health接口是否開啟
endpoints.health.enabled=true
打開瀏覽器訪問http://localhost:8099/admin/prometheus 赏胚,輸入用戶名密碼;可以看到以下信息商虐,是不是很完美觉阅。
二、Prometheus 采集 Spring Boot 指標(biāo)數(shù)據(jù)
首先秘车,下載 Prometheus 的安裝軟件典勇,下載可能比較慢,有需要的可以給我留言鲫尊,我可以提供prometheus-1.8.0.linux-amd64.tar.gz版本痴柔,官網(wǎng)下載地址:https://github.com/prometheus/prometheus/releases
其次,編寫配置文件 prometheus.yml :
global:
scrape_interval: 10s
scrape_timeout: 10s
evaluation_interval: 10m
scrape_configs:
- job_name: spring-boot
scrape_interval: 5s
scrape_timeout: 5s
metrics_path: /admin/prometheus
scheme: http
basic_auth:
username: admin
password: 123456
static_configs:
- targets:
- 192.168.8.244:8099 #此處填寫 Spring Boot 應(yīng)用的 IP + 端口號
有關(guān)配置選項的完整規(guī)范疫向,請查看配置文檔咳蔚。
然后,啟動 Prometheus :
./prometheus -config.file=prometheus.yml &
最后搔驼,訪問 http://localhost:9090/targets , 檢查 Spring Boot 采集狀態(tài)是否正常谈火。
三、Grafana 可視化監(jiān)控數(shù)據(jù)
首先舌涨,獲取 Grafana 的安裝包糯耍,有需要的可以給我留言,我可以提供grafana-4.5.2-1.x86_64.rpm囊嘉,官方下載地址:https://grafana.com/grafana/download
其次温技,啟動 Grafana:
sudo /bin/systemctl start grafana-server.service
然后,訪問 http://localhost:3000/ 查看Grafana是否安裝成功扭粱。開始登陸時需要用戶名密碼舵鳞,Grafana 登錄賬號 admin 密碼 admin
能夠打開上面的頁面表示安裝成功。
最后琢蛤,就是配置Grafana蜓堕。
1抛虏、Grafana配置
1、登陸成功后顯示的頁面如下:
2套才、配置數(shù)據(jù)源迂猴,點擊Add data source,進(jìn)入以下頁面:
配置結(jié)果如下:
3背伴、配置單個指標(biāo)的可視化監(jiān)控面板沸毁,點擊Create your first dashboard,進(jìn)入如下頁面:
注意:此處不能任意填寫挂据,只能填已有的指標(biāo)點以清,具體的可以在 Prometheus 的首頁看到,即 http://localhost:9090/graph
多配置幾個指標(biāo)之后崎逃,即可有如下效果: