SpringBoot揭密:spring-boot-starter-actuator与应用监控

SpringBootActuator为开发者提供了强大的应用监控功能,通过简单配置即可暴露HTTP端点,如health和metrics,用于系统健康检查和指标监控。Actuator对接MicroMeter,支持将数据上报至监控系统如Prometheus进行可视化和告警设置。引入相关依赖并配置后,可通过/actuator/prometheus接口获取监控数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        系统上线后其稳定性是至关重要的,开发者做到对系统运行状态及各种信息了如指掌,才有底气保证系统没有问题,很多开发者连通过运行数据来监控系统状态的意识都没有,系统上线稳定后是否稳定全凭运气和被动接收用户反馈才知道。

        SpringBoot提供了非常成熟的应用监控模块SpringBoot Actuator,其中Actuator可理解为探测器,通过spring-boot-starter-actuator开箱即用,即可获得通过HTTP或者JMX方式暴露的EndPoint,EndPoint可理解为Spring MVC中类似于Controller的Handler,引入以下依赖,即可获取该功能:

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

        配置management.endpoints.web.exposure.include=*,通过访问basePath:/actuator能看到目前提供的endpoint,典型的如health(可用于健康检查)、info、metrics(可用于指标监控)

{
    "_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": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        "health-path": {
            "href": "http://localhost:8080/actuator/health/{*path}",
            "templated": true
        },
        "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
        },
        "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
        }
    }
}

        这些信息均是可以自定义扩展的,特别是health和metrics,对于指标数据,SpringBoot Actuator对接了各种监控系统,支持将指标数据上报到监控系统可视化及配置告警,如监控系统prometheus和可视化grafana,Actuator采用了MicroMeter技术来采集指标,MicroMeter可理解为类似于SLF4J的门面,可以将监控指标数据Meter注册到各监控系统的注册表MeterRegister中,最终通过监控系统所需的格式上报上去,如果对接prometheus,可引入以下依赖:

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

        Actuator中引入了诸多自动装配类,但发现满足prometheus endpoint自动装配条件(@ConditionalOnClass(PrometheusMeterRegistry.class)普罗米修斯这个类正是上面依赖引入的)后即提供新的接口:http://localhost:8080/actuator/prometheus

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值