EndPoint详解

EndPoint主要用于暴露一些SpringMvc内部运行的信息,通常是通过SpringMvc的请求地址获取相关信息。如/health获取健康检查信息。

简单单元测试

@Test
public void testHealth() throws Exception{mockMvc.perform(MockMvcRequestBuilders.get("/health").contentType(MediaType.APPLICATION_JSON_UTF8)).andDo(new ResultHandler() {@Overridepublic void handle(MvcResult result) throws Exception {System.out.println(result.getResponse().getContentAsString());}});
}
//结果
{"status":"UP","discoveryComposite":{"description":"Discovery Client not initialized","status":"UNKNOWN","discoveryClient":{"description":"Discovery Client not initialized","status":"UNKNOWN"}},"diskSpace":{"status":"UP","total":197553815552,"free":46789115904,"threshold":10485760},"refreshScope":{"status":"UP"},"hystrix":{"status":"UP"},"consul":{"status":"UP","services":{"consul":[]},"advertiseAddress":"172.17.0.8","datacenter":"dc1","domain":"consul.","nodeName":"node-client-v-5-1","bindAddress":"0.0.0.0","clientAddress":"0.0.0.0"}}

url映射

HandlerMapping使用EndpointHandlerMapping,重写了registerHandlerMethod,主要是注册HandlerMethod时重写请求路径。

private String[] getPatterns(Object handler, RequestMappingInfo mapping) {String path = getPath(handler);String prefix = StringUtils.hasText(this.prefix) ? this.prefix + path : path;Set<String> defaultPatterns = mapping.getPatternsCondition().getPatterns();if (defaultPatterns.isEmpty()) {return new String[] { prefix, prefix + ".json" };}List<String> patterns = new ArrayList<String>(defaultPatterns);for (int i = 0; i < patterns.size(); i++) {patterns.set(i, prefix + patterns.get(i));}return patterns.toArray(new String[patterns.size()]);
}
private String getPath(Object handler) {if (handler instanceof String) {handler = getApplicationContext().getBean((String) handler);}if (handler instanceof MvcEndpoint) {return ((MvcEndpoint) handler).getPath();}return "";
}

如果handler时MvcEndpoint类型,则请求路径调用getPath方法获取。

健康检查为例/health

实现
public HealthEndpoint(HealthAggregator healthAggregator,Map<String, HealthIndicator> healthIndicators) {super("health", false);Assert.notNull(healthAggregator, "HealthAggregator must not be null");Assert.notNull(healthIndicators, "HealthIndicators must not be null");CompositeHealthIndicator healthIndicator = new CompositeHealthIndicator(healthAggregator);for (Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) {healthIndicator.addHealthIndicator(getKey(entry.getKey()), entry.getValue());}this.healthIndicator = healthIndicator;
}
@Overridepublic Health invoke() {return this.healthIndicator.health();}

最终回调用HealthIndicator的health()方法。而健康检查的HealthIndicator为CompositeHealthIndicator。其health方法:

@Override
public Health health() {Map<String, Health> healths = new LinkedHashMap<String, Health>();for (Map.Entry<String, HealthIndicator> entry : this.indicators.entrySet()) {healths.put(entry.getKey(), entry.getValue().health());}return this.healthAggregator.aggregate(healths);
}

所以,HealthIndicator才是最为关键的,CompositeHealthIndicator只是将所有的HealthIndicator合并。

自定义健康检查实现,即实现HealthIndicator。
@Component
public class TestHelthIndicator extends AbstractHealthIndicator {@Overrideprotected void doHealthCheck(Health.Builder builder) throws Exception {builder.up().withDetail("test","testHealth");}
}
"testHelthIndicator":{"status":"UP","test":"testHealth"},

转载于:https://www.cnblogs.com/dragonfei/p/6241759.html

EndPoint详解相关推荐

  1. SpringBoot2.x系列教程(七十)Spring Boot Actuator集成及自定义Endpoint详解

    前言 曾经看到Spring Boot Actuator这个框架时,一直在想,它到底有什么作用呢?虽然知道它提供了很多端点,有助于应用程序的监控和管理,但如果没有直接的实践案例,还是很难有说服力的. 直 ...

  2. Web.config配置文件详解(新手必看)

    Web.config配置文件详解(新手必看) 花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点. ...

  3. grpc通信原理_容器原理架构详解(全)

    目录 1 容器原理架构 1.1 容器与虚拟化 1.2 容器应用架构 1.3 容器引擎架构 1.4 Namespace与Cgroups 1.5 容器镜像原理 2 K8S原理架构 2.1 K8S主要功能 ...

  4. 《Linux设备驱动开发详解 A》一一2.3 接口与总线

    本节书摘来华章计算机出版社<Linux设备驱动开发详解 A>一书中的第2章,第2.3节,作者:宋宝华 更多章节内容可以访问云栖社区"华章计算机"公众号查看.1 2.3 ...

  5. 解析 http 请求 header 错误_详解http报文(2)-web容器是如何解析http报文的

    摘要 在详解http报文一文中,详细介绍了http报文的文本结构.那么作为服务端,web容器是如何解析http报文的呢?本文以jetty和undertow容器为例,来解析web容器是如何处理http报 ...

  6. WebService入门详解

    1.什么是webservice 先来考虑一个问题,如果我们要在自己的程序里面展示天气预报,那怎么弄?正确的做法是我们发送一个请求到一个系统,他会给我们返回来天气情况.这个就是一个webservice. ...

  7. 史上最详细Docker安装最新版Minio 带详解 绝对值得收藏!!! 让我们一起学会使用minio搭建属于自己的文件服务器!!走上白嫖之路!解决启动了但是浏览器访问不了的原因

    让我们一起学会使用minio搭建属于自己的文件服务器!!走上白嫖之路! WARNING: Console endpoint is listening on a dynamic port (34451) ...

  8. Python日志详解【两篇就够了系列】--第二篇loguru

    目录 第二章 Python日志loguru库详解 一.loguru简介 二.日志级别 三.loguru日志常用参数配置解析 1.rotation 2.retention 3.compression 4 ...

  9. go使用grpc实现异步_(python、go)基于ETCD的gRPC分布式服务器实现详解

    作者:Zarten知乎专栏:框架工具篇详解知乎ID: Zarten简介: 互联网一线工作者,尊重原创并欢迎评论留言指出不足之处,也希望多些关注和点赞是给作者最好的鼓励 ! 1-概述 gRPC框架是一个 ...

最新文章

  1. Velocity文档(3)
  2. td 中连续数字或连续英文内容不自动换行
  3. 用Python 给你的个人微信朋友圈数据生成一本电子书吧!
  4. javaBean List Map json(转)
  5. 信用卡申请被拒原因分析
  6. 开课吧:一文解析Nexus是什么
  7. Atitit 编程范式 体系树 目录 1. 编程范型、编程范式或程序设计法(英语:Programming paradigm) 1 2. 编程范式 2 3. 声明式编程体系树 3 3.1. 声明式(对
  8. opencv打开本地文件.avi提示找不到xvidcore.dll
  9. Google浏览器调试页面时设置分辨率
  10. TDTX云笔记--TDTX个人简历
  11. js正则表达式之match函数
  12. CSS文本溢出打点显示
  13. java多属性的map_java集合(四)Map集合之Properties详解
  14. safari 模拟手机显示
  15. 鉴于B站的代码粘贴没有全选功能,up在这里放上软件小妹的脚本代码
  16. Java课设 2048小游戏
  17. 数据挖掘(4.1)--分类和预测
  18. 微信公众号三方平台开发【帐号注册、平台创建】
  19. EduCoder-Web程序设计基础-html5—移动端电商页面制作-(第1关:移动端电商页面制作)
  20. Linux系统中的软件管理详解(下)—搭建网络软件仓库及第三方软件仓库

热门文章

  1. 非接触IC卡中typeA卡和typeB卡的区别--总结,二者的调制方式和编码方式不同
  2. yuv数据(nv12和nv21)和RGB数据之间转换的c++代码
  3. 用Java语言编写打印菱形
  4. Cannot create an instance of class AndroidViewModel (androidx ViewModelProvider AndroidViewModel)
  5. 【ICPR 2021】遥感图中的密集小目标检测:Tiny Object Detection in Aerial Images
  6. pip 如何指定国内源安装
  7. 嵌入式笔试面试问题总结
  8. 怎么找贷款意向客户?
  9. Hibernate对原生sql处理及结果集和VO的映射
  10. 2020年教师资格证考试课件百度云网盘地址分享