一、前言我们都知道Spring Boot是一个用于快速开发Java Web的框架不需要太多的配置即可使用Spring的大量功能。Spring Boot遵循着“约定大于配置”的原则许多功能使用默认的配置即可。这样的做法好处在于我们不需要像使用Spring那样编写一大堆的XML配置代码但过于简单的配置过程会让我们在了解各种依赖配置之间的关系过程上带来一些困难。不过没关系在Spring Boot中我们可以使用Actuator来监控应用Actuator提供了一系列的RESTful API让我们可以更为细致的了解各种信息。二、引入ActuatordependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency三、配置Actuatormanagement:endpoint:shutdown:enabled:truehealth:show-details:always#显示详细的健康信息endpoints:web:exposure:include:*#通过HTTP暴露Actuator endpoints。exclude:jmx:exposure:exclude:*#通过JMX暴露Actuator endpoints。include:配置中关闭了安全认证的功能如果需要开启这个功能的话还需引入spring-boot-starter-security依赖。除了使用Spring Security来开启监控路径安全认证外还可以使用Shiro对监控路径进行权限控制。监控的端口和应用一致配置context-path为/monitor这样可以避免和自己应用的路径映射地址重复。endpoints.shutdown.enabled: true提供了使用post请求来关闭Spring Boot应用的功能。四、Actuator接口列表以下是一些非常有用的actuator endpoints列表。你可以在official documentation上面看到完整的列表。HTTP方法路径描述GETauditevents显示应用暴露的审计事件(比如认证进入、订单失败)GETinfo显示应用的基本信息GEThealth显示应用的健康状态GETmetrics显示应用多样的度量信息GETloggers显示和修改配置的loggersGETlogfile返回logfile中的内容(如果logging.file或者logging.path被设置)GEThttptrace显示HTTP足迹最近100个HTTPrequest/repsponseGETenv显示当前的环境特性GETflyway显示数据库迁移路径的详细信息GETliquidbase显示Liquibase数据库迁移的纤细信息POSTshutdown让你逐步关闭应用GETmappings显示所有的RequestMapping路径GETscheduledtasks显示应用中的调度任务GETthreaddump执行一个线程dumpGETheapdump返回一个GZip压缩的JVM堆dump五、打开和关闭Actuator Endpoints默认上述所有的endpints都是打开的除了shutdown endpoint。你可以通过设置management.endpoint.{id}.enabled to true or false(id是endpoint的id)来决定打开还是关闭一个actuator endpoint。举个例子要想打开shutdown endpoint增加以下内容在你的application.properties文件中management:endpoint:shutdown:enabled:true六、暴露Actuator Endpoints默认素偶偶的actuator endpoint通过JMX被暴露而通过HTTP暴露的只有health和info。以下是你可以通过应用的properties可以通过HTTP和JMX暴露的actuator endpoint。6.1 通过HTTP暴露Actuator endpointsmanagement:endpoints:web:exposure:include:*#通过HTTP暴露Actuator endpoints。exclude:6.2 通过JMX暴露Actuator endpointsmanagement:endpoints:jmx:exposure:exclude:*#通过JMX暴露Actuator endpoints。include:通过设置management.endpoints.web.exposure.include为*我们可以在http://localhost:8080/actuator页面看到如下内容。{_links:{self:{href:http://localhost:8080/actuator,templated:false},beans:{href:http://localhost:8080/actuator/beans,templated:false},caches-cache:{href:http://localhost:8080/actuator/caches/{cache},templated:true},caches:{href:http://localhost:8080/actuator/caches,templated:false},health-path:{href:http://localhost:8080/actuator/health/{*path},templated:true},health:{href:http://localhost:8080/actuator/health,templated:false},info:{href:http://localhost:8080/actuator/info,templated:false},conditions:{href:http://localhost:8080/actuator/conditions,templated:false},shutdown:{href:http://localhost:8080/actuator/shutdown,templated:false},configprops:{href:http://localhost:8080/actuator/configprops,templated:false},env:{href:http://localhost:8080/actuator/env,templated:false},env-toMatch:{href:http://localhost:8080/actuator/env/{toMatch},templated:true},loggers:{href:http://localhost:8080/actuator/loggers,templated:false},loggers-name:{href:http://localhost:8080/actuator/loggers/{name},templated:true},heapdump:{href:http://localhost:8080/actuator/heapdump,templated:false},threaddump:{href:http://localhost:8080/actuator/threaddump,templated:false},metrics-requiredMetricName:{href:http://localhost:8080/actuator/metrics/{requiredMetricName},templated:true},metrics:{href:http://localhost:8080/actuator/metrics,templated:false},scheduledtasks:{href:http://localhost:8080/actuator/scheduledtasks,templated:false},mappings:{href:http://localhost:8080/actuator/mappings,templated:false}}}七、/health endpoint7.1 说明health endpoint通过合并几个健康指数检查应用的健康情况。Spring Boot Actuator有几个预定义的健康指标比如DataSourceHealthIndicator,DiskSpaceHealthIndicator, MongoHealthIndicator,RedisHealthIndicator, CassandraHealthIndicator等。它使用这些健康指标作为健康检查的一部分。举个例子如果你的应用使用RedisRedisHealthindicator将被当作检查的一部分。如果使用MongoDB那么MongoHealthIndicator将被当作检查的一部分。你也可以关闭特定的健康检查指标比如在yml中使用如下命令management:health:mongo:enabled:false7.2 显示详细的健康信息health endpoint只展示了简单的UP和DOWN状态。为了获得健康检查中所有指标的详细信息你可以通过在application.yaml中增加如下内容management: endpoint: health: show-details: always一旦你打开上述开关你在/health中可以看到如下详细内容7.3 创建一个自定义的健康指标你可以通过实现HealthIndicator接口来自定义一个健康指标或者继承AbstractHealthIndicator类。ComponentpublicclassCustomHealthIndicatorextendsAbstractHealthIndicator{OverrideprotectedvoiddoHealthCheck(Health.Builderbuilder)throwsException{builder.up().withDetail(app,Alive and Kicking).withDetail(error,Nothing! Im good.);}}一旦你增加上面的健康指标到你的应用中去后health endpoint将展示如下细节:八、/metrics endpointmetrics endpoint展示了你可以追踪的所有的度量。想要获得每个度量的详细信息你需要传递度量的名称到URL中像http://localhost:8080/actuator/metrics/{MetricName}举个例子获得systems.cpu.usage的详细信息使用以下URLhttp://localhost:8080/actuator/metrics/system.cpu.usage。它将显示如下内容:九、/loggers endpointloggers endpoint可以通过访问http://localhost:8080/actuator/loggers来进入。它展示了应用中可配置的loggers的列表和相关的日志等级。你同样能够使用http://localhost:8080/actuator/loggers/{name} 来展示特定logger的细节。举个例子为了获得root logger的细节你可以使用http://localhost:8080/actuator/loggers/root在运行时改变日志等级loggers endpoint也允许你在运行时改变应用的日志等级。举个例子为了改变root logger的等级为DEBUG 发送一个POST请求到http://localhost:8080/actuator/loggers/root加入如下参数 {“configuredLevel”: “DEBUG”}十、/info endpointinfo endpoint展示了应用的基本信息。它通过META-INF/build-info.properties来获得编译信息通过git.properties来获得Git信息。它同时可以展示任何其他信息只要这个环境property中含有infokey。你可以增加properties到application.yaml中比如info:app:name:project.namedescription:project.descriptionversion:project.versionencoding:project.build.sourceEncodingspring-boot-version:spring-boot.versionjava:version:java.version一旦你增加上面的propertiesinfo endpoint将展示如下信息十一、使用Spring Security来保证Actuator Endpoints安全ctuator endpoints是敏感的必须保障进入是被授权的。如果Spring Security是包含在你的应用中那么endpoint是通过HTTP认证被保护起来的。如果没有 你可以增加以下以来到你的应用中去dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-security/artifactId/dependency接下去让我们看一下如何覆写spring security配置并且定义你自己的进入规则。下面的例子展示了一个简单的spring securiy配置。它使用叫做EndPointRequest的ReqeustMatcher工厂模式来配置Actuator endpoints进入规则。ConfigurationpublicclassActuatorSecurityConfigextendsWebSecurityConfigurerAdapter{Overrideprotectedvoidconfigure(HttpSecurityhttp)throwsException{http.authorizeRequests().requestMatchers(EndpointRequest.to(ShutdownEndpoint.class)).hasRole(ACTUATOR_ADMIN).requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll().requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll().antMatchers(/).permitAll().antMatchers(/**).authenticated().and().httpBasic();}}为了能够测试以上的配置你可以在application.yaml中增加spring security用户spring:security:user:name:actuatorpassword:actuatorroles:ACTUATOR_ADMIN