1. 监控管理
通过引入 spring-boot-starter-actuator
,可以使用 Spring Boot 为我们提供的准生产环境下的应用监控和管理功能。我们可以通过 HTTP,JMX,SSH 协议来进行操作,自动得到审计、健康及指标信息等
端点名 | 描述 |
---|---|
autoconfig | 所有自动配置信息 |
auditevents | 审计事件 |
beans | 所有 Bean 的信息 |
configprops | 所有配置属性 |
dump | 线程状态信息 |
env | 当前环境信息 |
health | 应用健康状况 |
info | 当前应用信息 |
metrics | 应用的各项指标 |
mappings | 应用 @RequestMapping 映射路径 |
shutdown | 关闭当前应用(默认关闭) |
trace | 追踪信息(最新的 Http 请求) |
2. 环境搭建
代码已经上传至 https://github.com/masteryourself-tutorial/tutorial-spring ,详见
tutorial-spring-boot-core/tutorial-spring-boot-actuator
工程
2.1 配置文件
1. pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2. application.properties
#修改访问路径 2.0之前默认是/ 2.0默认是 /actuator 可以通过这个属性值修改
management.endpoints.web.base-path=/actuator
#开放所有页面节点 默认只开启了health、info两个节点
management.endpoints.web.exposure.include=*
#显示健康具体信息 默认不会显示详细信息
management.endpoint.health.show-details=always
2.2 核心代码
1. ActuatorApplication
@SpringBootApplication
public class ActuatorApplication {
public static void main(String[] args) {
SpringApplication.run(ActuatorApplication.class, args);
}
}
2. MyApplicationHealthIndicator
访问 http://localhost:8080/actuator/health
@Component
public class MyApplicationHealthIndicator implements HealthIndicator {
@Override
public Health health() {
return Health.up().withDetail("msg", "服务提供正常").build();
}
}