Actuator是什么?
Spring Boot Actuator 模塊提供了生產級別的功能,比如健康檢查,審計,指標收集,HTTP 跟蹤等,幫助我們監控和管理Spring Boot 應用。這個模塊是一個采集應用內部信息暴露給外部的模塊,上述的功能都可以通過HTTP 和 JMX 訪問。
因為暴露內部信息的特性,Actuator 也可以和一些外部的應用監控系統整合(Prometheus, Graphite, DataDog, Influx, Wavefront, New Relic等)。這些監控系統提供了出色的儀表板,圖形,分析和警報,可幫助你通過一個統一友好的界面,監視和管理你的應用程序。
Actuator使用Micrometer與這些外部應用程序監視系統集成。這樣一來,只需很少的配置即可輕松集成外部的監控系統。
Actuator 使用
引入依賴
我們新建一個項目:jlw-actuator
-
引入 spring-boot-starter-actuator 的Maven依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
-
啟動項目,訪問:/actuator/health,理論上頁面會輸出以下成功的信息。
{ "status": "UP" }
Endpoints
Spring Boot 提供了所謂的 endpoints (端點)給外部來與應用程序進行訪問和交互,允許添加自己的端點。可以啟用或禁用每個端點,並通過HTTP或JMX公開(可遠程訪問)。默認暴露的兩個端點為/actuator/health和 /actuator/info
。
暴露端點
端點可能包含敏感信息,所以應該仔細考慮何時公開它們。
下面的是默認暴露配置:
management.endpoints.jmx.exposure.exclude = *
management.endpoints.jmx.exposure.include = *
management.endpoints.web.exposure.exclude = *
management.endpoints.web.exposure.include = info, health
暴露除/env
,/beans
之外的端點:
management:
endpoints:
web:
exposure:
include: "*"
exclude: "env,beans"
禁用默認其他節點
下面的意思是默認不啟用所有端點,僅啟用/info
端點
management:
endpoints:
enabled-by-default: false
endpoint:
info:
enabled: true
修改前綴/actuator
-
在配置文件添加以下配置,自定義
base-path
屬性management: endpoints: web: base-path: /jinglingwang #https://jinglingwang.cn/archives/actuator
-
訪問地址更改為:/jinglingwang/health
健康信息
/health
端點會聚合程序的健康指標來檢查程序的健康情況,默認配置值是never
,不會顯示詳細信息,還有when-authorized
和always
兩個選項值可配置。
management:
endpoint:
health:
show-details: always
配置成always后訪問/jinglingwang/health 會展示更多的信息,效果圖如下:
禁用組件自動配置
Spring Boot會自動配置key為couchbase、datasource、diskspace、elasticsearch、hazelcast、influxdb、jms、ldap、mail、mongo、neo4j、ping、rabbit、redis、solr
等運行狀況指示器,可以通過management.health.key.enabled來配置啟用/禁用指示器。
比如,禁用redis健康監測:
management:
health:
redis:
enabled: false
重啟之后,再訪問/jinglingwang/health端點,里面就不會再有redis相關的信息了。
特殊的 shutdown 端點
-
默認情況下,除shutdown之外的所有端點都處於啟用狀態。
management: endpoint: shutdown: enabled: true
注意是endpoint,不是endpoints
生產環境使用需慎重! -
然后Post訪問http://localhost:9000/jinglingwang/shutdown 就可以實現停機操作。
自定義健康指標
定制運行狀況信息,可以注冊實現 HealthIndicator 接口的springbean。需要提供health()方法的實現並返回一個健康響應。運行狀況響應應該包括狀態,並且可以選擇包括要顯示的其他詳細信息。
@Component
public class MyHealthIndicator implements HealthIndicator{
@Override
public Health health() {
//int errorCode = checkError(); //自定義邏輯 https://jinglingwang.cn/archives/actuator
int errorCode = 1;
if (errorCode != 1) {
return Health.down().withDetail("Error Code", "https://jinglingwang.cn/ not found").build();
}
return Health.up()
.withDetail("code","200")
.withDetail("message","https://jinglingwang.cn/")
.build();
}
}
端點安全校驗
默認暴露的端點是沒有任何安全校驗的,敏感信息很容易就暴露了,如果把生產環境的全部端點都暴露在外網環境下,是非常可怕的一件事,這時候我們就可以結合Spring Security來做安全校驗。
-
引入Security相關依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-security</artifactId> </dependency>
-
配置好用戶名密碼
spring: security: user: name: jinglingwang password: jinglingwang.cn roles: ENDPOINT_ADMIN
-
端點訪問配置
@Configuration(proxyBeanMethods = false) public class ActuatorSecurity extends WebSecurityConfigurerAdapter{ @Override protected void configure(HttpSecurity http) throws Exception { // 確保所有端點都具有ENDPOINT_ADMIN角色 http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests((requests) -> requests.anyRequest().hasRole("ENDPOINT_ADMIN")); http.httpBasic(); } }
整合監控頁面
基於 Prometheus 和 Dashboard(如Grafana)進行監控數據可視化展示。
項目引入Prometheus
Prometheus 是一個通過從監控目標上采集 HTTP 數據收集指標數據的監控平台,用戶可以非常方便的安裝和使用Prometheus並且能夠非常方便的對其進行擴展。
-
引入Prometheus依賴
由於我們使用springboot的是2.X,其中已經包含了io.micrometer相關的包,不需要再次引入,如果使用的springboot版本是1.X,需要自己額外引入。
<dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-core</artifactId> <version>${micrometer.version}</version> </dependency>
-
引入micrometer-registry-prometheus包,提供了基於actuator的端點,訪問路徑是
**/prometheus
<dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <scope>runtime</scope> </dependency>
-
開放所有端點,或者至少
/metrics
端點management.endpoint.metrics.enabled=true
-
然后訪問http://localhost:8181/jinglingwang/prometheus,即可看到相關數據
安裝Prometheus
-
下載Prometheus:https://prometheus.io/download/
我本地環境是Win10 -
直接解壓到自定義目錄
-
修改
prometheus.yml
需要配置scrape_configs
這個節點scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'jinglingwang' metrics_path: /actuator/prometheus # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:8181']
我這個項目的端口是8181,所以targets需要注意一下,以及獲取數據的訪問路徑是
/jinglingwang/prometheus
。 -
啟動Prometheus
Win10 直接運行prometheus.exe
就可以了,默認端口是9090. -
訪問控制台頁面: http://localhost:9090/classic/targets;
接入Grafana
Grafana是一個開源的指標量監測和可視化工具。常用於展示基礎設施的時序數據和應用程序運行分析。Grafana的dashboard展示非常炫酷,絕對是運維提升逼格的一大利器。
Grafana只是一個dashboard(4版本開始將引入報警功能),負責把數據庫中的數據進行可視化展示,本身並不存儲任何數據。Grafana目前支持的時序數據庫有: Graphite, Prometheus, Elasticsearch, InfluxDB, OpenTSDB, AWS Cloudwatch。
Grafana的套路基本上跟kibana差不多,都是根據查詢條件設置聚合規則,在合適的圖表上進行展示,多個圖表共同組建成一個dashboard,熟悉kibana的用戶應該可以非常容易上手。
官方在線演示Demo: http://play.grafana.org/
安裝
下載頁面:https://grafana.com/grafana/download
Grafana Window 安裝非常簡單,解壓后直接運行bin
目錄下的grafana-server.exe
就可以了,默認啟動端口是3030,初始登錄帳號密碼是admin/admin。
添加數據源
在左邊導航Configuration,點擊data source 添加一個新的數據源,這里我選擇的是Prometheus。
Name 填寫prometheus.yml
里面配置的job_name,如果填寫不一致會出現Grafana訪問沒有數據的問題。
URL填寫啟動Prometheus時的服務地址http://localhost:9090/,配置圖如下:
然后點擊下面的Save & Test
保存測試通過即可。
配置阿里的SLS JVM監控大盤
訪問https://grafana.com/grafana/dashboards/12856頁面可以查看到阿里的SLS JVM監控大盤
然后點擊Grafana 左邊的➕,再點擊import,輸入Id:12856,點擊Load即可,然后選擇Prometheus的數據源,最后點擊Import完成導致模板。
最后效果圖如下:
參考:
- https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#
- https://elkguide.elasticsearch.cn/elasticsearch/other/grafana.html
個人博客:精靈王