Actuator提供可以通过HTTP或者JMX监控和管理线上应用的附加功能,包括了审核、服务运行状况和指标收集,这些功能通过endpoints获取,每个endpoints都可以通过Http或者JMX 进行enabled(启用)和disabled(禁用)和exposed(公开)。
当依赖加入到项目中之后,endpoints可用的时候会自动注册到程序中,可以通过http或者jmx进行访问
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
启动项目访问/actuator/health,得到如下内容
{"status":"UP"}
其中/actuator 是访问endpoints的默认url前缀,/health 则展示应用的健康信息,
可以通过以下属性配置,修改endpoints的url前缀,修改之后health访问地址为 /simple/health
management.endpoints.web.base-path=/simple
由于endpoints中会包含敏感信息,所以暴露端点的时候应该谨慎考虑,下面列出常用的endpoints以及默认暴露情况
ID | 描述 | JMX | Web |
---|---|---|---|
health | 应用健康信息 | YES | YES |
info | 任意应用信息 | YES | NO |
beans | 显示应用中所有的beans信息 | YES | NO |
env | Spring中的ConfigurableEnvironment公开属性 | YES | NO |
metrics | 应用程序的指标 | YES | NO |
headdump | 获取hprof dump文件 | N/A | NO |
loggers | 展示和修改应用的日志配置 | YES | NO |
可以通过以下命令选择暴露和过滤不想暴露的endpoints
Property | Default |
---|---|
management.endpoints.jmx.exposure.exclude | |
management.endpoints.jmx.exposure.include | * |
management.endpoints.web.exposure.exclude | |
management.endpoints.web.exposure.include | health、info |
#通过management.endpoint.<id>.enabled属性设置,开启shutdown endpoint
#默认情况下除了shutdown以外的所有endpoint都是开启状态的
management:
endpoint:
shutdown:
enabled: true
management:
endpoints:
web:
exposure:
#暴露endpoints,通过http可以访问 /actuator/health、/actuator/beans、/actuator/info
include: health,info,beans
#禁止info端点暴露, 通过http访问不了
exclude: info
如果include和exclude同时使用的时候,exclude的优先级高,所以上面来说info通过http是访问不了的
创建Bean,在类上添加@Endpoint、@WebEndpoint、@EndpointWebExtension其中一个注解,在方法上添加@ReadOperation或者@WriteOperation或者@DeleteOperation。如果是web应用,endpoint可以使用Jersey、SpringMVC、Spring WebFlux通过HTTP暴露,如果Jersey和SpingMVC都可用,则使用SpingMVC。
也可以使用@ServletEndpoint 和@ControllerEndpoint,但是建议使用@Endpoint 和@WebEndpoint
这里以@Endpoint举例,其他的可以参考提供的完整demo代码
@Endpoint id属性必须进行设置
@Configuration
//id必须进行设置
@Endpoint(id = "myCustomEndpoints")
public class MyCustomEndpoints {
@ReadOperation
public ShopData getData(){
return new ShopData("店小二","地球村");
}
@Data
@AllArgsConstructor
public static class ShopData{
private String name;
private String address;
}
}
management:
endpoints:
web:
exposure:
##暴露myCustomEndpoints
include: health,info,beans,env,myCustomEndpoints
访问:actuator/myCustomEndpoints 返回结果{"name":"店小二","address":"地球村"}
Health endpoint
可以通过health指标来判断应用程序的运行状况,可以通过一下属性配置查看health的详细信息
有三个只可以选择,nerver、when-authorized、always 默认微nerver
#如果show-components 没有指定 则用show-details的值进行展示
#如果show-components 指定了,则以show-components的配置为准
management.endpoint.health.show-details=always
management.endpoint.health.show-components=always
springboot 自带了一部分一些常见的健康指标(只列出一部分)
自定义健康指标
/**
* 验证custom.url 的值是否正确,如果正确则状态是up,如果不正确,则表示down
*/
@Component
public class MyCustomIndicator implements HealthIndicator {
@Value("${custom.url}")
private String customUrl;
@Override
public Health health() {
if("http://www.baidu.com".equalsIgnoreCase(customUrl)){
return Health.up().build();
}
return Health.down().withDetail("customUrl",customUrl).build();
}
}
custom.url = http://www.baidu.com
{"status":"UP","components":{"diskSpace":{"status":"UP","details":{"total":250685575168,"free":46600232960,"threshold":10485760,"exists":true}},"myCustomIndicator":{"status":"UP"},"ping":{"status":"UP"}}}
配置错误的custom.url 运行结果如下:
custom.url = http://www.baidu.com001
{"status":"DOWN","components":{"diskSpace":{"status":"UP","details":{"total":250685575168,"free":46598496256,"threshold":10485760,"exists":true}},"myCustomIndicator":{"status":"DOWN","details":{"customUrl":"http://www.baidu.com001"}},"ping":{"status":"UP"}}}
info endpoint
SpringBoot包含了许多自动配置的InfoContributor 下面列出几个常用的
自定义 Application Infomation
#通过设置info.* Spring属性来自定义ifo端点公开的数据
info.app.encoding = UTF-8
#info.app.java.source = 8
info.app.java.source = @java.version@
运行结果如下:
{"app":{"encoding":"UTF-8","java":{"source":"1.8.0_191"}}}
如果需要获取build info
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
然后运行结果如下
{"app":{"encoding":"UTF-8","java":{"source":"1.8.0_191"}},"build":{"version":"1.0-SNAPSHOT","artifact":"actuator-simple","name":"actuator-simple","group":"com.hs.springboot","time":"2021-06-04T02:51:53.878Z"}}
Loggers endpoint
用来查看和修改应用的日志级别 通过/actuator/loggers 可以查看所有的日志级别情况,由于访问内容比较多,就截取一部分
{"levels":["OFF","ERROR","WARN","INFO","DEBUG","TRACE"],"loggers":{"ROOT":{"configuredLevel":"INFO","effectiveLevel":"INFO"},"com":{"configuredLevel":null,"effectiveLevel":"INFO"},"com.hs":{"configuredLevel":null,"effectiveLevel":"INFO"},"com.hs.springboot":{"configuredLevel":null,"effectiveLevel":"INFO"},"com.hs.springboot.actuator":{"configuredLevel":null,"effectiveLevel":"INFO"},"com.hs.springboot.actuator.ActuatorSimpleApplication":{"configuredLevel":null,"effectiveLevel":"INFO"},"io":{"configuredLevel":null,"effectiveLevel":"INFO"},"io.micrometer":
如果想单独访问某个的日志配置,例如访问ROOT的,访问路径如下:/actuator/loggers/ROOT
//如果配置了configuredLevel 则effectiveLevel和configuredLevel相同,如果没有显示配置 则configuredLevel为null
{"configuredLevel":"INFO","effectiveLevel":"INFO"}
修改ROOT的日志级别 请求路径/actuator/loggers/ROOT 进行动态调整应用程序的日志级别 参数如下:
{
"configuredLevel": "DEBUG"
}
总结:
Actuator 提供了可以查看应用程序运行的状态信息,同时也可以自定义符合自己业务要求的健康检查
还可以和监控软件例如Prometheus 进行系统整合(后续介绍和Prometheus 相关的整合)
参考资料:
原文:https://www.cnblogs.com/hupang/p/14849411.html