使用Spring Boot搭建微服务框架已经是Java微服务开发的主流场景。微服务监控是微服务运维管理的重要一环。Spring Actuator拥有操作简单,监控指标完善,扩展性好的优点。

参考Spring官方指导文档,梳理了一下在项目中如何使用Spring Actuator。

一、配置依赖

  1. 在pom文件中引入依赖的jar包:
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

配套使的spring boot版本是2.1.6.RELEASE。

  1. 在yml中添加actuator管理配置:
management:
  security:
    enabled: false
  endpoints:
    web:
      exposure:
         include: health,metrics
  endpoint:
    health:
      show-details: always

管理接口基本上都是在内部环境调用,简化处理不配置安全认证。

二、微服务健康指标

Actuator默认提供了微服务状态,数据库连接,MQ状态、磁盘占用等指标数据的查看功能。访问方式是服务URL+actuator/health。样例代码的访问地址是:http://localhost:10000/elon/elon-actuator/actuator/health

除了默认支持的健康指标外,还可以根据自己的需要自定义:从HealthIndicator派生自定义指标类,注入到Spring框架。

package com.elon.actuator.service;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.Status;
import org.springframework.stereotype.Component;

/**
 * 自定义健康检查指标
 *
 * @author elon
 * @since 2020-12-13
 */
@Component
public class CustomHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        Health.Builder builder = new Health.Builder();
        builder.status(Status.UP);

        /**
         * 搜集所有自定义指标数据,封装到Health中返回
         */
        builder.withDetail("自定义健康指标1", "正常").withDetail("自定义健康指标2", "异常");

        return builder.build();
    }
}

从Web端调用http://localhost:10000/elon/elon-actuator/actuator/health接口时,CustomHealthIndicator的health方法会被调用,可以将搜集的指标数据封装到Health中返回。

调用接口返回的数据如下。可以看到自定义的健康指标数据一同返回了。

{
    "status": "UP",
    "details": {
        "custom": {
            "status": "UP",
            "details": {
                "自定义健康指标1": "正常",
                "自定义健康指标2": "异常"
            }
        },
        "db": {
            "status": "UP",
            "details": {
                "database": "MySQL",
                "hello": 1
            }
        },
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 214747312128,
                "free": 129423261696,
                "threshold": 10485760
            }
        }
    }
}

三、查看微服务度量指标

Actuator除了监控微服务整体的健康状态外,还能看很多细的指标数据。如:内存占用情况、CPU利用率、线程、WEB会话等。

调用http://localhost:10000/elon/elon-actuator/actuator/metrics可以看到能监控哪些度量指标:

{"names":["jvm.memory.max","tomcat.threads.busy","jvm.threads.states","jvm.gc.memory.promoted","jvm.memory.used","jvm.gc.max.data.size","jdbc.connections.max","jdbc.connections.min","jvm.memory.committed","system.cpu.count","jvm.buffer.memory.used","tomcat.sessions.created","jvm.threads.daemon","system.cpu.usage","jvm.gc.memory.allocated","tomcat.global.sent","tomcat.sessions.expired","tomcat.global.request.max","jvm.threads.live","jvm.threads.peak","tomcat.global.request","process.uptime","tomcat.sessions.rejected","tomcat.global.received","process.cpu.usage","jvm.classes.loaded","jvm.gc.pause","jvm.classes.unloaded","tomcat.sessions.active.current","tomcat.threads.config.max","tomcat.sessions.alive.max","jvm.gc.live.data.size","log4j2.events","tomcat.global.error","custom.metrics1","custom.metrics2","jvm.buffer.count","tomcat.threads.current","jvm.buffer.total.capacity","tomcat.sessions.active.max","process.start.time"]}

查看单个度量指标的值,使用http://localhost:10000/elon/elon-actuator/actuator/metrics/{指标项拼接 }。例如查看jvm.memory.max的值,调用接口http://localhost:10000/elon/elon-actuator/actuator/metrics/jvm.memory.max

四、自定义微服务度量指标

package com.elon.actuator.service;

import com.google.common.util.concurrent.AtomicDouble;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Component;

/**
 * 自定义度量指标
 *
 * @author elon
 * @since 2020-12-13
 */
@Component
public class CustomMetrics {
    // 保存自定义度量指标值
    private AtomicDouble value1 = new AtomicDouble(0);
    private AtomicDouble value2 = new AtomicDouble(0);

    public CustomMetrics(MeterRegistry registry) {
        // 注册指标
        registry.gauge("custom.metrics1", value1);
        registry.gauge("custom.metrics2", value2);
    }

    public void setValue1(double value1) {
        this.value1.set(value1);
    }

    public void setValue2(double value2) {
        this.value2.set(value2);
    }
}

注册完后调用接口http://localhost:10000/elon/elon-actuator/actuator/metrics,可以看到自定义指标已列出:

java监控健康检查接口实现 springboot健康监控_spring boot

查看自定义指标值的方式与默认指标一致,都是用URL+指标项名称拼接。

自定义健康指标是在查询时实时收集数据,自定义度量指标是在注册指标时绑定了一个指标值变量(框架提供了多种注册方式,这是其中一种)。

通过测试接口改变了指标值后,再调用查询接口http://localhost:10000/elon/elon-actuator/actuator/metrics/custom.metrics1。可以看到指标值发生了变化。

DEMO代码路径:https://github.com/ylforever/elon-actuator