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();
}
}