Spring Boot Actuator
Spring Boot Actuator 的关键特性是在应用程序里提供众多 Web 接口,通过它们了解应用程序运行时的内部状况。Actuator 提供了 13 个接口,可以分为三大类:配置接口、度量接口和其它接口,具体如下表所示。
一 Actuator 的 REST 接口
HTTP | 路径 | 描述 |
---|---|---|
GET | /autoconfig | 提供了一份自动配置报告,记录哪些自动配置条件通过了,哪些没通过 |
GET | /configprops | 描述配置属性(包含默认值)如何注入Bean |
GET | /beans | 描述应用程序上下文里全部的Bean,以及它们的关系 |
GET | /dump | 获取线程活动的快照 |
GET | /env | 获取全部环境属性 |
GET | /env/{name} | 根据名称获取特定的环境属性值 |
GET | /health | 报告应用程序的健康指标,这些值由HealthIndicator的实现类提供 |
GET | /info | 获取应用程序的定制信息,这些信息由info打头的属性提供 |
GET | /mappings | 描述全部的URI路径,以及它们和控制器(包含Actuator端点)的映射关系 |
GET | /metrics | 报告各种应用程序度量信息,比如内存用量和HTTP请求计数 |
GET | /metrics/{name} | 报告指定名称的应用程序度量值 |
POST | /shutdown | 关闭应用程序,要求endpoints.shutdown.enabled设置为true |
GET | /trace | 提供基本的HTTP请求跟踪信息(时间戳、HTTP头等) |
二 Actuator 配置
Maven 配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Yaml 配置前缀
management.endpoint: # Actuator Prefix
management.endpoint.<name> # Endpoint Prefix
Web 上下文
# 默认配置
management.endpoints.web.base-path=/
# 建议配置
management.endpoints.web.base-path=/actuator
端点开放控制
- jmx
- web
# 1. 启用、关闭
management.endpoin.<name>.enabled=true # 启用 shutdown
e:
management.endpoin.shutdown.enabled=true # 启用 shutdown
management.endpoin.info.enabled=false # 关闭 info
# 2. 黑名单、白名单
# - 该配置关注 Http 、JMX 的白名单、黑名单规则。
# - exclude 黑名单优先级高于 include白名单
management.endpoints.jmx.exposure.exclude=env,beans # 排除 jmx
management.endpoints.jmx.exposure.include=* # 开放 jmx
management.endpoints.web.exposure.exclude=env,beans # 排除 jmx
management.endpoints.web.exposure.include=* # 开放 jmx
# 3. cache time
management.endpoint.beans.cache.time-to-live=10s
## 三 Security 配置
Maven 配置
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Yaml 配置
spring:
security:
user:
name: ***
password: ***
Java 配置类
package com.yuedu.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//对actuator监控所用的访问全部需要认证
http.formLogin().and().authorizeRequests().antMatchers("/actuator/*").authenticated();
}
}
参考:
- https://www.jianshu.com/p/af9738634a21
- https://blog.csdn.net/yaomingyang/article/details/84035975
官方:
- https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints