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的更多相关文章

  1. hystrix源码小贴士之Servo Publisher

    HystrixServoMetricsPublisher 继承HystrixMetricsPublisher,创建HystrixServoMetricsPublisherCommand.Hystrix ...

  2. hystrix源码小贴士之Yammer Publisher

    HystrixYammerMetricsPublisher 继承HystrixMetricsPublisher,创建HystrixYammerMetricsPublisherCommand.Hystr ...

  3. hystrix源码小贴士之调用异常处理

    executeCommandAndObserve方法处理onerror异常. return execution.doOnNext(markEmits) .doOnCompleted(markOnCom ...

  4. hystrix源码小贴士之中断

    execution.isolation.thread.interruptOnCancel可以设置当cancellation发生时是否需要中断.通过Future的cancel方法和线程的中断方法来实现是 ...

  5. 【一起学源码-微服务】Hystrix 源码一:Hystrix基础原理与Demo搭建

    说明 原创不易,如若转载 请标明来源! 欢迎关注本人微信公众号:壹枝花算不算浪漫 更多内容也可查看本人博客:一枝花算不算浪漫 前言 前情回顾 上一个系列文章讲解了Feign的源码,主要是Feign动态 ...

  6. Hystrix源码解析

    1. Hystrix源码解析 1.1. @HystrixCommand原理 直接通过Aspect切面来做的 1.2. feign hystrix原理 它的本质原理就是对HystrixCommand的动 ...

  7. hystrix源码之概述

    概述 hystrix核心原理是通过代理执行用户命令,记录命令执行的metrics信息,通过这些metrics信息进行降级和熔断. 源码结构包括一下几个部分: 熔断器 熔断器就是hystrix用来判断调 ...

  8. Django 源码小剖: 响应数据 response 的返回

    响应数据的返回 在 WSGIHandler.__call__(self, environ, start_response) 方法调用了 WSGIHandler.get_response() 方法, 由 ...

  9. Django 源码小剖: 初探 WSGI

    Django 源码小剖: 初探 WSGI python 作为一种脚本语言, 已经逐渐大量用于 web 后台开发中, 而基于 python 的 web 应用程序框架也越来越多, Bottle, Djan ...

随机推荐

  1. unity探索者之微信分享所有流程,非第三方插件

    版权声明:本文为原创文章,转载请声明http://www.cnblogs.com/unityExplorer/p/7560575.html 很久没有写新博客了,前段时间有些忙,这几天趟了几个微信分享的 ...

  2. SSM框架整合练习——一个简单的文章管理系统

    使用SSM框架搭建的简易文章管理系统,实现了简单的增删改查功能. @ 目录 开发工具版本: 最终的项目结构 IDEA+Maven搭建项目骨架 1. 新建Maven项目: 2. 在新建的项目中添加所需要 ...

  3. CentOS7 安装图形化桌面

    1.装好CentOS7后,我们一开始是上不了网的 (ping 百度报错:Name or service not know) 2.输入dhclient,可以自动获取一个IP地址,再用命令ip addr查 ...

  4. keil5 使用JLink 向nrf52840DK 下载程序出现No Cortex-M SW Device Found

    今天打开52840 keil5 工程,下载程序无法下载成功,提示如下: 在Jlink 配置中(Option for target....-> Debug->Setting )SWDIO也无 ...

  5. python 用 prettytable 输出漂亮的表格

    原文链接:https://linuxops.org/blog/python/prettytable.html #!/usr/bin/python #**coding:utf-** import sys ...

  6. wsgi的environ变量

    The environ dictionary is required to contain these CGI environment variables, as defined by the Com ...

  7. Java动态代理(二)——jdk动态代理

    一.什么是动态代理?代理类在程序运行时创建的代理方式被成为动态代理.动态代理的代理类并不是在Java代码中定义的,而是在运行时根据我们在Java代码中的“指示”动态生成的.相比于静态代理, 动态代理的 ...

  8. python数据类型和运算符

    一.python类型判断 type,isinstance type(变量或常量):返回数据类型 a = 23.3print(type(a))b = 2e3print(b, type(b))输出: &l ...

  9. akka-grpc - 应用案例

    上期说道:http/2还属于一种不算普及的技术协议,可能目前只适合用于内部系统集成,现在开始大面积介入可能为时尚早.不过有些项目需求不等人,需要使用这项技术,所以研究了一下akka-grpc,写了一篇 ...

  10. 滴滴Ceph分布式存储系统优化之锁优化

    桔妹导读:Ceph是国际知名的开源分布式存储系统,在工业界和学术界都有着重要的影响.Ceph的架构和算法设计发表在国际系统领域顶级会议OSDI.SOSP.SC等上.Ceph社区得到Red Hat.SU ...