springboot 自定义actuator信息

本文介绍了如何在Spring Boot应用中自定义Actuator端点,包括添加依赖、配置文件设置以开放web端点,展示JVM垃圾回收信息,并将自定义端点整合进Spring管理,最终可通过http://localhost:8080/actuator/myHealth进行访问。

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

springboot 自定义actuator信息

依赖
<dependencies>
	<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>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<scope>runtime</scope>
		<optional>true</optional>
	</dependency>
	<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
		<optional>true</optional>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
		<exclusions>
			<exclusion>
				<groupId>org.junit.vintage</groupId>
				<artifactId>junit-vintage-engine</artifactId>
			</exclusion>
		</exclusions>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-configuration-processor</artifactId>
		<optional>true</optional>
	</dependency>
</dependencies>
配置文件开放web端点
management:
  endpoints:
    web:
      exposure:
        include: '*'
这里展示了jvm垃圾回收相关信息
@Component
public class MyHealth implements HealthIndicator {
    @Override
    public Health health() {
        List<GarbageCollectorMXBean> gge = ManagementFactory.getGarbageCollectorMXBeans();
        Health.Builder builder = new Health.Builder();
        builder.status("run");
        builder.withDetail("garbage", gge);
        Health health = builder.build();
        return health;
    }
}
暴露端点可以在actuator中访问
@WebEndpoint(id = "myHealth")
public class MyEndPoint {

    @Autowired
    private MyHealth myHealth;

    @ReadOperation
    public Health my(){
        return myHealth.getHealth(true);
    }

}
加入到spring中管理
@Configuration
public class MyEndPoindConfiguration {

    @Bean
    public MyEndPoint myEndPoint(){
        return new MyEndPoint();
    }

}

请求路径:http://localhost:8080/actuator/myHealth

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值