深入SpringBoot:自定义Endpoint

前言

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

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来实现应用监控的功能。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容