SpringBoot有一个非常有用的功能,非常实用,可以了解服务的各种情况,例如健康检查连接之类
- 配置所有的actuator
management.endpoints.web.exposure.include=*
- 下面看下常用的一些api,/actuator/health
{
"status": "UP"
}
- /actuator/metrics
{
"names": [
"jvm.memory.max",
"jvm.threads.states",
"jdbc.connections.active",
"jvm.gc.memory.promoted",
"jvm.memory.used",
"jvm.gc.max.data.size",
"jdbc.connections.max",
"jdbc.connections.min",
"jvm.gc.pause",
"jvm.memory.committed",
"system.cpu.count",
***
]
}
- /actuator/metrics/jvm.memory.max
{
"name": "jvm.memory.max",
"description": "The maximum amount of memory in bytes that can be used for memory management",
"baseUnit": "bytes",
"measurements": [
{
"statistic": "VALUE",
"value": 5.560598527E9
}
],
"availableTags": [
{
"tag": "area",
"values": [
"heap",
"nonheap"
]
},
{
"tag": "id",
"values": [
"Compressed Class Space",
"PS Survivor Space",
"PS Old Gen",
"Metaspace",
"PS Eden Space",
"Code Cache"
]
}
]
}
- /actuator/loggers
{
"levels": [
"OFF",
"ERROR",
"WARN",
"INFO",
"DEBUG",
"TRACE"
],
"loggers": {
"ROOT": {
"configuredLevel": "INFO",
"effectiveLevel": "INFO"
},
"com": {
"configuredLevel": null,
"effectiveLevel": "INFO"
}
}
}
- /actuator/configprops
{
"contexts": {
"application": {
"beans": {
"spring.jpa-org.springframework.boot.autoconfigure.orm.jpa.JpaProperties": {
"prefix": "spring.jpa",
"properties": {
"mappingResources": [],
"showSql": false,
"generateDdl": false,
"properties": {}
}
***
}
}
}
}
- 允许指定的actuator
management.endpoint.<NAME>.enabled=true
management.endpoint.<NAME>.enabled=false
- 或者使用默认的enable为false,指定开启哪一个enable
management.endpoints.enabled-by-default.enabled=false
management.endpoint.health.enabled=true
management.endpoint.loggers.enabled=true
- 自定义现有actuator中的内容
@Component
public class MaxMemoryAAAHealthIndicator implements HealthIndicator {
@Override
public Health health() {
boolean invalid = Runtime.getRuntime().maxMemory() <(100*100*1024);
Status status = invalid ? Status.DOWN : Status.UP;
return Health.status(status).build();
}
}
//需要在配置中增加一些内容
management.endpoint.health.show-details=always
- 配置完成上面的内容,再次访问地址/actuator/health可以看到新增的内容
{
"status": "UP",
"details": {
"maxMemoryAAA": {
"status": "UP"
},
***
}
}
- 自定义actuator,其中ReadOperation,WriteOperation,DeleteOperation分别对应get,post,delete访问的方式
@Component
@Endpoint(id="readiness")
public class ReadinessEndpoint {
private String ready = "NOT_READY";
@ReadOperation
public String getReadiness(){
return ready;
}
@WriteOperation
public String writeOperation(){
return ready+"_WRITE";
}
@DeleteOperation
public String deleteOperation(){
return ready+"_DELETE";
}
@EventListener(ApplicationReadyEvent.class)
public void setReady(){
ready = "READY";
}
}
//增加新的actuator到配置文件中
management.endpoints.web.exposure.include=beans,metrics,info,health,loggers,readiness
- 配置完成后,访问新增的api地址/actuator/readiness得到结果READY