hystrix源码小贴士之之hystrix-metrics-event-stream
hystrix-metrics-event-stream主要提供了一些servlet,可以让用户通过http请求获取metrics信息。
HystrixSampleSseServlet
继承了HttpServlet,不断从sampleStream中读取值并返回,直到sampleStream发送complete或出现异常。
private void handleRequest(HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
...
sampleSubscription = sampleStream
.observeOn(Schedulers.io())
.subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
logger.error("HystrixSampleSseServlet: ({}) received unexpected OnCompleted from sample stream", getClass().getSimpleName());
moreDataWillBeSent.set(false);
}
@Override
public void onError(Throwable e) {
moreDataWillBeSent.set(false);
}
@Override
public void onNext(String sampleDataAsString) {
if (sampleDataAsString != null) {
try {
writer.print("data: " + sampleDataAsString + "\n\n");
// explicitly check for client disconnect - PrintWriter does not throw exceptions
if (writer.checkError()) {
moreDataWillBeSent.set(false);
}
writer.flush();
} catch (Exception ex) {
moreDataWillBeSent.set(false);
}
}
}
}); while (moreDataWillBeSent.get() && !isDestroyed) {
try {
Thread.sleep(pausePollerThreadDelayInMs);
//in case stream has not started emitting yet, catch any clients which connect/disconnect before emits start
writer.print("ping: \n\n");
// explicitly check for client disconnect - PrintWriter does not throw exceptions
if (writer.checkError()) {
moreDataWillBeSent.set(false);
}
writer.flush();
} catch (Exception ex) {
moreDataWillBeSent.set(false);
}
}
}
} finally {
decrementCurrentConcurrentConnections();
if (sampleSubscription != null && !sampleSubscription.isUnsubscribed()) {
sampleSubscription.unsubscribe();
}
}
}
HystrixConfigSseServlet
继承HystrixSampleSseServlet,读取HystrixConfigurationStream流数据。
public HystrixConfigSseServlet() {
this(HystrixConfigurationStream.getInstance().observe(), DEFAULT_PAUSE_POLLER_THREAD_DELAY_IN_MS);
} /* package-private */ HystrixConfigSseServlet(Observable<HystrixConfiguration> sampleStream, int pausePollerThreadDelayInMs) {
super(sampleStream.map(new Func1<HystrixConfiguration, String>() {
@Override
public String call(HystrixConfiguration hystrixConfiguration) {
return SerialHystrixConfiguration.toJsonString(hystrixConfiguration);
}
}), pausePollerThreadDelayInMs);
}
HystrixMetricsStreamServlet
继承HystrixSampleSseServlet,读取HystrixDashboardStream流数据。
public HystrixMetricsStreamServlet() {
this(HystrixDashboardStream.getInstance().observe(), DEFAULT_PAUSE_POLLER_THREAD_DELAY_IN_MS);
} /* package-private */ HystrixMetricsStreamServlet(Observable<HystrixDashboardStream.DashboardData> sampleStream, int pausePollerThreadDelayInMs) {
super(sampleStream.concatMap(new Func1<HystrixDashboardStream.DashboardData, Observable<String>>() {
@Override
public Observable<String> call(HystrixDashboardStream.DashboardData dashboardData) {
return Observable.from(SerialHystrixDashboardData.toMultipleJsonStrings(dashboardData));
}
}), pausePollerThreadDelayInMs);
}
HystrixRequestEventsSseServlet
继承HystrixSampleSseServlet,读取HystrixRequestEventsStream流数据。
public HystrixRequestEventsSseServlet() {
this(HystrixRequestEventsStream.getInstance().observe(), DEFAULT_PAUSE_POLLER_THREAD_DELAY_IN_MS);
} /* package-private */ HystrixRequestEventsSseServlet(Observable<HystrixRequestEvents> sampleStream, int pausePollerThreadDelayInMs) {
super(sampleStream.map(new Func1<HystrixRequestEvents, String>() {
@Override
public String call(HystrixRequestEvents requestEvents) {
return SerialHystrixRequestEvents.toJsonString(requestEvents);
}
}), pausePollerThreadDelayInMs);
}
HystrixUtilizationSseServlet
继承HystrixSampleSseServlet,读取HystrixUtilizationStream流数据。
public HystrixUtilizationSseServlet() {
this(HystrixUtilizationStream.getInstance().observe(), DEFAULT_PAUSE_POLLER_THREAD_DELAY_IN_MS);
} /* package-private */ HystrixUtilizationSseServlet(Observable<HystrixUtilization> sampleStream, int pausePollerThreadDelayInMs) {
super(sampleStream.map(new Func1<HystrixUtilization, String>() {
@Override
public String call(HystrixUtilization hystrixUtilization) {
return SerialHystrixUtilization.toJsonString(hystrixUtilization);
}
}), pausePollerThreadDelayInMs);
}
使用hystrix-metrics-event-stream
- 引入hystrix-metrics-event-stream-*.jar包
- 添加web.xml配置
<servlet>
<description></description>
<display-name>HystrixUtilizationSseServlet</display-name>
<servlet-name>HystrixUtilizationSseServlet</servlet-name>
<servlet-class>com.netflix.hystrix.contrib.sample.stream.HystrixUtilizationSseServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HystrixUtilizationSseServlet</servlet-name>
<url-pattern>/hystrix/utilization.stream</url-pattern>
</servlet-mapping>
hystrix源码小贴士之之hystrix-metrics-event-stream的更多相关文章
- hystrix源码小贴士之Servo Publisher
HystrixServoMetricsPublisher 继承HystrixMetricsPublisher,创建HystrixServoMetricsPublisherCommand.Hystrix ...
- hystrix源码小贴士之Yammer Publisher
HystrixYammerMetricsPublisher 继承HystrixMetricsPublisher,创建HystrixYammerMetricsPublisherCommand.Hystr ...
- hystrix源码小贴士之调用异常处理
executeCommandAndObserve方法处理onerror异常. return execution.doOnNext(markEmits) .doOnCompleted(markOnCom ...
- hystrix源码小贴士之中断
execution.isolation.thread.interruptOnCancel可以设置当cancellation发生时是否需要中断.通过Future的cancel方法和线程的中断方法来实现是 ...
- 【一起学源码-微服务】Hystrix 源码一:Hystrix基础原理与Demo搭建
说明 原创不易,如若转载 请标明来源! 欢迎关注本人微信公众号:壹枝花算不算浪漫 更多内容也可查看本人博客:一枝花算不算浪漫 前言 前情回顾 上一个系列文章讲解了Feign的源码,主要是Feign动态 ...
- Hystrix源码解析
1. Hystrix源码解析 1.1. @HystrixCommand原理 直接通过Aspect切面来做的 1.2. feign hystrix原理 它的本质原理就是对HystrixCommand的动 ...
- hystrix源码之概述
概述 hystrix核心原理是通过代理执行用户命令,记录命令执行的metrics信息,通过这些metrics信息进行降级和熔断. 源码结构包括一下几个部分: 熔断器 熔断器就是hystrix用来判断调 ...
- Django 源码小剖: 响应数据 response 的返回
响应数据的返回 在 WSGIHandler.__call__(self, environ, start_response) 方法调用了 WSGIHandler.get_response() 方法, 由 ...
- Django 源码小剖: 初探 WSGI
Django 源码小剖: 初探 WSGI python 作为一种脚本语言, 已经逐渐大量用于 web 后台开发中, 而基于 python 的 web 应用程序框架也越来越多, Bottle, Djan ...
随机推荐
- 你怎么改造和重新设计一个ATM银行自动取款机
- ubuntu apt 相关命令
sudo apt-get update 更新源sudo apt-get install package 安装包sudo apt-get remove package 删除包sudo apt-cach ...
- DBeaver链接kerberos安全认证的Phoenix集群
DBeaver链接kerberos安全认证的Phoenix集群 最近公司的CDH集群,启动了kerberos安全认证,所有的用户验证全部需要依赖kerberos来进行.之前的裸奔集群,总算有了一些安全 ...
- new操作符创建对象的四个步骤
new操作符创建对象可以分为以下四个步骤: 创建一个空对象 将所创建对象的__proto__属性值设为构造函数的prototype的属性值 执行构造函数中的代码,构造函数中的this指向该对象 返回对 ...
- 02.RDB持久化配置与工作流程
一.如何配置RDB持久化机制 配置文件redis.conf save 60 1000 表示每隔60s,检查如果有超过1000个key发生了变更,那么就生成一个新的dump.rdb文件,就是当前redi ...
- asterisk PBX 对接中国移动IMS
前提: 最近有项目需求,需要对接移动的IMS,移动的对接同事给出了信息: 用户名:+86750735xxxx@ims.gd.chinamobile.com 密码:123456 (系统导入的号码,默认 ...
- URL的字符编码
摘要: 在通过URL访问HTTP SERVER的时候,通常会产生trace callback的异常,返回505的错误," VERSION IS NOT SUPPORTED ?" , ...
- Python 30道高频面试题及详细解答
开学啦,开学啦!周末坐地铁的时候看到很多同学推着行李箱,拎着大包小包的穿梭在人群中,哎新的一学期又开始啦,同时也意味着很多同学要准备毕业啦,尤其是准大四,准研三的同学. 今年的招聘行情并不乐观,小公司 ...
- android开发之java代码中字符串对比忽略大小写。java程序员必回,可用来比对验证码等问题
比如:字符串a与字符串B对比,只需调用 a.equalsIgnoreCase(B);即可.很简单的一个方法.忽略大小写.
- 喵的Unity游戏开发之路 - 互动环境(有影响的运动)
如图片.视频或代码格式等显示异常,请查看原文: https://mp.weixin.qq.com/s/Sv0FOxZCAHHUQPjT8rUeNw 很多童鞋没有系统的Unity3D游戏开发基础,也不知 ...