- 配置文件的读取
- 自动装配
Spring Boot Actuator
为生产准备的特性。当你发布应用的试试,Spring Boot 包含一堆特性 来帮助你监控和管理应用。你选择通过Http 端点或者JMX来管理和监控你的应用。审计、健康和度量收集自动装配到你的应用。
启用Spring Boot Actuator
加入maven依赖即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
端点
端点列表
JMX和Web是对应的ID默认启用状态
| ID | 描述 | JMX | Web |
|---|---|---|---|
auditevents | 暴露当前应用的审计事件信息,需要AuditEventRepository的Bean | Yes | No |
beans | 显示当前应用的完整的Bean列表 | Yes | No |
caches | 暴露可获取的缓存 | Yes | No |
conditions | 显示装配或者自动装配成功或者失败的条件 | Yes | No |
configprops | 显示@ConfigurationProperties的列表 | Yes | No |
env | 暴露ConfigurableEnvironment的配置信息 | Yes | No |
flyway | 显示应用的任意数据库迁移,需要Flyway的Bean | Yes | No |
health | 显示应用的健康信息 | Yes | Yes |
httptrace | 显示Http调用信息(默认显示最后100个http请求响应),需要HttpTraceRepository的bean | Yes | No |
info | 显示任意应用信息 | Yes | Yes |
intergationgraph | 显示Spring 聚合图表,需要spring-integration-core依赖 | Yes | No |
loggers | 显示和修改应用的日志配置 | Yes | No |
liquibase | 显示应用的任意数据库迁移,需要Liquibase的Bean | Yes | No |
metrics | 显示当前的应用的度量指标 | Yes | No |
mappings | 显示@RequestMapping列表 | Yes | No |
scheduledtasks | 显示定时任务列表 | Yes | No |
shutdown | 优雅地关闭应用,默认是关闭的 | Yes | No |
theaddump | 执行线程转存 | Yes | No |
若是web应用的话,还有以下端点
| ID | 秒速 | JMX | Web |
|---|---|---|---|
sessions | 允许检索和删除用户session信息,需要一个Servletweb应用 | Yes | No |
heapdump | 返回一个hprof的堆转存文件 | N/A | No |
jolokia | 通过http暴露JMX的Bean,需要jolokia-core,在WebFlux不可用 | N/A | No |
logfile | 返回日志文件的内容(需要配置logging.file.name和logging.file.path),支持http的Range请求头来返回当前日志文件的部分信息 | N/A | No |
promethueus | 暴露prometheus的度量信息,需要micrometer-registry-prometheus依赖 | N/A | No |
启用端点
management.endpoint.<id>.enabled=true启用对应的id的端点management.endpoints.enabled-by-default=false默认都不启用management.endpoints.<type>.exposure.include=info,health启用info和health端点,type包含jms以及webmanagement.endpoints.<type>.exposure.exclude=info,health关闭info和health端点,type包含jms以及web
include与exclude所有的内容,使用*,注意在yaml配置*的时候需要引号management:
endpoints:
web:
exposure:
include: "*"
端点安全
Spring Boot 提供了RequestMatcher对象联合Spring Security使用
@Configuration(proxyBeanMethods = false)
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(EndpointRequest.toAnyEndpoint())
.authorizeRequests((requests) ->
requests.anyRequest().hasRole("ENDPOINT_ADMIN"));
http.httpBasic();
}
}
若是被Spring Security阻止,可以设置所有放行
@Configuration(proxyBeanMethods = false)
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(EndpointRequest.toAnyEndpoint())
.authorizeRequests((requests) ->
requests.anyRequest().permitAll());
}
}
跨域配置
management.endpoints.web.cors.allowed-origins=https://example.com跨域源management.endpoints.web.cors.allowed-methods=GET,POST跨域访问方法
自定义实现
@Bean上添加@EndPoint注解,任意方法上添加@ReadOperation、@WriteOperation和@DeleteOperation在JMX以及Web上都会暴露。若是只想暴露一种,使用@JmxEndpoint或者@WebEndpoint。使用@EndpointWebExtension和@EndpointJmxExtension在已经暴露的端点上添加参数。使用Servlet或者Spring注解@Controller和@RestController来禁止JMX或者其他的web环境。
健康信息配置
-
management.endpoint.health.show-details=never默认不显示详情信息when-authorized认证信息,配置management.endpoint.health.roles进行配置,默认的话所有认证的用户都有权限always所有用户都显示
-
management.endpoint.health.show-components -
management.health.defaults.enabled关闭健康检查信息