spring boot actuator监控管理的简单使用
1. 引入maven依赖
<!--actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. 自定义监控端点
@ReadOperation
是对应 GET
请求
@WriteOperation
是对应POST
请求
@DeleteOperation
是对应DELETE
请求
package space.goldchen.springboot.actuator;
import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpoint;
import org.springframework.stereotype.Component;
/**
* 自定义监控端点
* @author chenzhao
* @create 2023-06-03 13:24
*/
@WebEndpoint(id="myEndpoint")
@Component
public class MyEndpoint {
/**
* 对应GET请求
* @return
*/
@ReadOperation
public String readMyEndpoint(){
return "Get:readMyEndpoint";
}
/**
* 对应POST请求
* @return
*/
@WriteOperation
public String writeMyEndpoint(){
return "Post:writeMyEndpoint";
}
/**
* 对应DELETE请求
* @return
*/
@DeleteOperation
public String deleteMyEndpoint(){
return "Delete:deleteMyEndpoint";
}
}
3. 配置文件设置
# actuator监控
management:
endpoint:
shutdown:
enabled: true # 关机
health:
enabled: true # 健康状态
endpoints:
enabled-by-default: false # 禁用默认
web:
exposure:
include: "health,shutdown,myEndpoint"
exclude: "env,beans"
4. 启动验证
1)访问:http://localhost:8080/actuator/
,
返回:
{
"_links": {
"self": {
"href": "http://localhost:8080/actuator",
"templated": false
},
"myEndpoint": {
"href": "http://localhost:8080/actuator/myEndpoint",
"templated": false
},
"health": {
"href": "http://localhost:8080/actuator/health",
"templated": false
},
"health-path": {
"href": "http://localhost:8080/actuator/health/{*path}",
"templated": true
},
"shutdown": {
"href": "http://localhost:8080/actuator/shutdown",
"templated": false
}
}
}
2)访问:http://localhost:8080/actuator/health
,
返回:
{
"status": "UP"
}
3)访问:http://localhost:8080/actuator/shutdownh
,执行远程关机
返回:
{
"message": "Shutting down, bye..."
}
4)访问自定义端点:http://localhost:8080/actuator/myEndpoint
,
分别使用GET/POST/DELETE请求,分别返回如下:
Get:readMyEndpoint
Post:writeMyEndpoint
Delete:deleteMyEndpoint