Spring Boot(五十三):SpringBoot Actuator之实现
1 场景介绍
对于一个大型的几十个、几百个微服务构成的微服务架构系统,在线上时通常会遇到下面一些问题,比如:
1. 如何知道哪些服务除了问题,如何快速定位? (健康状况)
2. 如何统一监控各个微服务的性能指标(内存、jvm、并发数、线程池、Http 请求统计)
3. 如何统一管理各个微服务的日志?(切换线上日志等级,快速搜索日志...)
4. 如何优雅管理服务下线(正在运行的线程不发生中断)
所以在这种大型分布式应用的环境下,我们如何能够快速发现问题、快速解决问题, 必须要有监控平台、(链路追踪、日志)
2 SpringBoot Actuator
SpringBoot自带监控功能Actuator,可以帮助实现对程序内部运行情况监控,比如监控状况、Bean加载情况、环境变量、日志信息、线程信息等
2.1 实现
项目结构如下:
新建springboot项目 actuator_admin为父模块
修改pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>actuator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>../01_actuator</module>
</modules>
<name>actuator</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
新建springboot Module:01_actuator
修改pom文件中的parent为actuator_admin模块。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-parent</artifactId>-->
<!-- <version>2.7.8-SNAPSHOT</version>-->
<groupId>com.example</groupId>
<artifactId>actuator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.actuator</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
启动01_actuator运行
访问localhost:8080接口
接口返回的内容如下:
{
"_links": {
"self": {
"href": "http://localhost:8081/actuator",
"templated": false
},
"health": {
"href": "http://localhost:8081/actuator/health",
"templated": false
},
"health-path": {
"href": "http://localhost:8081/actuator/health/{*path}",
"templated": true
}
}
}
以上暴露的接口都是可以访问的。但不是所有的接口的暴露出来了,下面我们将所有的接口全部暴露出来。
修改application.properties暴露所有监控信息为HTTP
server.port=8081
#以web方式暴露所有端点
management.endpoints.web.exposure.include= *
#暴露所有监控信息为HTTP
management.endpoints.enabled-by-default=true
#修改访问端口的后缀
management.endpoints.web.base-path= /handsome
访问localhost:8081/handsome
接口返回内容如下:
{
"_links": {
"self": {
"href": "http://localhost:8081/handsome",
"templated": false
},
"beans": {
"href": "http://localhost:8081/handsome/beans",
"templated": false
},
"caches-cache": {
"href": "http://localhost:8081/handsome/caches/{cache}",
"templated": true
},
"caches": {
"href": "http://localhost:8081/handsome/caches",
"templated": false
},
"health": {
"href": "http://localhost:8081/handsome/health",
"templated": false
},
"health-path": {
"href": "http://localhost:8081/handsome/health/{*path}",
"templated": true
},
"info": {
"href": "http://localhost:8081/handsome/info",
"templated": false
},
"conditions": {
"href": "http://localhost:8081/handsome/conditions",
"templated": false
},
"shutdown": {
"href": "http://localhost:8081/handsome/shutdown",
"templated": false
},
"configprops": {
"href": "http://localhost:8081/handsome/configprops",
"templated": false
},
"configprops-prefix": {
"href": "http://localhost:8081/handsome/configprops/{prefix}",
"templated": true
},
"env": {
"href": "http://localhost:8081/handsome/env",
"templated": false
},
"env-toMatch": {
"href": "http://localhost:8081/handsome/env/{toMatch}",
"templated": true
},
"loggers": {
"href": "http://localhost:8081/handsome/loggers",
"templated": false
},
"loggers-name": {
"href": "http://localhost:8081/handsome/loggers/{name}",
"templated": true
},
"heapdump": {
"href": "http://localhost:8081/handsome/heapdump",
"templated": false
},
"threaddump": {
"href": "http://localhost:8081/handsome/threaddump",
"templated": false
},
"metrics": {
"href": "http://localhost:8081/handsome/metrics",
"templated": false
},
"metrics-requiredMetricName": {
"href": "http://localhost:8081/handsome/metrics/{requiredMetricName}",
"templated": true
},
"scheduledtasks": {
"href": "http://localhost:8081/handsome/scheduledtasks",
"templated": false
},
"mappings": {
"href": "http://localhost:8081/handsome/mappings",
"templated": false
}
}
}
Health:监控状况
一个组件指标状态为Down则总状态信息Down,很多组件中都定制了Health子节点指标: 比如jdbc、redis、等等
shutdown:优雅关闭
注意需要web服务器的支持
配置文件添加:
server.shutdown=graceful
Metrics:运行时指标
Loggers:日志记录
配置文件添加
logging.file.name: D:/logs/xushu.log