7、SpringBoot 监控

18 阅读3分钟

SpringBoot监控概述

SpringBoot自带监控功能Actuator,可以帮助实现对程序内部运行情况监控,比如监控状况、Bean加载情况、配置属性 、日志信息等。

导入依赖坐标

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

启动项目,访问 actuator 暴露的接口

项目启动后会在控制台打印actuator 提供的接口信息 localhost:8080/actuator

访问该接口会显示几个不同作用的地址

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        "health-path": {
            "href": "http://localhost:8080/actuator/health/{*path}",
            "templated": true
        }
    }
}

其中 health 地址可以查看当前项目的启动情况以及一些组件的情况,比如我们在项目中引入了 redis 依赖坐标,但是此时没有开启 redis 服务,就会看到报错.注意,没有在 配置文件中 中开启展示详情时,只能看到 springboot 项目是否正常运行

未开启展示详情

{
    "status": "DOWN"
}

开启展示详情

# 开启显示健康检查的完整信息
management.endpoint.health.show-details=always
{
    "status": "DOWN",
    "components": {
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 245107195904,
                "free": 168727097344,
                "threshold": 10485760,
                "path": "/Users/hxy/code/java/IdeaProjects/springboot-condition/.",
                "exists": true
            }
        },
        "ping": {
            "status": "UP"
        },
        "redis": {
            "status": "DOWN",
            "details": {
                "error": "org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis"
            }
        }
    }
}

启动 redis 服务之后

{
    "status": "UP",
    "components": {
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 245107195904,
                "free": 168726020096,
                "threshold": 10485760,
                "path": "/Users/hxy/code/java/IdeaProjects/springboot-condition/.",
                "exists": true
            }
        },
        "ping": {
            "status": "UP"
        },
        "redis": {
            "status": "UP",
            "details": {
                "version": "7.2.4"
            }
        }
    }

暴露所有监控

# 暴露所有监控endpoint
management.endpoints.web.exposure.include=*
{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "beans": {
            "href": "http://localhost:8080/actuator/beans",
            "templated": false
        },
        "caches-cache": {
            "href": "http://localhost:8080/actuator/caches/{cache}",
            "templated": true
        },
        "caches": {
            "href": "http://localhost:8080/actuator/caches",
            "templated": false
        },
        "health-path": {
            "href": "http://localhost:8080/actuator/health/{*path}",
            "templated": true
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        "info": {
            "href": "http://localhost:8080/actuator/info",
            "templated": false
        },
        "conditions": {
            "href": "http://localhost:8080/actuator/conditions",
            "templated": false
        },
        "configprops": {
            "href": "http://localhost:8080/actuator/configprops",
            "templated": false
        },
        "configprops-prefix": {
            "href": "http://localhost:8080/actuator/configprops/{prefix}",
            "templated": true
        },
        "env": {
            "href": "http://localhost:8080/actuator/env",
            "templated": false
        },
        "env-toMatch": {
            "href": "http://localhost:8080/actuator/env/{toMatch}",
            "templated": true
        },
        "loggers": {
            "href": "http://localhost:8080/actuator/loggers",
            "templated": false
        },
        "loggers-name": {
            "href": "http://localhost:8080/actuator/loggers/{name}",
            "templated": true
        },
        "heapdump": {
            "href": "http://localhost:8080/actuator/heapdump",
            "templated": false
        },
        "threaddump": {
            "href": "http://localhost:8080/actuator/threaddump",
            "templated": false
        },
        "metrics-requiredMetricName": {
            "href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
            "templated": true
        },
        "metrics": {
            "href": "http://localhost:8080/actuator/metrics",
            "templated": false
        },
        "scheduledtasks": {
            "href": "http://localhost:8080/actuator/scheduledtasks",
            "templated": false
        },
        "mappings": {
            "href": "http://localhost:8080/actuator/mappings",
            "templated": false
        }
    }
}

比如,可以查看当前所有的 bean,可以查看所有的访问路径地址

SpringBoot监控 Spring Boot Admin

  • Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序。
  • Spring Boot Admin 有两个角色,客户端(Client)和服务端(Server)。这里的客户端就是被监控的 SpringBoot 应用,服务端就是查看监控的可视化界面
  • 应用程序作为Spring Boot Admin Client向为Spring Boot Admin Server注册
  • Spring Boot Admin Server 的UI界面将展示Spring Boot Admin Client的Actuator Endpoint 上的一些监控信息

使用步骤

admin server:
1️⃣创建 admin-server 模块
2️⃣导入坐标 admin-starter-server
3️⃣在引导类启用监控功能@EnableAdminServer

admin client:
1️⃣创建 admin-client 模块
2️⃣导入坐标 admin-starter-client
3️⃣配置相关信息:server 地址等
4️⃣启动 server 和 client 服务,访问 server
注意需要开启下面各种配置
spring.boot.admin.client.url=http://localhost:9000
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*