以上是官網對Actuator解釋,大致意思就是:
當使用Actuator的時候SpringBoot包含一些額外的特性幫助你監控和管理你的應用。你可以選擇HTTP端點或者JMX來管理監控你的應用。Auditing, health, and metrics 能夠自動的應用的你的程序。
官方喊話了:Actuator是對你的應用程序進行管理和監控的,使用也非常方便,因爲它可以自動的應用到你的程序。
Actuator使用
Actuator使用很簡單,只需要在pom.xml文件中加入actuator依賴就可以了:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
我們在管理監控我們的應用的時候需要使用Actuator提供給我們的端點來進行訪問:http://{IP}:{端口}/actuator/{端點}
以上只是一個health端點的示例,在第一次使用的時候可能會發現以下結果:
這樣的原因是因爲2.x中heanlth默認細節是不會顯示的。我們需要在配置文件yml中配置:
management:
endpoint:
health:
show-details: always
上面只是使用一個示例來介紹怎麼使用Actuator。下面羅列出Actuator所有的端點:
這些端點中SpringBoot只爲我們暴露了health和info端點,如果要使用其他的端點需要在配置文件中配置:
management:
endpoints:
web:
exposure:
include: "*"
我這樣的配置是把所有的端點都暴露出來了。具體的可以查看官方文檔https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#production-ready
需要注意一點的是shutdown這個端點,這個端點是用來讓程序優雅停機。這個端點默認是不可用也不暴露的,我們需要通過配置文件將它開啓並且暴露:
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
shutdown:
enabled: true
使用post請求實現優雅停機
SpringBoot1.x和SpringBoot2.x還是有區別的,具體的需要查看官方文檔,這裏就不做陳述了。