一、Actuator 介紹
Actuator 是 SpringBoot 項目中一個非常強大一個功能,有助於對應用程序進行監視和管理,通過 restful api 請求來監管、審計、收集應用的運行情況。
Actuator 的核心是端點 Endpoint,它用來監視應用程序及交互,spring-boot-actuator 中已經內置了非常多的 Endpoint(health、info、beans、metrics、httptrace、shutdown等等),同時也允許我們自己擴展自己的 Endpoints。每個 Endpoint 都可以啟用和禁用。要遠程訪問 Endpoint,還必須通過 JMX 或 HTTP 進行暴露,大部分應用選擇HTTP,Endpoint 的ID默認映射到一個帶 /actuator 前綴的URL。例如,health 端點默認映射到 /actuator/health。
二、Actuator 使用
啟用 Actuator 最簡單方式是添加 spring-boot-starter-actuator ‘Starter’依賴。
1、pom.xml
<!-- 2、監控 —— Actuator插件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2、application.yml
management: endpoints: # 暴露 EndPoint 以供訪問,有jmx和web兩種方式,exclude 的優先級高於 include jmx: exposure: exclude: '*' include: '*' web: exposure: # exclude: '*' include: ["health","info","beans","mappings","logfile","metrics","shutdown","env"] base-path: /actuator # 配置 Endpoint 的基礎路徑 cors: # 配置跨域資源共享 allowed-origins: http://example.com allowed-methods: GET,POST enabled-by-default: true # 修改全局 endpoint 默認設置 endpoint: auditevents: # 1、顯示當前引用程序的審計事件信息,默認開啟 enabled: true cache: time-to-live: 10s # 配置端點緩存響應的時間 beans: # 2、顯示一個應用中所有 Spring Beans 的完整列表,默認開啟 enabled: true conditions: # 3、顯示配置類和自動配置類的狀態及它們被應用和未被應用的原因,默認開啟 enabled: true configprops: # 4、顯示一個所有@ConfigurationProperties的集合列表,默認開啟 enabled: true env: # 5、顯示來自Spring的 ConfigurableEnvironment的屬性,默認開啟 enabled: true flyway: # 6、顯示數據庫遷移路徑,如果有的話,默認開啟 enabled: true health: # 7、顯示健康信息,默認開啟 enabled: true show-details: always info: # 8、顯示任意的應用信息,默認開啟 enabled: true liquibase: # 9、展示任何Liquibase數據庫遷移路徑,如果有的話,默認開啟 enabled: true metrics: # 10、展示當前應用的metrics信息,默認開啟 enabled: true mappings: # 11、顯示一個所有@RequestMapping路徑的集合列表,默認開啟 enabled: true scheduledtasks: # 12、顯示應用程序中的計划任務,默認開啟 enabled: true sessions: # 13、允許從Spring會話支持的會話存儲中檢索和刪除(retrieval and deletion)用戶會話。使用Spring Session對反應性Web應用程序的支持時不可用。默認開啟。 enabled: true shutdown: # 14、允許應用以優雅的方式關閉,默認關閉 enabled: true threaddump: # 15、執行一個線程dump enabled: true # web 應用時可以使用以下端點 heapdump: # 16、 返回一個GZip壓縮的hprof堆dump文件,默認開啟 enabled: true jolokia: # 17、通過HTTP暴露JMX beans(當Jolokia在類路徑上時,WebFlux不可用),默認開啟 enabled: true logfile: # 18、返回日志文件內容(如果設置了logging.file或logging.path屬性的話),支持使用HTTP Range頭接收日志文件內容的部分信息,默認開啟 enabled: true prometheus: #19、以可以被Prometheus服務器抓取的格式顯示metrics信息,默認開啟 enabled: true
這大抵就是全部默認的 Endpoint 的配置了,怎么樣?強大吧!之前做了一個網絡監控的項目,就是能夠實時查看服務器的 CPU、內存、磁盤、IO 這些(基於 sigar.jar 實現),然后現在發現 SpringBoot 就這樣輕松支持了,還更強大,更簡便......
默認的 Endpoint 映射前綴是 /actuator,可以通過如上 base-path 自定義設置。
每個 Endpoint 都可以配置開啟或者禁用。但是僅僅開啟 Endpoint 是不夠的,還需要通過 jmx 或者 web 暴露他們,通過 exclude 和 include 屬性配置。
3、效果
做好了如上的配置,接下來我們只需要訪問對應的 Endpoint 就可以啦,/actuator/[Endpoint ID](http://127.0.0.1:8080/actuator/health)。
三、自定義 Endpoint
自定義 Endpoint 端點,只需要在我們的新建Bean上使用 @Endpoint 注解即可。則 Bean 中的方法就可以通過 JMX 或者 HTTP 公開。除此之外,你還可以使用 @JmxEndpoint 或 @WebEndpoint 編寫 EndPoint。但是這些 EndPoint 僅限於各自的公開方式。例如,@WebEndpoint 僅通過HTTP公開,而不通過JMX公開。
那么是不是類中所有的方法都支持對外公開呢?很明顯不是的。這里又要提到三個注解,只有加三個注解的方法才支持對外公開,並且每個注解都有支持它的 HTTP method。如下:
Operation | HTTP method |
---|---|
@ReadOperation | GET |
@WriteOperation | POST |
@DeleteOperation | DELETE |
Endpoint 上的操作通過參數接收輸入。 當通過網絡公開時,這些參數的值取自URL的查詢參數和JSON請求主體。 通過JMX公開時,參數將映射到MBean操作的參數。參數默認是必需的,可以通過使用 @Nullable 注釋使其成為可選的。
可以通過使用 @Selector 注釋操作方法的一個或多個參數來進一步定制路徑。@Selector 會將路徑上的參數作為變量傳遞給操作方法。這個注解有點詭異,且聽我徐徐道來~~
1、Endpoint Bean
@Component @Endpoint(id = "my", enableByDefault = true) //設置 id,並選擇是否默認開啟 public class MyEndPoint { @ReadOperation public List<String> getPaths() { List<String> list = new ArrayList<>(); list.add("java"); list.add("c++"); list.add("python"); return list; } @ReadOperation public String get(@Selector String arg0) { return arg0; }
@WriteOperation public String post() { return "post"; } @DeleteOperation public Integer delete() { return 1; } }
2、暴露 Endpoint
設置好了上面的 Endpoint Bean,還不能真正的訪問到它們,需要在 application.yml 中將它們暴露出來:
management: endpoints: # 暴露 EndPoint 以供訪問,有jmx和web兩種方式,exclude 的優先級高於 include jmx: exposure: exclude: '*' include: '*' web: exposure: # exclude: '*' include: ["health","info","beans","mappings","logfile","metrics","shutdown","env","my"]
做好這些配置后,你就能訪問到 Endpoint 了(http://127.0.0.1:8080/actuator/my)
3、@Selector
注意到沒有,上面的 Endpoint 有一個 @Selector 參數的方法,並且參數名是 arg0,這個參數名是有學問滴......
原來我給的參數名是 path,原來我設想我可以訪問 /actuator/my/[任意字符] 的路徑,但是會報 400 參數不匹配錯誤。但是嘞,/actuator/my/[任意字符]?path=[任意字符] 是正常訪問的,真是奇了怪了!。
原來,為了使 @Selector 正常工作,必須使用嵌入的參數名稱編譯 Endpoint(-parameters),如下。或者將參數名改為 arg0 就能達到目的。這個是 stackoverflow 上的一個解釋~
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <compilerArgs> <arg>-parameters</arg> </compilerArgs> </configuration> </plugin> <!-- 或者: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <parameters>true</parameters> </configuration> </plugin> --> </plugins>
tips: -parameters 的方式我沒有驗證通過呀~~汗