版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u011314442/article/details/101111791
在微服务体系中,有很多工程相互作用,如果其中某一个Down 了,可能就不是那么容易快速定位 bug 所属了。
此时服务的监督、管理就显得很有意义了。
SpringBoot 提供了 Actuator ,用以简单方便的对工程进行各项信息的监控、管理、查看...
1. 建一个 springboot 的工程:actuator。(springcloud 对 actuator 有扩展,但它是 springboot 就有的。)
工程结构如下:
2. 引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
完整 pom.xml 为:
<?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>
<groupId>springcloud-actuator</groupId>
<artifactId>actuator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>actuator</name>
<description>监控、管理项目</description>
<parent>
<groupId>com.base</groupId>
<artifactId>base-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<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>
</project>
3. 配置文件:
# 项目名
spring.application.name= actuator
# 端口
server.port= 5555
# 暴露各个端点 ,默认只暴露 2 个端点:health、info 。
management.endpoints.web.exposure.include= *
# health 细节默认不显示,开启显示
management.endpoint.health.show-details= always
4. 启动工程,提示有15个端点暴露:
5. 请求actuator 工程,查看运行状态、相关配置、信息 ...
另一个工程信息:(对 actuator 的使用完全一样:pom 依赖和配置一样。只是工程业务内容不一样,信息更加丰富。)
还可查看当前工程的具体信息:
6. 左例 (ID列),即为各个端点名,Description 为对应详细解说。红框选中的是上面已经体验过的几种端点 。
以此方式,通过对各个暴露端点的访问,可随时监控工程,甚至是自动调整 。
6. 更多详细说明见 springboot 文档:
https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#production-ready
7. 代码地址:
https://gitee.com/FJ_WoMenDeShiJie/springcloud-actuator
也推荐另一文:Springboot:监控与管理 Actuator