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%以上,否则考虑 ...
随机推荐
- Unity3D手游开发日记(9) - 互动草的效果
所谓互动草,就是角色跑动或者释放技能,能影响草的摆动方向和幅度. 前面的文章早已经实现了风吹草动的效果,迟迟没有在Unity上面做互动草,是因为以前我在端游项目做过一套太过于牛逼的方案.在CE3的互动 ...
- java生成base64编码的png
java代码: 引用包: import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java. ...
- Asp.Net MVC4入门指南(2):添加一个控制器
MVC代表: 模型-视图-控制器 .MVC是一个架构良好并且易于测试和易于维护的开发模式.基于MVC模式的应用程序包含: · Models: 表示该应用程序的数据并使用验证逻辑来强制实施业务规则的数据 ...
- 使用JMeter进行负载测试——终极指南
这篇教程讨论的是JMeter,它是一款基于Java的.集合了几个应用程序.具有特定用途的负载和性能测试工具. 本篇主要涉及的内容: 解释一下JMeter的用途 JMeter的实现方式以及采用的技术 安 ...
- nginx 报错 upstream timed out (110: Connection timed out)解决方案
nginx 作PHP的web接口服务器. 在线上发现时不时经常崩溃.504,导致接口访问无响应回复. 查看日志: [error] 11618#0: *324911 upstream timed out ...
- C# HttpWebRequest与HttpWebResponse详解
C# HttpWebRequest与HttpWebResponse详解 http://www.codeproject.com/Articles/6554/How-to-use-HttpWebRequ ...
- FMDB基本应用
1.打开数据库 #import "ViewController.h" #import "FMDB.h" @interface ViewController () ...
- FreeBSD_11-系统管理——{Part_1-桌面}
一.Xorg 安装 xorg pkg install xorg 清除旧文件(如果已前安装过 xorg) /etc/X11/xorg.conf /usr/local/etc/X11/xorg.conf ...
- cocos2d-x 之 CCArray 的遍历(3)
cocos2d-x中CCArray的遍历,需要几个宏.现代C++程序设计建议尽量不要使用宏,所以数组的遍历也可以自己写. 但cocos2d-x官方已经提供了几个方便数组遍历的几个宏,用好了,能方便许多 ...
- Android入门
在学Android,摘自<第一行代码——Android> 布局管理 通过xml文件进行布局管理. android:id="@+id/button_1" 为当前的元素定义 ...