SpringActuator

Spring Boot Actuator 提供了一系列默认和可自定义的端点,用于健康检查、应用信息和监控。默认启用的端点有health和info,其他端点可通过include/exclude配置暴露。可以通过@Endpoint注解创建自定义端点,例如`/actuator/customActuator`。health端点提供应用状态检查,info端点展示应用信息。还可以自定义健康指标和应用信息贡献者。端点路径和服务器配置也可调整,如更改基础路径或设置白名单。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

端点

常见端点

Actuator中内置了大量的端点让我们使用,可用的端点如下
在这里插入图片描述

默认启动的端点

在Web中,默认启用的端点只有health、info,其余都是关闭的
在这里插入图片描述

如何暴露端点

在这里插入图片描述
include表示暴露的端点,exclude表示不要暴露的端点,exclude的优先级高于include,* 表示所有端点(注意:yaml中的 * 有特殊含义,如果使用了需要在两边添加引号),例子如下:

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans

自定义端点

如果在一个Bean上使用@Endpoint, 它的任何由@ReadOperation, @WriteOperation, @DeleteOperation标注的方法都会自动暴露在JMX和Web应用以及HTTP中。例如:

@RestController
@Endpoint(id = "customActuator")
class CustomActuator {

    @ReadOperation
    fun getCustomData(): String {
        return "ok 200"
    }

}

上例暴露了一个id为customActuator的端点,访问/actuator/customActuator可以看到正确的返回结果。

自定义带参的端点

POST  /actuator/customActuator

{
    "name": "test",
    "counter": 42
}
@WriteOperation
public void updateCustomData(String name, int counter) {
    // injects "test" and 42
}

在这里插入图片描述

Health 信息

使用health信息去检查你运行中应用的状态,它提供了下面三种授权方式,默认值是never
在这里插入图片描述

自动配置Health指标

Health指标会在合适的时候自动配置,你也可以去启用或禁用指标通过management.health.key.enabled,下表列出了所有默认配置key:
在这里插入图片描述
默认未配置的key
在这里插入图片描述

自定义Health指标

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class MyHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        int errorCode = check(); // perform some specific health check
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }

}

Application Info

info可以通过 /actuator/info 访问,你可以通过设置info.*来自定义暴露数据,例如:

info.app.encoding=UTF-8
info.app.java.source=11
info.app.java.target=11

或者用代码:

import java.util.Collections;

import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;

@Component
public class ExampleInfoContributor implements InfoContributor {

    @Override
    public void contribute(Info.Builder builder) {
        builder.withDetail("app",
                Collections.singletonMap("encoding", "UTF-8"));
    }

}

管理HTTP

自定义端点路径:

management.endpoints.web.base-path=/manage

注意:该路径是相对于server.servlet.context-path, 如果设置了management.server.port,那么management.endpoints.web.base-path相对于 management.server.base-path

自定义端口

management.server.port=8081

服务器白名单

下面的例子不允许远程服务器访问:

management.server.address=127.0.0.1

禁用HTTP端点

management.server.port=-1

或者

management.endpoints.web.exposure.exclude=*

参考

https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-endpoints

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值