spring-boot-starter-actuator

25 篇文章 0 订阅
本文介绍了如何在Spring Boot应用中使用Actuator进行健康检查和自定义监控指标。通过添加依赖和配置,可以实现端点的自定义,包括自定义健康指示器和自定义端点,展示详细的健康信息和应用信息。此外,还配置了跨域访问和暴露所有端点。
摘要由CSDN通过智能技术生成

在这里插入图片描述

pom.xml

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

ActuatorApplication

package com.example.actuator;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ActuatorApplication {

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

}

application.properties

server.port=8080

# 配置项目访问路径,配置后若访问actuator,需 /demo/acturator
#server.servlet.context-path=/demo

# 配置后,http://localhost:8080/actuator -> http://localhost:8080/manage 访问
#management.endpoints.web.base-path=/manage
# 自定义管理服务器端口
#management.server.port=8081
# 当配置为使用自定义端口时,还可以使用各种management.server.ssl.*属性为管理服务器配置自己的 SSL
#server.ssl.enabled=true
#server.ssl.key-store=classpath:store.jks
#server.ssl.key-password=secret
#management.server.ssl.enabled=false

# 默认所有配置改为可用
management.endpoints.enabled-by-default=true
# 开启某个的访问
management.endpoint.beans.enabled=true
# http://localhost:8080/actuator/health 显示详情
# com.example.actuator.config.CustomHealthIndicator 可自定义监控指标,并可进行监控
management.endpoint.health.show-details=always

# 开启web的访问
management.endpoints.web.exposure.include=*

# 配置跨域
management.endpoints.web.cors.allowed-origins=http://localhost:8080
management.endpoints.web.cors.allowed-methods=GET,POST

# 自定义应用信息,http://localhost:8080/actuator/info
info.app.name=MyService
info.app.description=My awesome service
info.app.version=1.0.0
# 数据信息从maven获取
info.build.artifact=@project.artifactId@
info.build.name=@project.name@
info.build.description=@project.description@
info.build.version=@project.version@

CustomHealthIndicator

package com.example.actuator.config;

import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;

/**
 * 自定义的健康指标 : http://localhost:8080/actuator/health 访问时即可看到
 * 类名 : xxHealthIndicator -> xx即为健康指标的名称
 */
@Component
public class CustomHealthIndicator extends AbstractHealthIndicator {

    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        if (true) {
            builder.up()
                    .withDetail("app", "Alive and Kicking")
                    .withDetail("error", "Nothing! I'm good.");
        } else {
            builder.down()
                    .withDetail("app", "Not Alive");
        }
    }

}

Custom2HealthIndicator

package com.example.actuator.config;

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

/**
 * 自定义的健康指标 : http://localhost:8080/actuator/health 访问时即可看到
 * 类名 : xxHealthIndicator -> xx即为健康指标的名称
 */
@Component
public class Custom2HealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        int errorCode = 0; // perform some specific health check
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }

}

CustomEndpoint

package com.example.actuator.config;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.context.annotation.Configuration;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义全新端点
 *  @EndPoint中的id不能使用驼峰法,需要以-分割
 *  @Spring Boot会去扫描@EndPoint注解下的@ReadOperation, @WriteOperation, @DeleteOperation注解,分别对应生成Get/Post/Delete的Mapping。注解中有个produces参数,可以指定media type, 如:application/json等
 *
 * 访问地址:
 *  http://localhost:8080/actuator/custom-endpoint
 *  http://localhost:8080/actuator/custom-endpoint/{unitType}
 *
 */
@Endpoint(id = "custom-endpoint")
@Configuration
public class CustomEndpoint {
    /**
     * 计算单位换算类型
     */
    private static final Map<String, Long> UNIT_TYPE = new HashMap<>();

    static {
        UNIT_TYPE.put("K", 1024L);
        UNIT_TYPE.put("M", 1024 * 1024L);
        UNIT_TYPE.put("G", 1024 * 1024 * 1024L);
    }

    @ReadOperation
    public Map<String, Object> endpoint() {
        Map<String, Object> map = new HashMap<>();

        MemoryMXBean memorymbean = ManagementFactory.getMemoryMXBean();
        map.put("堆内存", memorymbean.getHeapMemoryUsage());
        map.put("方法区内存", memorymbean.getNonHeapMemoryUsage());

        List<String> inputArgs = ManagementFactory.getRuntimeMXBean().getInputArguments();
        map.put("运行时设置的JVM参数", inputArgs);

        // 总的内存量
        long totle = Runtime.getRuntime().totalMemory();
        // 空闲的内存量
        long free = Runtime.getRuntime().freeMemory();
        // 最大的内存量
        long max = Runtime.getRuntime().maxMemory();
        Map<String, Long> params = new HashMap<>();
        params.put("totalMemory", totle);
        params.put("freeMemory", free);
        params.put("maxMemory", max);
        map.put("运行时内存情况", params);
        return map;
    }

    /**
     * @Selector 会将路径上的参数作为变量传递给操作方法
     * init约等于xms的值,max约等于xmx的值。used是已经被使用的内存大小,committed是当前可使用的内存大小(包括已使用的),committed >= used。committed不足时jvm向系统申请,若超过max则发生OutOfMemoryError错误
     */
    @ReadOperation
    public Map<String, Object> select(@Selector String unitType) {
        long conversion = UNIT_TYPE.getOrDefault(unitType.toUpperCase(), 1L);
        Map<String, Object> map = new HashMap<>();

        // 堆内存
        MemoryMXBean memorymbean = ManagementFactory.getMemoryMXBean();

        // 方法区内存
        MemoryUsage heapMemoryUsage = memorymbean.getHeapMemoryUsage();
        long heapInit = heapMemoryUsage.getInit() / conversion;
        long heapCommitted = heapMemoryUsage.getCommitted() / conversion;
        long heapUsed = heapMemoryUsage.getUsed() / conversion;
        long heapMax = heapMemoryUsage.getMax() / conversion;

        Map<String, Long> heapMap = new HashMap<>();
        heapMap.put("init", heapInit);
        heapMap.put("committed", heapCommitted);
        heapMap.put("used", heapUsed);
        heapMap.put("max", heapMax);
        map.put("堆内存", heapMap);

        MemoryUsage nonHeapMemoryUsage = memorymbean.getNonHeapMemoryUsage();
        long noHeapInit = nonHeapMemoryUsage.getInit() / conversion;
        long noHeapCommitted = nonHeapMemoryUsage.getCommitted() / conversion;
        long noHeapUsed = nonHeapMemoryUsage.getUsed() / conversion;
        long noHeapMax = nonHeapMemoryUsage.getMax() / conversion;

        Map<String, Long> noHeapMap = new HashMap<>();
        noHeapMap.put("init", noHeapInit);
        noHeapMap.put("committed", noHeapCommitted);
        noHeapMap.put("used", noHeapUsed);
        noHeapMap.put("max", noHeapMax);
        map.put("方法区内存", noHeapMap);

        List<String> inputArgs = ManagementFactory.getRuntimeMXBean().getInputArguments();
        map.put("运行时设置的JVM参数", inputArgs);

        // 总的内存量
        long totle = Runtime.getRuntime().totalMemory();
        // 空闲的内存量
        long free = Runtime.getRuntime().freeMemory();
        // 最大的内存量
        long max = Runtime.getRuntime().maxMemory();
        Map<String, Long> params = new HashMap<>();
        params.put("totalMemory", totle / conversion);
        params.put("freeMemory", free / conversion);
        params.put("maxMemory", max / conversion);
        map.put("运行时内存情况", params);
        return map;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小安灬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值