Spring Boot的Actuator在应用程序中提供众多Web端点,我们可以通过它们了解应用程序允许的状态,也可以通过提供的rest接口可以获取整个依赖于spring体系中bean如何组装在一起,也可以掌握应用程序允许的环境属性消息等
如何启用Actuator的端点,只需要在项目中引入Actuator的依赖:
在Maven中构建 :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
其中springboot2.x版本与spirngboot1.5x构建有很大不同
springboot2.x:
@Endpoint(id = "dtc-refresh"):@Endpoint设置为端点类id设置端点访问路径
@WriteOperation:类比于post请求作用
@DeleteOperation:类比于delete请求作用
@ReadOperation:类比于get请求作用
@Selector:接收请求参数
第一步定义端点类:
@Endpoint(id = "test")
public class TestEndpoint{
/**
*test
*/
@WriteOperation
public void test1() {
//编写代码
}
}
@ReadOperation
public void test2(@Selector String type) {
//
}
2将端点注入到spring中(使用@Componen注解或使用@bean)
3:在2.x系列 actuator默认只会开启info,health,如果需要开启所有端点访问(不开启反问会404):
###springboot2.x Actuator只暴露了health和info端点
##如果需要则使用下面配置
management:
endpoints:
web:
exposure:
include: "*"
4:最终访问http://localhost:8866/actuator/test

本文介绍了如何在Spring Boot 2.x应用中自定义Actuator端点。通过添加依赖,定义端点类并配置相应的操作(如@WriteOperation、@DeleteOperation和@ReadOperation),可以创建自定义的监控和管理功能。此外,文章还提到了2.x版本与1.5.x版本在构建上的差异,以及启用所有端点的步骤。
2164

被折叠的 条评论
为什么被折叠?



