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