您的位置:首页 > 编程语言 > Java开发

Spring Boot Actuator分析,自定义端点

2018-02-07 16:36 549 查看
 每个Actuator端点都是有一个特定的ID用来决定端点的路径。/beans端点的默认ID就是 beans。端点的路径是由ID决定的,那么可以通过修改ID来改变端点的路径。要做的就是设置一个属性, 属性名是 endpoints.endpoint-id.id

修改端点的ID:
   如把/beans改为/beansome:
   endpoints.beans.id=beansome
   这时要是想查看bean的信息时,路径就由原来的/beans变为/beansome;
   开启和禁用端点:  
     默认情况下,所有端点(除了/shutdown)都是启用的。 禁用端点所有的端点:    endpoints.enabled=false
     禁用某个特定的端点:
     endpoints.endpoint-id.enabled=false
     禁用后,再次访问该端点的URL时,会出现404错误。

默认端点信息:
HTTP方法路径描述鉴权
GET/autoconfig查看自动配置的使用情况true
GET/configprops查看配置属性,包括默认配置true
GET/beans查看bean及其关系列表true
GET/dump打印线程栈true
GET/env查看所有环境变量true
GET/env/{name}查看具体变量值true
GET/health查看应用健康指标false
GET/info查看应用信息false
GET/mappings查看所有url映射true
GET/metrics查看应用基本指标true
GET/metrics/{name}查看具体指标true
POST/shutdown关闭应用true
GET/trace查看基本追踪信息true
自定义端点
首先,我们需要继承 AbstractEndpoint 抽象类。因为它是 Endpoint 接口的抽象实现,此外,我们还需要重写 invoke 方法。
@ConfigurationProperties(prefix = "endpoints.person")
public class PersonEndpoint extends AbstractEndpoint<Map<String, Object>> {

public PersonEndpoint() {
super("person", false);
}

@Override
public Map<String, Object> invoke() {
Map<String, Object> result = new HashMap<String, Object>();
DateTime dateTime = DateTime.now();
result.put("当前时间", dateTime.toString());
result.put("当前时间戳", dateTime.getMillis());
return result;
}
}
通过设置 @ConfigurationProperties(prefix = "endpoints.person"),我们就可以在 application.properties 中通过 endpoints.person 配置我们的端点。
上面就是自定义的端点,简单解释下:构造方法PersonEndpoint(),两个参数分别表示端点 ID 和是否端点默认是敏感的。我这边设置端点 ID 是 servertime,它默认不是敏感的。
我们需要通过重写 invoke 方法,返回我们要监控的内容。这里我定义了一个 Map,它将返回两个参数,一个是标准的包含时区的当前时间格式,一个是当前时间的时间戳格式

创建配置类,实例化bean
@Configuration
public class EndpointConfig {

@Bean
public static Endpoint<Map<String, Object>> servertime() {
return new PersonEndpoint();
}
}

启动Spring Boot工程在浏览器中访问http://localhost:8080/person

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息