目录

搭建Spring-Boot-Admin监控系统

搭建Spring Boot Admin监控系统

什么是Spring Boot Admin

Spring Boot Admin 是一个用于管理和监控 Spring Boot 应用程序的开源工具。它提供了一个用户友好的 Web 界面,用于集中管理和监控多个 Spring Boot 应用程序的运行状态、健康状况、日志、配置等信息。

Spring Boot Admin 的核心功能

  1. 应用监控

    • 实时监控 Spring Boot 应用程序的健康状态( UPDOWN 等)。
    • 显示应用程序的详细信息,如内存使用、线程状态、垃圾回收等。
  2. 日志管理

    • 查看应用程序的日志文件。
    • 支持动态调整日志级别(如 DEBUGINFOWARNERROR )。
  3. 配置管理

    • 查看应用程序的配置信息(如 application.propertiesapplication.yml )。
    • 支持动态修改配置(需结合 Spring Cloud Config 或 Spring Cloud Bus)。
  4. 性能监控

    • 监控应用程序的性能指标,如请求速率、响应时间、错误率等。
    • 支持集成 Micrometer 和 Prometheus。
  5. 通知功能

    • 支持通过邮件、Slack、PagerDuty 等渠道发送告警通知。
    • 当应用程序状态发生变化(如从 UP 变为 DOWN )时,自动发送通知。
  6. 集中管理

    • 支持同时监控多个 Spring Boot 应用程序。
    • 提供统一的 Web 界面,方便集中管理。

原理:客户端向服务端推送状态消息,如下图所示:

https://i-blog.csdnimg.cn/direct/a0a64e14a60d46f0b950a13df15cac53.png

官方文档:

服务器端

新建sprint boot项目

第一步

增加依赖spring-boot-admin-starter-server

https://i-blog.csdnimg.cn/direct/603d93ce2ce743ba8228a605b0e41110.png

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>3.0.4</version>
        </dependency>

注意版本号,一定要跟sprint boot的版本保持一致,如果spring boot是2.X,那就要找对应2.X的version,当前我用的是spring boot3,所以这里的version为3.0.4。

第二步

在@Configuration类或者主类上增加@EnableAdminServer启动服务器

https://i-blog.csdnimg.cn/direct/21fae6b5113a45d3aa43150ba1f53a37.png

第三步

在application.properties设置端口

server.port=9000

https://i-blog.csdnimg.cn/direct/df3d758d07e543c0b556ea9a714f6d7a.png

以上配置完成后,访问 ,就可以看到如下界面。

https://i-blog.csdnimg.cn/direct/6e7e27b774414b759a25fafb3babb0e5.png

客户端

新建sprint boot项目

第一步

在Spring Boot应用上增加spring-boot-admin-starter-client依赖

https://i-blog.csdnimg.cn/direct/1304251d5e4b44fa807e6b4be77b8bf9.png

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>3.0.4</version>
        </dependency>

version与服务器端保持一致。

第二步

开启指标监控就,并接入admin-server

https://i-blog.csdnimg.cn/direct/6be47a5aea7943c0a86435b1719c1d16.png

server.port=9001
spring.boot.admin.client.url=http://localhost:9000

management.endpoints.web.exposure.include=*

9001是客户端的端口号

是服务器端的地址

management.endpoints.web.exposure.include=* 表示显示完整的监控信息

第三步

刷新页面,就可以看到新增的实例

https://i-blog.csdnimg.cn/direct/d798ab62a01c4f79a371014714d6ab90.png

第四步

如果想看到数据库信息,可以增加对应的配置

https://i-blog.csdnimg.cn/direct/302974bf9bce42d7a726d8be0803b641.png

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>8.2.0</version> <!-- 使用最新版本 -->
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>3.2.0</version>
        </dependency>

https://i-blog.csdnimg.cn/direct/a3d5edd80dec42818195c47c47eed650.png

# 启用 health 端点的详细信息
management.endpoint.health.show-details=always
# 暴露数据库连接信息
management.endpoint.health.show-components=always


spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/novel?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=123456

novel是数据库名称

https://i-blog.csdnimg.cn/direct/09017a59eec944e6948b7c860d95801d.png

就可以在细节中,看到db的信息。

https://i-blog.csdnimg.cn/direct/98ecb1adc8454828a16831c9d2a874e7.png

参考文章: