EndPoint详解

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

简单单元测试

@Test
public void testHealth() throws Exception{
mockMvc.perform(MockMvcRequestBuilders.get("/health").contentType(MediaType.APPLICATION_JSON_UTF8)).andDo(new ResultHandler() {
@Override
public 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;
}
@Override
public 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 {
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
builder.up().withDetail("test","testHealth");
}
}
"testHelthIndicator":{"status":"UP","test":"testHealth"},

EndPoint详解的更多相关文章

  1. linux查看端口及端口详解

    今天现场查看了TCP端口的占用情况,如下图   红色部分是IP,现场那边问我是不是我的程序占用了tcp的链接,,我远程登陆现场查看了一下,这种类型的tcp链接占用了400多个,,后边查了一下资料,说E ...

  2. 基础拾遗------webservice详解

    基础拾遗 基础拾遗------特性详解 基础拾遗------webservice详解 基础拾遗------redis详解 基础拾遗------反射详解 基础拾遗------委托详解 基础拾遗----- ...

  3. WebConfig配置文件详解

    今天看到博客园一位朋友整理的一个WebConfig配置文件详解,觉得不错,转载一下: <?xml version="1.0"?> <!--注意: 除了手动编辑此文 ...

  4. css3径向渐变详解-遁地龙卷风

    (-1)写在前面 我用的是chrome49,如果你用的不是.可以尝试换下浏览器前缀.IE在这方面的实现又特例独行了.不想提及-,这篇是为后续做准备. (0)快速使用 background-image: ...

  5. WebConfig节点详解

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

  6. WebService核心文件【server-config.wsdd】详解及调用示例

    WebService核心文件[server-config.wsdd]详解及调用示例 作者:Vashon 一.准备工作 导入需要的jar包: 二.配置web.xml 在web工程的web.xml中添加如 ...

  7. 【OpenStack】OpenStack系列4之Glance详解

    下载安装 参考:http://www.linuxidc.com/Linux/2012-08/68964.htm http://www.it165.net/os/html/201402/7246.htm ...

  8. iOS开发——UI篇OC篇&SpriteKit详解

    SpriteKit详解 SpriteKit,iOS/Mac游戏制作的新纪元 这是我的WWDC2013系列笔记中的一篇,完整的笔记列表请参看这篇总览.本文仅作为个人记录使用,也欢迎在许可协议范围内转载或 ...

  9. Oracle Statspack报告中各项指标含义详解~~学习性能必看!!!

    Oracle Statspack报告中各项指标含义详解~~学习性能必看!!! Data Buffer Hit Ratio#<#90# 数据块在数据缓冲区中的命中率,通常应该在90%以上,否则考虑 ...

随机推荐

  1. android笔记:ViewPager实现界面的滑动

    最近在学习ViewPager实现界面的滑动,拜读了郭神的博客文章,并抽取归纳了自己对ViewPager的理解. ViewPager实现界面滑动的步骤如下: 1.在xml布局内加入控件android.s ...

  2. 开源PLM软件Aras详解三 服务端简易开发

    废话少说,直接进入主题, 以CAD为例: 先找到CAD对象类:具体操作见下图 双击打开,找到服务端事件:见下图 点击新建对象,点击添加,新建Method 编写Method,语言分为前端和后端,前端支持 ...

  3. AngularJS学习--- 动画操作 (Applying Animations) ngAnimate step 12

    1.切换目录 git checkout step-12 npm start 2.效果图 这里在点击右边的缩略图时,会有一个很明显的从下向上的动画过程. 3.代码实现: step11和step12之间的 ...

  4. iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)

    iOS页面间传值实现方法:1.通过设置属性,实现页面间传值:2.委托delegate方式:3.通知notification方式:4.block方式:5.UserDefault或者文件方式:6.单例模式 ...

  5. js获取文件大小

    var file = urlBox.doc.activeElement.files[0]||urlBox.files[0] ; if (file) { var fileSize = 0; if (fi ...

  6. IAP 破解漏洞验证

    IAP支付有个漏洞,用户使用的可能是IAP Free 或者俄罗斯破解什么的,所产生的交易号:170000029449420 product_id:com.zeptolab.ctrbonus.super ...

  7. oracle 多级菜单查询 。start with connect by prior

    select * from S_dept where CODE in(select sd.code from s_dept sd start with sd.code='GDKB' connect b ...

  8. su - root 报su: incorrect password的错误

    检查/bin/下面的文件的组属 和 sh 文件 的权限 是否有问题 例如:-rwxr-xr-x. 1 weblogic dba 34904 Jul 15  2011 /bin/su1   修改这个文件 ...

  9. shell脚本学习

    1.注释 如果使用bash,则在脚本文件头注释:#/bin/bash2.将脚本文件加上可读与执行权限,就可以使用./shell.sh来执行,也可以使用sh shell.sh的方式来直接执行,sh是ba ...

  10. 【洛谷P1378】油滴扩展

    搜索-- PS一个坑点:r<=0时并不是舍弃这种情况,而是让r=0 (因为每个点都要放一滴油)(读题啊!) #include<cstdio> #include<cstring& ...