本文转自:https://www.jianshu.com/p/9fab4e81d7bb

最近在研究改写actuator的方式,这些放这里已备忘

Endpoint

SpringBoot的Endpoint主要是用来监控应用服务的运行状况,并集成在Mvc中提供查看接口。内置的Endpoint比如HealthEndpoint会监控dist和db的状况,MetricsEndpoint则会监控内存和gc的状况。
Endpoint的接口如下,其中invoke()是主要的方法,用于返回监控的内容,isSensitive()用于权限控制。

    public interface Endpoint<T> {
String getId();
boolean isEnabled();
boolean isSensitive();
T invoke();
}

Endpoint的加载还是依靠spring.factories实现的。spring-boot-actuator包下的META-INF/spring.factories配置了EndpointAutoConfiguration

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
...
org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration,\
...

EndpointAutoConfiguration就会注入必要的Endpoint。有些Endpoint需要外部的收集类,比如TraceEndpoint

    @Bean
@ConditionalOnMissingBean
public TraceEndpoint traceEndpoint() {
return new TraceEndpoint(this.traceRepository);
}

TraceEndpoint会记录每次请求的Request和Response的状态,需要嵌入到Request的流程中,这里就主要用到了3个类。

  1. TraceRepository用于保存和获取Request和Response的状态。
    public interface TraceRepository {
List<Trace> findAll();
void add(Map<String, Object> traceInfo);
}
  1. WebRequestTraceFilter用于嵌入web request,收集请求的状态并保存在TraceRepository中。
  2. TraceEndpointinvoke()方法直接调用TraceRepository保存的数据。
    public class TraceEndpoint extends AbstractEndpoint<List<Trace>> {
private final TraceRepository repository;
public TraceEndpoint(TraceRepository repository) {
super("trace");
Assert.notNull(repository, "Repository must not be null");
this.repository = repository;
}
public List<Trace> invoke() {
return this.repository.findAll();
}
}

Endpoint的Mvc接口主要是通过EndpointWebMvcManagementContextConfiguration实现的,这个类的配置也放在spring.factories中。

...
org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration=\
org.springframework.boot.actuate.autoconfigure.EndpointWebMvcManagementContextConfiguration,\
org.springframework.boot.actuate.autoconfigure.EndpointWebMvcHypermediaManagementContextConfiguration

EndpointWebMvcManagementContextConfiguration注入EndpointHandlerMapping来实现Endpoint的Mvc接口。

    @Bean
@ConditionalOnMissingBean
public EndpointHandlerMapping endpointHandlerMapping() {
Set<? extends MvcEndpoint> endpoints = mvcEndpoints().getEndpoints();
CorsConfiguration corsConfiguration = getCorsConfiguration(this.corsProperties);
EndpointHandlerMapping mapping = new EndpointHandlerMapping(endpoints,corsConfiguration);
boolean disabled = this.managementServerProperties.getPort() != null && this.managementServerProperties.getPort() == -1;
mapping.setDisabled(disabled);
if (!disabled) {
mapping.setPrefix(this.managementServerProperties.getContextPath());
}
if (this.mappingCustomizers != null) {
for (EndpointHandlerMappingCustomizer customizer : this.mappingCustomizers) {
customizer.customize(mapping);
}
}
return mapping;
}

自定义Endpoint

自定义Endpoint也是类似的原理。这里自定义Endpoint实现应用内存的定时收集。完整的代码放在Github上了。

  1. 收集内存,MemStatus是内存的存储结构,MemCollector是内存的收集类,使用Spring内置的定时功能,每5秒收集当前内存。
    public static class MemStatus {
public MemStatus(Date date, Map<String, Object> status) {
this.date = date;
this.status = status;
}
private Date date;
private Map<String, Object> status;
public Date getDate() {
return date;
}
public Map<String, Object> getStatus() {
return status;
}
}
    public static class MemCollector {
private int maxSize = 5;
private List<MemStatus> status;
public MemCollector(List<MemStatus> status) {
this.status = status;
}
@Scheduled(cron = "0/5 * * * * ? ")
public void collect() {
Runtime runtime = Runtime.getRuntime();
Long maxMemory = runtime.maxMemory();
Long totalMemory = runtime.totalMemory();
Map<String, Object> memoryMap = new HashMap<String, Object>(2, 1);
Date date = Calendar.getInstance().getTime();
memoryMap.put("maxMemory", maxMemory);
memoryMap.put("totalMemory", totalMemory);
if (status.size() > maxSize) {
status.remove(0);
status.add(new MemStatus(date, memoryMap));
} else {
status.add(new MemStatus(date, memoryMap));
}
}
}
  1. 自定义Endpoint,getIdEndPoint的唯一标识,也是Mvc接口对外暴露的路径。invoke方法,取出maxMemorytotalMemory和对应的时间。
    public static class MyEndPoint implements Endpoint {
private List<MemStatus> status;
public MyEndPoint(List<MemStatus> status) {
this.status = status;
}
public String getId() {
return "my";
}
public boolean isEnabled() {
return true;
}
public boolean isSensitive() {
return false;
}
public Object invoke() {
if (status == null || status.isEmpty()) {
return "hello world";
}
Map<String, List<Map<String, Object>>> result = new HashMap<String, List<Map<String, Object>>>();
for (MemStatus memStatus : status) {
for (Map.Entry<String, Object> entry : memStatus.status.entrySet()) {
List<Map<String, Object>> collectList = result.get(entry.getKey());
if (collectList == null) {
collectList = new LinkedList<Map<String, Object>>();
result.put(entry.getKey(), collectList);
}
Map<String, Object> soloCollect = new HashMap<String, Object>();
soloCollect.put("date", memStatus.getDate());
soloCollect.put(entry.getKey(), entry.getValue());
collectList.add(soloCollect);
}
}
return result;
}
}
  1. AutoConfig,注入了MyEndPoint,和MemCollector
    public static class EndPointAutoConfig {
private List<MemStatus> status = new ArrayList<MemStatus>();
@Bean
public MyEndPoint myEndPoint() {
return new MyEndPoint(status);
}
@Bean
public MemCollector memCollector() {
return new MemCollector(status);
}
}
  1. 程序入口,运行后访问http://localhost:8080/my 就可以看到了。
    @Configuration
@EnableAutoConfiguration
public class CustomizeEndPoint { public static void main(String[] args) {
SpringApplication application = new SpringApplication(CustomizeEndPoint.class);
application.run(args);
}
}

结语

Endpoint也是通过spring.factories实现扩展功能,注入了对应的Bean来实现应用监控的功能。


作者:wcong
链接:https://www.jianshu.com/p/9fab4e81d7bb
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

深入SpringBoot:自定义Endpoint(转)的更多相关文章

  1. 深入SpringBoot:自定义Endpoint

    前言 上一篇文章介绍了SpringBoot的PropertySourceLoader,自定义了Json格式的配置文件加载.这里再介绍下EndPoint,并通过自定EndPoint来介绍实现原理. En ...

  2. SpringBoot自定义拦截器实现IP白名单功能

    SpringBoot自定义拦截器实现IP白名单功能 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8993331.html 首先,相关功能已经上线了,且先让我先 ...

  3. SpringBoot自定义错误信息,SpringBoot适配Ajax请求

    SpringBoot自定义错误信息,SpringBoot自定义异常处理类, SpringBoot异常结果处理适配页面及Ajax请求, SpringBoot适配Ajax请求 ============== ...

  4. SpringBoot自定义错误页面,SpringBoot 404、500错误提示页面

    SpringBoot自定义错误页面,SpringBoot 404.500错误提示页面 SpringBoot 4xx.html.5xx.html错误提示页面 ====================== ...

  5. springboot自定义错误页面

    springboot自定义错误页面 1.加入配置: @Bean public EmbeddedServletContainerCustomizer containerCustomizer() { re ...

  6. SpringBoot自定义Filter

    SpringBoot自定义Filter SpringBoot自动添加了OrderedCharacterEncodingFilter和HiddenHttpMethodFilter,当然我们可以自定 义F ...

  7. springboot 自定义LocaleResolver切换语言

    springboot 自定义LocaleResolver切换语言 我们在做项目的时候,往往有很多项目需要根据用户的需要来切换不同的语言,使用国际化就可以轻松解决. 我们可以自定义springboor中 ...

  8. SpringMVC拦截器与SpringBoot自定义拦截器

    首先我们先回顾一下传统拦截器的写法: 第一步创建一个类实现HandlerInterceptor接口,重写接口的方法. 第二步在XML中进行如下配置,就可以实现自定义拦截器了 SpringBoot实现自 ...

  9. [技术博客] SPRINGBOOT自定义注解

    SPRINGBOOT自定义注解 在springboot中,有各种各样的注解,这些注解能够简化我们的配置,提高开发效率.一般来说,springboot提供的注解已经佷丰富了,但如果我们想针对某个特定情景 ...

随机推荐

  1. ORA-20002: [WF_NO_USER] NAME=<name> ORIG_SYSTEM=NULL ORIG_SYSTEM_ID=NULL

    Solution APPLIES TO: Identity Manager Connector - Version 10.1.2 to 10.1.2Oracle User Management - V ...

  2. 老码农冒死揭开行业黑幕:如何编写无法维护的代码[ZZ]

    下面是一篇有意思的"代码大全",可谓 逆软件工程. 老码农冒死揭开行业黑幕:如何编写无法维护的代码 原文如下 让自己稳拿铁饭碗 ;-) – Roedy Green(翻译版略有删节) ...

  3. 使用jetty-maven-plugin运行maven多项目

    1.准备工作 org.eclipse.jetty   jetty-maven-plugin    9.2.11.v20150529 jdk  1.7 maven  3.1 2.采用maven管理多项目 ...

  4. Golang Tcp粘包处理(转)

    在用golang开发人工客服系统的时候碰到了粘包问题,那么什么是粘包呢?例如我们和客户端约定数据交互格式是一个json格式的字符串: {"Id":1,"Name" ...

  5. vsftpd 常见问题

    一.vsftp服务能开启却连接不上的解决办法: 用虚拟机装了centos,vsftp是用centos自带的.启动vsftd服务后却一直连不上,原因是被防火墙给挡了. 查看防火墙状态:/etc/init ...

  6. AbpZero的Swagger汉化之旅

    做汉化主要是为了出一份前后端都能看得懂的在线文档,废话不多说,我们开始准备, 我们要在启动项目的Startup.cs中重定向一下swagger的读取方式 1.在这个类下面,新增一个方法: public ...

  7. ProtoBuf序列化和反序列化方法

    最近公司需要将以前的协议全都改成ProtoBuf生成的协议,再将结构体打包和解包过程终于到一些问题 ,无法使用Marshal.SizeOf计算结构体大小,最后找了一下ProtoBuf的文档,可以用它自 ...

  8. Socket网络编程(TCP/IP/端口/类)和实例

    Socket网络编程(TCP/IP/端口/类)和实例 原文:C# Socket网络编程精华篇 转自:微冷的雨 我们在讲解Socket编程前,先看几个和Socket编程紧密相关的概念: TCP/IP层次 ...

  9. 纯文本-FileOutputStream的解码方式

    1.通过string.getBytes(charsetNane)获得的字节数组,字节数组的编码方式,决定了FileOutStream写出文件的格式 例1:字节数组采用“GBK”编码,write生成的文 ...

  10. MySQL实例crash的案例分析

    [作者] 王栋:携程技术保障中心数据库专家,对数据库疑难问题的排查和数据库自动化智能化运维工具的开发有强烈的兴趣. [问题描述] 我们生产环境有一组集群的多台MySQL服务器(MySQL 5.6.21 ...