EndPoint详解
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详解的更多相关文章
- linux查看端口及端口详解
今天现场查看了TCP端口的占用情况,如下图 红色部分是IP,现场那边问我是不是我的程序占用了tcp的链接,,我远程登陆现场查看了一下,这种类型的tcp链接占用了400多个,,后边查了一下资料,说E ...
- 基础拾遗------webservice详解
基础拾遗 基础拾遗------特性详解 基础拾遗------webservice详解 基础拾遗------redis详解 基础拾遗------反射详解 基础拾遗------委托详解 基础拾遗----- ...
- WebConfig配置文件详解
今天看到博客园一位朋友整理的一个WebConfig配置文件详解,觉得不错,转载一下: <?xml version="1.0"?> <!--注意: 除了手动编辑此文 ...
- css3径向渐变详解-遁地龙卷风
(-1)写在前面 我用的是chrome49,如果你用的不是.可以尝试换下浏览器前缀.IE在这方面的实现又特例独行了.不想提及-,这篇是为后续做准备. (0)快速使用 background-image: ...
- WebConfig节点详解
<!-- Web.config配置文件详解(新手必看) 花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法. 很适合新手参看,由于Web.config在使用很灵活,可 ...
- WebService核心文件【server-config.wsdd】详解及调用示例
WebService核心文件[server-config.wsdd]详解及调用示例 作者:Vashon 一.准备工作 导入需要的jar包: 二.配置web.xml 在web工程的web.xml中添加如 ...
- 【OpenStack】OpenStack系列4之Glance详解
下载安装 参考:http://www.linuxidc.com/Linux/2012-08/68964.htm http://www.it165.net/os/html/201402/7246.htm ...
- iOS开发——UI篇OC篇&SpriteKit详解
SpriteKit详解 SpriteKit,iOS/Mac游戏制作的新纪元 这是我的WWDC2013系列笔记中的一篇,完整的笔记列表请参看这篇总览.本文仅作为个人记录使用,也欢迎在许可协议范围内转载或 ...
- Oracle Statspack报告中各项指标含义详解~~学习性能必看!!!
Oracle Statspack报告中各项指标含义详解~~学习性能必看!!! Data Buffer Hit Ratio#<#90# 数据块在数据缓冲区中的命中率,通常应该在90%以上,否则考虑 ...
随机推荐
- 用python监控Linux,CPU,内存,硬盘
#!/usr/local/bin/python3.5 #coding:utf-8 import mailll, linecache, re, socket, os, time hostname = s ...
- php SPL常用接口
在PHP中有好几个预定义的接口,比较常用的四个接口(Countable.ArrayAccess.Iterator.IteratorAggregate(聚合式aggregate迭代器Iterator)) ...
- centos上手动编译安装tmux的问题
https://blog.linuxeye.com/323.html 装个tmux也是不容易.. 关键词:libevent要自己下载2.0版,然后编译.安装时还要各种参数指定目录.
- 【转】HTTP状态码(HTTP Status Code)
原文链接:http://www.chaoji.com/features/httpstatus.aspx 一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - ...
- caffe 安装在win 7 vs2015 无gpu的安装方式-是无法安装 的
网上多数是vs2012或者vs2013上安装方式,带NA-显卡的需要安装CUDA7.5,安装cuDNN4,cuDNN. 一 :下载caffe源码(microsoft版) 下载地址:https://gi ...
- Java实现多线程的两种方式
实现多线程的两种方式: 方式1: 继承Thread类 A: 自定义MyThread类继承Thread类 B: 在MyThread类中重写run() C: 创建MyThread类的对象 D: 启动线程对 ...
- three.js 之旅 (二)
three.js中各种场景的使用方法: 当然首先要先引入three.js库:其次,手动定义一个 canvas 标签. <script type="text/javascript&quo ...
- ubuntu卸载java
Try this command: java -version If 1.6* comes then try: sudo apt-get autoremove openjdk-6-jre If 1.7 ...
- Asp.Net Web API 2第二课——CRUD操作
详情请查看http://aehyok.com/Blog/Detail/69.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...
- 使用Js脚本 修改控制IE的注册表相关设置(activex等)
使用Js脚本 修改控制IE的注册表相关设置(activex等) 分类: PHP2012-12-05 18:51 2035人阅读 评论(2) 收藏 举报 脚本写法: <SCRIPT LANGUAG ...