Spring Boot指标监控

指标监控 – Actuator

简介

未来每一个微服务在云上部署以后,我们都需要对其进行监控、追踪、审计、控制等。SpringBoot
就抽取了Actuator场景,使得我们每个微服务快速引用即可获得生产级别的应用监控、审计等功能。

启用

官网介绍

这是 spring官方的学习文档:列在这里供大家参考
https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator

流程

引入相关依赖:这里我使用的是 maven引入

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

配置

当依赖引入后,会默认在 localhost:port 开启监控
当然也可以自己进行配置

1
2
3
4
5
6
management:
endpoints:
enabled-by-default: true # 暴露所有端点信息
web:
exposure:
include: '*' # web方式暴露

随后启动 spring boot 功程访问 http://localhost:(你的port)/actuator

进阶用法

可视化

这是一个学习地址
https://github.com/codecentric/spring-boot-admin

搭建过程

  1. 首先创建一个新spring boot工程(模块)
    需要确保,此端口号和你的实际spring boot项目,不发生冲突
1
2
3
4
# 服务端配置
# 我这里开在 8888端口
server:
port: 8888
  1. 引入相关依赖
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    <!-- 服务端依赖 -->
    <!-- 引入 web模块, 可以打开一个可视化的网页窗口 -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 这个是开发工具 -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
    </dependency>
    <!-- 自带的测试模块 -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    <!-- 这个才是咱的重点,开启一个服务端 -->
    <dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.7.3</version>
    </dependency>
1
2
3
4
5
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.7.3</version>
</dependency>
1
2
3
4
5
spring:
boot:
admin:
client:
url: http://localhost:8888 # 服务端地址
  1. 优雅地启动
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    // 在你的主程序上方添加注解
    @SpringBootApplication
    @EnableAdminServer
    public class AdminServerApplication {

    public static void main(String[] args) {
    SpringApplication.run(AdminServerApplication.class, args);
    }

    }
    这时你就能看到,如下画面(客户端尚未开启):
    023a8b0236622d2db359eca868cfeb55.md.png

开启你的应用,看到这个画面,恭喜你(已经成功开启):
de26ed9ae394fe40a56d2e41c9f5b641.md.png


Spring Boot指标监控
http://lhystutest.top/2022/11/24/spring boot/指标监测/
作者
lhy
发布于
2022年11月24日
许可协议