SpringBoot基础之Spring Actuator

原文:滑动验证页面

SpringBoot有一个非常有用的功能,非常实用,可以了解服务的各种情况,例如健康检查连接之类

  1. 配置所有的actuator

management.endpoints.web.exposure.include=*
  1. 下面看下常用的一些api,/actuator/health

{
    "status": "UP"
}
  1. /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",
       ***
    ]
}
  1. /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"
            ]
        }
    ]
}
  1. /actuator/loggers

{
    "levels": [
        "OFF",
        "ERROR",
        "WARN",
        "INFO",
        "DEBUG",
        "TRACE"
    ],
    "loggers": {
        "ROOT": {
            "configuredLevel": "INFO",
            "effectiveLevel": "INFO"
        },
        "com": {
            "configuredLevel": null,
            "effectiveLevel": "INFO"
        }
    }
}
  1. /actuator/configprops

{
    "contexts": {
        "application": {
            "beans": {
                "spring.jpa-org.springframework.boot.autoconfigure.orm.jpa.JpaProperties": {
                    "prefix": "spring.jpa",
                    "properties": {
                        "mappingResources": [],
                        "showSql": false,
                        "generateDdl": false,
                        "properties": {}
                    }
                    ***
            }
        }
    }
}
  1. 允许指定的actuator

management.endpoint.<NAME>.enabled=true
management.endpoint.<NAME>.enabled=false
  1. 或者使用默认的enable为false,指定开启哪一个enable

management.endpoints.enabled-by-default.enabled=false
management.endpoint.health.enabled=true
management.endpoint.loggers.enabled=true
  1. 自定义现有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
  1. 配置完成上面的内容,再次访问地址/actuator/health可以看到新增的内容

{
    "status": "UP",
    "details": {
        "maxMemoryAAA": {
            "status": "UP"
        },
        ***
    }
}
  1. 自定义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
  1. 配置完成后,访问新增的api地址/actuator/readiness得到结果READY

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值