Spring Boot Actuator是一個用于監控和管理Spring Boot應用的子項目,它提供了一組REST端點和命令行工具, 用于查看應用的運行狀態、性能指標和健康狀況等。Actuator還支持應用度量數據的導出,以及自定義端點和安全控制等功能。通過使用Spring Boot Actuator,開發人員可以更加方便地了解應用的運行狀況,及時發現和解決問題。
隨著微服務架構的普及,Spring Boot 已經成為Java開發人員的首選框架。然而,隨著應用的規模不斷擴大, 如何有效地監控和管理這些應用成為一個重要的問題。Spring Boot Actuator的出現,為開發人員提供了一個解決方案。本文將詳細介紹Spring Boot Actuator的功能、工作原理、使用場景以及應用示例,幫助讀者更好地理解和掌握這一工具。
服務端點 | 描述 |
auditevents | 公開當前應用程序的審核事件信息。 |
beans | 顯示應用程序中所有Spring bean的完整列表。 |
caches | 公開可用的緩存 |
conditions | 顯示在配置和自動配置類上評估的條件以及它們匹配或不匹配的原因。 |
configprops | 顯示所有@ConfigurationProperties的有序列表。 |
env | 公開Spring的ConfigurableEnvironment中的屬性 |
flyway | 顯示已應用的任何Flyway數據庫遷移。 |
health | 顯示應用健康信息。 |
httptrace | 顯示HTTP跟蹤信息(默認情況下,最后100個HTTP請求 – 響應交換)。 |
info | 顯示任意應用信息。 |
integrationgraph | 顯示Spring集成圖。 |
loggers | 顯示和修改應用程序中日志記錄器的配置。 |
liquibase | 顯示已應用的任何Liquibase數據庫遷移。 |
metrics | 顯示當前應用程序的“指標”信息。 |
mappings | 顯示所有@RequestMapping路徑的有序列表。 |
scheduledtasks | 顯示應用程序中的計劃任務。 |
sessions | 允許從Spring Session支持的會話存儲中檢索和刪除用戶會話。使用Spring Session對響應式Web應用程序的支持時不可用。 |
shutdown | 允許應用程序正常關閉。 |
http://localhost:8080/actuator
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
management: endpoints: web: exposure:# [health, info] include: "*"
監控端點相關注解:
自定義一個端點服務:
@Endpoint(id = "custom")public class CustomEndpoint { /** * /actuator/custom */ @ReadOperation public Map custom() { return new HashMap(); } /** * /actuator/custom/{name}?value={value} */ @ReadOperation public Map name(@Selector String name, @Nullable String value) { return new HashMap(); }}
Spring-Actuator主要實現數據的采集,以及提供REST API以及JMX的訪問渠道,那么數據具體如何友好地顯示出來?這時我們需要對應的UI,其中spring-boot-admin就是這樣一款工具。
http://localhost:8080/applications
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> </dependency>
@EnableAdminServerpublic class Application{ }
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.6.2</version> </dependency>
客戶端配置
spring: boot: admin: client: url: http://localhost:8080
上面說到,Actuator除了采集指標,提供訪問API外,還提供了“應用度量數據的導出”的功能,這樣就能將我們采集到的指標輸出到指定的存儲服務或終端以便進一步分析。其中Prometheus就是這樣一個應用。
<dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId></dependency>
management: endpoints: web: exposure: include: "*" metrics: export: prometheus: enabled: true prometheus: enabled: true
scrape_configs: - job_name: 'spring-boot-actuator' metrics_path: '/actuator/prometheus' static_configs: - targets: ['localhost:8080'] # 使用你的Spring Boot應用程序的實際主機和端口替換
prometheus.exe --config.file=prometheus.ymlgrafana-server.exe
由于篇幅有限,關于Grafana如何集成Prometheus,網上有很多具體實踐,這里不重復贅述...
由于項目使用spring-boot版本為2.3.7.RELEASE,而spring-boot-admin-starter-server版本設置設置為2.7.x版本時,UI相關配置一直無法加載,通過源碼可以看到
在2.6.x版本中對應spring-boot-admin-server-ui存在META-IN/spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=/ de.codecentric.boot.admin.server.ui.config.AdminServerUiAutoConfiguration
而在2.7.x版本中,spring.factories刪除了且改為了 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
de.codecentric.boot.admin.server.ui.config.AdminServerUiAutoConfiguration
因此如果需要使用2.7.x版本的spring-boot-admin,記得把spring-boot升級到2.7.x
參數名稱被解析為arg0,導致請求匹配失敗。通過下面的配置保證編譯后的文件通過反射獲取的參數名稱不變
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <debug>false</debug> <!-- 防止方法參數名解析為arg0... --> <compilerArgs> <arg>-parameters</arg> </compilerArgs> </configuration> </plugin>
如果使用Idea,你可以在應用啟動后,Actuator功能面板的Mappings中看到服務地址的變化
服務監控是為了更好的了解服務運行狀況,及時發現服務可能出現的問題,并在出現故障時能夠有效的定位問題產生的原因。更大層面解決系統運行過程中的維護 成本。關于監控相關的應用還有一些,比如SkyWalking、Zipkin、Elastic APM等等。
本文鏈接:http://www.www897cc.com/showinfo-26-17662-0.html一文帶你了解Spring Actuator
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: 不吃飯也要掌握的Synchronized鎖升級過程
下一篇: 解析幾何:計算兩條線段的交點