端点
常见端点
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=*