Actuator是什么?先从官网解释几句:SpringBoot可以帮助你在应用推送到生产环境时进行监控和管理。您可以选择使用http断点或JMX来管理和监控应用程序。审计、健康和指标收集可以自动应用于您的应用程序。总结一下:Actuator用于监控您的应用程序。快速开始引入依赖org.springframework.bootspring-boot-starter-actuatororg.springframework.bootspring-boot-starter-web一般来说,这两个依赖相互配合.yml和自动配置下面是一个yml配置:server:port:8081management:endpoints:web:base-path:/actuator#Actuator提供的API接口根目录Exposure:include:'*'#需要的端点tobeopenedexclude:#需要排除的端点management.endpoints.web对应的配置类是:org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties。相关自动配置在org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration中完成,有兴趣的同学可以查看源码。basePath是Actuator提供的API接口的根目录,默认配置为/actuator。Exposure.include表示需要打开的端点。默认只打开health和info两个断点。设置*可以打开所有端点。exposure.include表示排除的端点,设置*表示排除所有端点。主程序类@SpringBootApplicationpublicclassSpringBootActuatorApplication{publicstaticvoidmain(String[]args){SpringApplication.run(SpringBootActuatorApplication.class,args);}}测试访问http://localhost:8081/actuato...,得到application的健康信息如下:{status:"UP"//open}Endpoints由于我们已经设置了所有Endpointsopen,所以当我们启动程序时,可以看到控制台输出:[restartedMain]o.s.b.a.e.web.EndpointLinksResolver:Exposing13endpoint(s)下面的基本路径'/actuator'非常清楚。总共公开了13个内置端点。Java训练这些端点。SpringBoot文档已经解释的很详细了。事实上,有超过13个内置端点。官方文档说:每个端点都可以通过HTTP或JMX启用或禁用并公开。它仅在端点同时启用和公开时可用,内置端点仅在自动配置生效时可用。大多数应用程序会选择HTTP暴露的端点,我们可以通过/actuator/health访问ID为health的端点。所有端点的官方列表对于JMX和Web都是通用的。如果你有一个web应用程序(SpringMVC、SpringWebFlux或Jersey),你可以使用以下额外的端点:启动端点如果你想启动一个端点,你可以按照management.endpoint.的配置。enabled启动,以关闭端点为例:management:endpoint:shutdown:enabled:true如果只想启动某个端点,可以执行以下操作:management:endpoints:enabled-by-default:false#不要启用默认配置endpoint:info:enabled:true此示例将仅启用info端点。暴露的端点端点可能包含敏感信息,在考虑暴露时应仔细考虑。Web程序默认暴露的端点只有两个:health和info。JMX程序默认公开所有端点。我们可以使用include和exclude属性来控制端点是否需要暴露。以下是两个例子。1.不要让JMX暴露所有端点,只让它暴露health和info端点。management:endpoints:jmx:exposure:include:"health,info"其次,公开除env和beans之外的所有web端点。management:endpoints:web:exposure:include:"*"exclude:"env,beans"management.endpoints.web.exposure.include=*management.endpoints.web.exposure.exclude=env,beans配置端点endpoint会自动缓存对不带参数的读取操作的响应。您可以使用cache.time-to-live属性对其进行配置。以下示例将bean端点缓存的生存时间设置为10秒。management:endpoint:beans:cache:time-to-live:"10s"发现页面默认情况下,您可以访问/actuator页面以获取所有端点信息:{_links:{self:{href:"http://localhost:8081/actuator",templated:false},health:{href:"http://localhost:8081/actuator/health",templated:false},health-path:{href:"http://localhost:8081/actuator/health/{*path}",templated:true},info:{href:"http://localhost:8081/actuator/info",templated:false}}}跨域支持默认支持CORS已禁用,并且仅在设置了management.endpoints.web.cors.allowed-origins属性时才启用。接下来,我们来测试一下。测试之前不要忘记启动yml中的设置:server:port:8081management:endpoints:web:exposure:include:'*'#需要开启端点。默认情况下,仅打开health和info端点。通过设置*enabled-by-default:falseendpoint:myEndPoint:enabled:true我们暂时关闭所有其他端点,只关注myEndPoint端点,然后启动程序,访问:http://localhost:8081/actuato...,获取json信息。{max-memory:3787980800,free-memory:235775968,available-processors:4,total-memory:298844160}其实如果用于SpringMVC或者SpringWebFlux,我们可以使用@RestControllerEndpoint或者@ControllerEndpoint注解定义一个更符合我们平时web开发模型的端点,可以看看下面这个java训练的例子。@Component@RestControllerEndpoint(id="web")publicclassMyWebEndPoint{@GetMapping("/")publicMapgetData(){//...returnmap;}}有这么多Healthendpoint端点,我们挑几个来学习,health作为默认开放的endpoints之一,还是需要很好的理解。下面的代码展示了一个案例:ComponentpublicclassMyHealthIndicatorimplementsHealthIndicator{@OverridepublicHealthhealth(){//执行一些特定的健康检查booleancheck=check();if(!check){returnHealth.down().withDetail("错误代码",0).建造();}returnHealth.up().build();}privatebooleancheck(){returnnewRandom().nextBoolean();}}id是没有HealthIndicator后缀的bena的名字,这里的id就是我的。看看相关的yml是怎么配置的?management:endpoints:web:exposure:include:'*'#需要开放端点。默认情况下,仅打开health和info端点。通过设置*,可以打开所有端点。enabled-by-default:falseendpoint:health:enabled:trueshow-details:always#何时显示完整的健康信息show-components:alwaysstatus:http-mapping:#设置不同健康状态对应的响应状态码DOWN:503order:FATAL,DOWN,OUT_OF_SERVICE,UP,UNKNOWN#测试状态排序,访问:/actuator/health,可以尝试点击几次,会有UP和DOWN,会得到如下信息:{status:"DOWN",components:{diskSpace:{status:"UP",details:{total:267117391872,free:130840469504,threshold:10485760,exists:true}},my:{status:"DOWN",details:{错误代码:0}},ping:{status:"UP"}}}my对应我们自定义的MyHealthIndicator,里面的详细信息包括:status状态,信息详情。diskSpace对应DiskSpaceHealthIndicator,ping对应PingHealthIndicator。其他端点,有兴趣的朋友可以去官网一一测试。简而言之,通过Actuator提供的端点,我们可以轻松地监控和管理我们的应用程序。来源互联网后端架构