1:SpringCloud中应用:
1.1:退出依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId></dependency>
1.2:启动类上增加
@SpringBootApplication@EnableHystrixDashboard@EnableCircuitBreaker//@EnableHystrix
1.3:Controller上配置
在Controller的办法上增加Hystrix的配置,形如:
@HystrixCommand(fallbackMethod = "error", commandProperties = { @HystrixProperty(name="execution.isolation.strategy", value = "THREAD"), @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "4000"), @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = ”50") }, threadPoolProperties = { @HystrixProperty(name = "coreSize", value = "1"), @HystrixProperty(name = "maxQueueSize", value = "10"), @HystrixProperty(name = "keepAliveTimeMinutes", value = "1000"), @HystrixProperty(name = "queueSizeRejectionThreshold", value = "8"), @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"), @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1500") })
Hystrix反对两种形式定义HystrixCommand,一种是将类继承自HystrixCommand类,重写run办法,另一种是在办法头上写注解的形式,应用注解的形式代码会比拟清晰,将Hystrix代码和业务代码隔离开
2: DashBoard
2.1: 概述
Hystrix自带了DashBoard,如果监控单个实例,能够很不便的通过Hystrix的dashboard进行查看运行状况,间接进入http://localhost:8080/hystrix
Hystrix Dashboard共反对三种不同的监控形式,顺次为:
- 1:默认的集群监控:通过URLhttp://turbine-hostname:port/turbine.stream开启,实现对默认集群的监控。
- 2:指定的集群监控:通过URLhttp://turbine-hostname:port/turbine.stream?cluster=[clusterName]开启,实现对clusterName集群的监控。
- 3:单体利用的监控:通过URLhttp://hystrix-app:port/hystrix.stream开启,实现对具体某个服务实例的监控。
前两者都对集群的监控,须要整合Turbine能力实现
2.2: 示例如下
- Delay:该参数用来管制服务器上轮询监控信息的延迟时间,默认为2000毫秒,能够通过配置该属性来升高客户端的网络和CPU耗费。
- Title:该参数对应了头部题目Hystrix Stream之后的内容,默认会应用具体监控实例的URL,能够通过配置该信息来展现更适合的题目。
- 监控信息的左上局部找到两个重要的图形信息:一个实心圆和一条曲线。
(1)实心圆:共有两种含意。它通过色彩的变动代表了实例的衰弱水平,它的衰弱度从绿色、黄色、橙色、红色递加。该实心圆除了色彩的变动之外,它的大小也会依据实例的申请流量发生变化,流量越大该实心圆就越大。所以通过该实心圆的展现,就能够在大量的实例中疾速的发现故障实例和高压力实例。
(2)曲线:用来记录2分钟内流量的绝对变动,能够通过它来察看到流量的回升和降落趋势。