java熔断、降级、hystrix监控

一、hystrix

https://blog.csdn.net/zjcsuct/article/details/78198632

二、实现方式

1.通过注解实现

2.AOP实现

3.继承方式实现

三、hystrix监控

1.单机监控

修改项目配置

1、pom.xml
 <dependency>
    <groupId>com.netflix.hystrix</groupId>
     <artifactId>hystrix-core</artifactId>
     <version>1.4.10</version>
 </dependency> 
 <dependency>
     <groupId>com.netflix.hystrix</groupId>
      <artifactId>hystrix-metrics-event-stream</artifactId>
     <version>1.4.10</version>
</dependency>

说明:
hystrix-core:hystrix核心接口包
hystrix-metrics-event-stream:只要客户端连接还连着,hystrix-metrics-event-stream就会不断的向客户端以text/event-stream的形式推送计数结果(metrics)

2.在web.xml添加如下配置
<servlet>
        <display-name>HystrixMetricsStreamServlet</display-name>
        <servlet-name>HystrixMetricsStreamServlet</servlet-name>
        <servlet-class>com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HystrixMetricsStreamServlet</servlet-name>
        <url-pattern>/hystrix.stream</url-pattern>
    </servlet-mapping>

下载hysttrix-dashboard

方式一:下载源码包
地址:https://github.com/kennedyoliveira/standalone-hystrix-dashboard

git clone https://github.com/kennedyoliveira/standalone-hystrix-dashboard.git
cd standalone-hystrix-dashboard

方式二:直接下载打好的jar包
因方式一下载后打包有莫名其妙的问题
地址:https://bintray.com/kennedyoliveira/maven/standalone-hystrix-dashboard/1.5.6
在页面最下面有个Downloads标签
这里写图片描述
下载 standalone-hystrix-dashboard-1.5.6-all.jar即可

部署hystrix-dashboard

1.如果用方式一的下载方式,需要编译进入目录编译

./gradlew runDashboard

2.如果用方式一的下载方式在C:\xxx\xxx\standalone-hystrix-dashboard-1.5.6\standalone-hystrix-dashboard-1.5.6\build\libs目录下找到打好的jar包
3.将方式一下载打好的jar包或方式二下载的jar包上传到服务器
4.使用如下命令启动

java -jar standalone-hystrix-dashboard-{VERSION}-all.jar

5.访问
默认端口为7979

访问接口查看监控指标

1.通过stream查看指标
http://127.0.0.1:8180/hystrix.stream
这里写图片描述
上面看到的数据不直观,我们通过standalone-hystrix-dashboard提供的界面看下
这里写图片描述
填写项目地址http://127.0.0.1:8180/hystrix.stream
点击:AddStream
点击:monitor stream
这里写图片描述

监控界面参数说明
这里写图片描述

2.集群监控

再生产环境下,一般都是部署的集群,但是按照上面单机监控的思路去解决的话,很多指标都是针对单台机器的,其实更多情况下我们不关注但台机器指标,而是看hystrx相关指标是否正常,所以需要一个聚合各机器指标的地方,这个项目就是turbine。集群hystrix指标监控的实现思路是
hysttrix-dashboard—–>turbine-web(集合监控数据)——->hystrix.stream

下载turbine-web

下载地址
http://search.maven.org/remotecontent?filepath=com/netflix/turbine/turbine-web/1.0.0/turbine-web-1.0.0.war

启动你要监控的项目集群

将要监控的项目集群启动

解压turbine-web

下载下来的是一个war工程,可以直接修改后缀为.zip使用解压软件解压,将解压后的工程放到tomcat中的webapps目录下。

配置聚合信息

进入WEB-INF\classes目录,打开config.properties
该文件中会配置:
1. Turbine在监控哪些集群:turbine.aggregator.clusterConfig=cluster-1,cluster-2
2. Turbine怎样获取到节点的监控信息(hystrix.stream):turbine.instanceUrlSuffix. = :/HystrixDemo/hystrix.stream
3. 集群下有哪些节点:turbine.ConfigPropertyBasedDiscovery.cluster-1.instances=localhost:8080,localhost:8081
上面这些都是最简单的配置方法 Turbine使用了Netflix的另一个开源项目Archaius(https://github.com/Netflix/archaius)来做配置文件的管理,其提供了非常强大的配置文件管理策略,有需要的同学可以深入研究(https://github.com/Netflix/Turbine/wiki/Configuration)。

启动turbine

集群监控方式

这里写图片描述
这样点击monitorStream就会展示cluster-1集群机器的汇总数据

说明:注意turbine的配置

猜你喜欢

转载自blog.csdn.net/forwujinwei/article/details/81027245