Actuator用于监控和度量SpringBoot应用,了解应用程序运行时的内部状况。在以前,这个功能通常是我们开发人员自行实现的,但是如果使用SpringBoot进行开发,引入Actuator的起步依赖即可实现,非常方便。
引入起步依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Actuator提供了若干端点,供我们调用以获取我们需要的数据。默认情况下,Actuator提供了web和JMX两种方式访问这些数据,数据以JSON形式呈现。
Actuator提供的端点很多,而且随着版本更新经常变动(SpringBoot2.x和1.x的Actuator就有很多不同),这里建议使用时查询对应版本的文档:https://docs.spring.io/spring-boot/docs/2.7.0/reference/htmlsingle/
我们引入Actuator依赖后,可以使用浏览器访问/actuator
路径,查看web能够访问的端点。
默认情况下,仅能通过web访问health
和info
两个端点。
默认情况下,SpringBoot启动了除shutdown以外的所有端点。相关配置可以在application.properties
中指定,下面是2个例子。
启动shutdown端点:
management.endpoint.shutdown.enabled=true
默认禁用所有端点:
management.endpoints.enabled-by-default=false
开启端点后,我们需要把它暴露出去,才能正常访问。默认情况下,所有端点(除了不适用于JMX的)通过JMX暴露,只有health
和info
通过web暴露。我们通过include
和exclude
控制端点是否暴露,include
表示暴露端点,exclude
表示不暴露端点。
通过JMX暴露health
端点:
management.endpoints.jmx.exposure.include=health
通过web暴露除了env
和beans
的所有端点:
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans