定义,全面监控SpringBoot项目。Actuator为springboot项目提供全面的监控和审查。通过启用和暴露端点,您可以轻松查看项目中的一些指标。添加依赖org.springframework.bootspring-boot-starter-actuator关于EndpointsActuator提供了很多内置的Endpoints,每个EP都可以启用/禁用并公开。当然,EP要等到推出并公开后才会真正生效。如何启用/禁用端点?除shutdown外,所有EP均默认启用。如果要启用/禁用EP,可以使用以下配置:management.endpoint..enabled=true/false。例如开启shutdown:management.endpoint.shutdown.enabled=true为了安全起见,生产环境不建议开启所有的EP,而是按需开启,所以先关闭默认行为,再开启需要的EP,如下所示:management.endpoints.enabled-by-default=falsemanagement.endpoint.info.enabled=true这样只有启用的EP才会加载到上下文中。这里只是启用和禁用EP,但是否暴露需要单独配置。如何公开/隐藏端点?EP会暴露应用的很多指标,所以它非常重要和敏感??。在生产环境中,一定要做好选择和保护。暴露和隐藏使用以下配置:#对于JMX规范协议management.endpoints.jmx.excludemanagement.endpoints.jmx.exposure.include*#对于http协议management.endpoints.web.excludemanagement.endpoints.web。接触。includehealth使用include暴露Ep,使用exclude隐藏Ep,其中exclude的优先级高于include。星号*表示全部,星号在properties和yaml中有点不同:星号(*)在YAML中有特殊的含义,所以需要用双引号括起来,比如"*",比如隐藏all和Only启用health和info:management.endpoints.web.exposure.include=health,info例如,公开所有并禁用env和beans:management.endpoints.web.exposure.include=*management.endpoints.web。exposure.exclude=env,beans上面说了EP中包含了很多敏感信息,一定要注意安全,怎么保证安全呢?如何实现HttpEndpoints安全加固如果项目中使用了springsecurity或shiro等安全框架,可以将EP放在安全框架后面,像其他业务API一样进行保护。或者,也可以使用RequestMatcher对象来控制某些用户有访问权限,例如:导入org.springframework.context.annotation.Configuration;导入org.springframework.security.config.annotation.web.builders.HttpSecurity;导入org.springframework.security.web.SecurityFilterChain;@Configuration(proxyBeanMethods=false)公共类MySecurity{@BeanpublicSecurityFilterChainsecurityFilterChain(HttpSecurityhttp)抛出异常http.httpBasic返回http.build();}}这样只有具有ENDPOINT_ADMIN角色的用户才能访问EP。如何通过项目的ip:port/actuator通过页面访问各种指标,比如localhost:8080/actuator:可以通过配置修改路径:#修改/actuor为/managemanagement.endpoints.web.base-path=/manage#修改具体指标/health为/myhealthmanagement.endpoints.web.path-mapping.health=myhealth#修改访问端口(默认与server.port相同)management.server.port=8036