心悦会员荣誉战场两张免做卡网站/六盘水seo
七、Hystrix断路器
7.1 简介
-
分布式系统面临的问题
复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败。多个微服务之间调用的时候,假设微服务A调用微服务B和微服务C,微服务B和微服务C又调用其它的微服务,这就是所谓的“扇出”。如果扇出的链路上某个微服务的调用响应时间过长或者不可用,对微服务A的调用就会占用越来越多的系统资源,进而引起系统崩溃,所谓的“雪崩效应”.
对于高流量的应用来说,单一的后端依赖可能会导致所有服务器上的所有资源都在几秒钟内饱和。比失败更糟糕的是,这些应用程序还可能导致服务之间的延迟增加,备份队列,线程和其他系统资源紧张,导致整个系统发生更多的级联故障。这些都表示需要对故障和延迟进行隔离和管理,以便单个依赖关系的失败,不能取消整个应用程序或系统。
所以,通常当你发现一个模块下的某个实例失败后,这时候这个模块依然还会接收流量,然后这个有问题的模块还调用了其他的模块,这样就会发生级联故障,或者叫雪崩。 -
Hystrix
在分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,Hystrix能够保证在一个依赖出问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。
“断路器”本身是一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间、不必要地占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。
- Hystrix的功能
服务降级
不让客户端等待并立刻返回一个友好提示(程序运行异常、超时、服务熔断触发服务降级、线程池/信号量打满也会导致服务降级)
服务熔断
达到最大服务访问后,直接拒绝访问,然后调用服务降级的方法并返回友好提示
服务限流
秒杀高并发等操作,严禁一窝蜂的过来拥挤,大家排队,一秒钟N个,有序进行
接近实时的监控
- Hystrix的停止维护
官网:https://github.com/Netflix/Hystrix/wiki/How-To-Use
维护停止说明: https://github.com/Netflix/Hystrix
7.2 Hystrix服务降级案例详解
新建cloud-provider-hystrix-payment8001
7.2.1 pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloud</artifactId><groupId>com.yyds</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-provider-hystrix-payment8001</artifactId><dependencies><!--hystrix--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><!--eureka-client--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies></project>
7.2.2 yml文件
server:port: 8001spring:application:name: cloud-provider-hystrix-paymenteureka:client:service-url:defaultZone: http://localhost:7001/eureka
# defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版本register-with-eureka: truefetch-registry: trueinstance:instance-id: payment8001prefer-ip-address: true # 显示ip
7.2.3 主启动类
package com.yyds.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class HystrixPaymentMain8001 {public static void main(String[] args) {SpringApplication.run(HystrixPaymentMain8001.class, args);System.out.println("*******************HystrixPaymentMain8001启动成功********************");}
}
7.2.4 业务类
package com.yyds.springcloud.controller;import com.yyds.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
@Slf4j
public class PaymentController {@Resourceprivate PaymentService paymentService;@Value("${server.port}")private String serverPort;@GetMapping("/payment/hystrix/ok/{id}")public String paymentInfo_OK(@PathVariable("id") Integer id) {String result = paymentService.paymentInfo_OK(id);log.info("****result: " + result);return result;}@GetMapping("/payment/hystrix/timeout/{id}")public String paymentInfo_TimeOut(@PathVariable("id") Integer id) throws InterruptedException {String result = paymentService.paymentInfo_TimeOut(id);log.info("****result: " + result);return result;}}
package com.yyds.springcloud.service;import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;@Service
public class PaymentService {/*** 正常访问,一切OK* */public String paymentInfo_OK(Integer id) {return "线程池:" + Thread.currentThread().getName() + "paymentInfo_OK,id: " + id + "\t" + "O(∩_∩)O";}/*** 超时访问,演示降级* */public String paymentInfo_TimeOut(Integer id) {try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace();}return "线程池:" + Thread.currentThread().getName() + "paymentInfo_TimeOut,id: " + id + "\t" + "O(∩_∩)O,耗费3秒";}}
7.2.5 测试
测试接口: http://localhost:8001/payment/hystrix/ok/1
测试接口: http://localhost:8001/payment/hystrix/timeout/1
上述测试,正常情况下是没有问题的。但是在高并发的情况下,例如:开启Jmeter,来20000个并发压死8001,20000个请求都去访问paymentInfo_TimeOut服务,两个接口都在自己转圈圈。这是因为tomcat的默认的工作线程数被打满 了,没有多余的线程来分解压力和处理。
假如此时外部的消费者80也来访问,那消费者只能干等,最终导致消费端80不满意,服务端8001直接被拖死。
因此,对于超时导致服务器变慢(转圈),超时不再等待;对于出错(宕机或程序运行出错),出错要有兜底。
具体来说:
-
对方服务(8001)超时了,调用者(80)不能一直卡死等待,必须有服务降级
-
对方服务(8001)down机了,调用者(80)不能一直卡死等待,必须有服务降级
-
对方服务(8001)OK,调用者(80)自己出故障或有自我要求(自己的等待时间小于服务提供者),自己处理降级
7.2.6 服务降级配置@HystrixCommand
cloud-provider-hystrix-payment8001设置自身调用超时时间的峰值,峰值内可以正常运行,超过了需要有兜底的方法处理,作服务降级fallback。
业务类@HystrixCommand
package com.yyds.springcloud.service;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;@Service
public class PaymentService {/*** 正常访问,一切OK* */public String paymentInfo_OK(Integer id) {return "线程池:" + Thread.currentThread().getName() + "paymentInfo_OK,id: " + id + "\t" + "O(∩_∩)O";}@HystrixCommand(fallbackMethod ="paymentInfo_TimeOutHandler",commandProperties = {@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="3000")})public String paymentInfo_TimeOut(Integer id) {int second = 5;try {TimeUnit.SECONDS.sleep(second);} catch (InterruptedException e) {e.printStackTrace();}return "线程池:"+Thread.currentThread().getName()+"paymentInfo_TimeOut,id: "+id+"\t"+"O(∩_∩)O,耗费秒: "+second;}public String paymentInfo_TimeOutHandler(Integer id){return "/(ㄒoㄒ)/调用支付接口超时或异常:\t"+ "\t当前线程池名字" + Thread.currentThread().getName();}}
主启动类添加注解@EnableCircuitBreaker
package com.yyds.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker // 添加此注解
public class HystrixPaymentMain8001 {public static void main(String[] args) {SpringApplication.run(HystrixPaymentMain8001.class, args);System.out.println("*******************HystrixPaymentMain8001启动成功********************");}}
此时访问:http://localhost:8001/payment/hystrix/timeout/1/(ㄒoㄒ)/调用支付接口超时或异常: 当前线程池名字HystrixTimer-1
7.2.7 消费者cloud-consumer-feign-hystrix-order80
新建模块cloud-consumer-feign-hystrix-order80
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloud</artifactId><groupId>com.yyds</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-consumer-feign-hystrix-order80</artifactId><dependencies><!--openfeign--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!--hystrix--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><!--eureka client--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!--web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--一般基础通用配置--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies></project>
yml文件
spring:application:name: cloud-provider-hystrix-order# 添加下面配置信息
eureka:client:service-url:defaultZone: http://localhost:7001/eureka# defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版本register-with-eureka: true # 是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡fetch-registry: truefeign:hystrix:enabled: true
主启动类
package com.yyds.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableFeignClients
@EnableHystrix // 添加此注解
public class OrderHystrixMain80
{public static void main(String[] args){SpringApplication.run(OrderHystrixMain80.class,args);}
}
业务类
package com.yyds.springcloud.service;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT")
public interface PaymentHystrixService {@GetMapping("/payment/hystrix/ok/{id}")String paymentInfo_OK(@PathVariable("id") Integer id);@GetMapping("/payment/hystrix/timeout/{id}")String paymentInfo_TimeOut(@PathVariable("id") Integer id);
}
package com.yyds.springcloud.controller;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.yyds.springcloud.service.PaymentHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
@Slf4j
public class OrderHystirxController {@Resourceprivate PaymentHystrixService paymentHystrixService;@GetMapping("/consumer/payment/hystrix/ok/{id}")public String paymentInfo_OK(@PathVariable("id") Integer id) {String result = paymentHystrixService.paymentInfo_OK(id);return result;}@GetMapping("/consumer/payment/hystrix/timeout/{id}")@HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")})public String paymentInfo_TimeOut(@PathVariable("id") Integer id){String result = paymentHystrixService.paymentInfo_TimeOut(id);return result;}public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id) {return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";}
}
测试
http://localhost/consumer/payment/hystrix/timeout/1我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o
存在的问题
-
每个业务方法对应一个兜底的方法,代码膨胀
-
统一和自定义的需要分开
7.2.8 解决问题__代码膨胀@DefaultProperties(defaultFallback = “”)
修改业务类
package com.yyds.springcloud.controller;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yyds.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystirxController
{@Resourceprivate PaymentHystrixService paymentHystrixService;@GetMapping("/consumer/payment/hystrix/ok/{id}")public String paymentInfo_OK(@PathVariable("id") Integer id){String result = paymentHystrixService.paymentInfo_OK(id);return result;}@GetMapping("/consumer/payment/hystrix/timeout/{id}")@HystrixCommand //加了@DefaultProperties属性注解,并且没有写具体方法名字,就用统一全局的public String paymentInfo_TimeOut(@PathVariable("id") Integer id){int age = 10/0;String result = paymentHystrixService.paymentInfo_TimeOut(id);return result;}public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";}// 下面是全局fallback方法public String payment_Global_FallbackMethod(){return "Global异常处理信息,请稍后再试,/(ㄒoㄒ)/~~";}
}
测试
http://localhost/consumer/payment/hystrix/timeout/1Global异常处理信息,请稍后再试,/(ㄒoㄒ)/~~
7.2.9 解决问题__和业务逻辑混一起
修改cloud-consumer-feign-hystrix-order80
package com.yyds.springcloud.service;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT", fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {@GetMapping("/payment/hystrix/ok/{id}")String paymentInfo_OK(@PathVariable("id") Integer id);@GetMapping("/payment/hystrix/timeout/{id}")String paymentInfo_TimeOut(@PathVariable("id") Integer id);}
package com.yyds.springcloud.service;import org.springframework.stereotype.Component;@Component
public class PaymentFallbackService implements PaymentHystrixService {@Overridepublic String paymentInfo_OK(Integer id) {return "paymentInfo_OK fall_back";}@Overridepublic String paymentInfo_TimeOut(Integer id) {return "paymentInfo_TimeOut fall_back";}
}
测试
http://localhost/consumer/payment/hystrix/ok/1
线程池:http-nio-8001-exec-2paymentInfo_OK,id: 1 O(∩_∩)O# 此时关闭8001服务提供者,模拟宕机
http://localhost/consumer/payment/hystrix/ok/1
paymentInfo_OK fall_back
7.3 Hystrix服务熔断案例详解
熔断机制概述
熔断机制是应对雪崩效应的一种微服务链路保护机制。当扇出链路的某个微服务出错不可用或者响应时间太长时,会进行服务的降级,进而熔断该节点微服务的调用,快速返回错误的响应信息。当检测到该节点微服务调用响应正常后,恢复调用链路。
在Spring Cloud框架里,熔断机制通过Hystrix实现。Hystrix会监控微服务间调用的状况,当失败的调用到一定阈值,缺省是5秒内20次调用失败,就会启动熔断机制。熔断机制的注解是@HystrixCommand
。
修改cloud-provider-hystrix-payment8001
PaymentService中添加此段代码
//=====================================服务熔断@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {@HystrixProperty(name = "circuitBreaker.enabled",value = "true"),// 是否开启断路器@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),// 请求次数@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), // 时间窗口期@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),// 失败率达到多少后跳闸})public String paymentCircuitBreaker(@PathVariable("id") Integer id){if(id < 0){throw new RuntimeException("******id 不能负数");}String serialNumber = IdUtil.simpleUUID();return Thread.currentThread().getName()+"\t"+"调用成功,流水号: " + serialNumber;}public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id){return "id 不能负数,请稍后再试,/(ㄒoㄒ)/~~ id: " +id;}
PaymentController中添加
@GetMapping("/payment/circuit/{id}")public String paymentCircuitBreaker(@PathVariable("id") Integer id) {String result = paymentService.paymentCircuitBreaker(id);log.info("****result: "+result);return result;}
测试
持续点击 http://localhost:8001/payment/circuit/-1
# 出现下面结果
id 不能负数,请稍后再试,/(ㄒoㄒ)/~~ id: -1# 此时访问下面地址,也不能访问
http://localhost:8001/payment/circuit/1
id 不能负数,请稍后再试,/(ㄒoㄒ)/~~ id: -1# 等一段时间,能正常访问
http://localhost:8001/payment/circuit/1
hystrix-PaymentService-10 调用成功,流水号: b1d538146c1647019fb4620bc12e648e
7.4 Hystrix工作流程
1、创建 HystrixCommand(用在依赖的服务返回单个操作结果的时候) 或 HystrixObserableCommand(用在依赖的服务返回多个操作结果的时候) 对象。
2、命令执行。其中 HystrixComand 实现了下面前两种执行方式;而 HystrixObservableCommand 实现了后两种执行方式:execute():同步执行,从依赖的服务返回一个单一的结果对象, 或是在发生错误的时候抛出异常。
queue():异步执行, 直接返回 一个Future对象, 其中包含了服务执行结束时要返回的单一结果对象。
observe():返回 Observable 对象,它代表了操作的多个结果,它是一个 Hot Obserable(不论 “事件源” 是否有 “订阅者”,都会在创建后对事件进行发布,所以对于 Hot Observable 的每一个 “订阅者” 都有可能是从 “事件源” 的中途开始的,并可能只是看到了整个操作的局部过程)。
toObservable(): 同样会返回 Observable 对象,也代表了操作的多个结果,但它返回的是一个Cold Observable(没有 “订阅者” 的时候并不会发布事件,而是进行等待,直到有 “订阅者” 之后才发布事件,所以对于 Cold Observable 的订阅者,它可以保证从一开始看到整个操作的全部过程)。
3、若当前命令的请求缓存功能是被启用的, 并且该命令缓存命中, 那么缓存的结果会立即以 Observable 对象的形式 返回。
4、检查断路器是否为打开状态。如果断路器是打开的,那么Hystrix不会执行命令,而是转接到 fallback 处理逻辑(第 8 步);如果断路器是关闭的,检查是否有可用资源来执行命令(第 5 步)。
5、线程池/请求队列/信号量是否占满。如果命令依赖服务的专有线程池和请求队列,或者信号量(不使用线程池的时候)已经被占满, 那么 Hystrix 也不会执行命令, 而是转接到 fallback 处理逻辑(第8步)。
6、Hystrix 会根据我们编写的方法来决定采取什么样的方式去请求依赖服务。HystrixCommand.run() :返回一个单一的结果,或者抛出异常。HystrixObservableCommand.construct(): 返回一个Observable 对象来发射多个结果,或通过 onError 发送错误通知。
7、Hystrix会将 “成功”、“失败”、“拒绝”、“超时” 等信息报告给断路器, 而断路器会维护一组计数器来统计这些数据。断路器会使用这些统计数据来决定是否要将断路器打开,来对某个依赖服务的请求进行 “熔断/短路”。
8、当命令执行失败的时候, Hystrix 会进入 fallback 尝试回退处理, 我们通常也称该操作为 “服务降级”。而能够引起服务降级处理的情况有下面几种:第4步: 当前命令处于"熔断/短路"状态,断路器是打开的时候。第5步: 当前命令的线程池、 请求队列或 者信号量被占满的时候。第6步:HystrixObservableCommand.construct() 或 HystrixCommand.run() 抛出异常的时候。
9、当Hystrix命令执行成功之后, 它会将处理结果直接返回或是以Observable 的形式返回。
tips:如果我们没有为命令实现降级逻辑或者在降级处理逻辑中抛出了异常, Hystrix 依然会返回一个 Observable 对象, 但是它不会发射任何结果数据, 而是通过 onError 方法通知命令立即中断请求,并通过onError()方法将引起命令失败的异常发送给调用者。
7.5 服务监控hystrixDashboard
7.5.1 案例详解
除了隔离依赖服务的调用以外,Hystrix还提供了准实时的调用监控(Hystrix Dashboard)
,Hystrix会持续地记录所有通过Hystrix发起的请求的执行信息,并以统计报表和图形的形式展示给用户,包括每秒执行多少请求多少成功,多少失败等。
Netflix通过hystrix-metrics-event-stream项目实现了对以上指标的监控。Spring Cloud也提供了Hystrix Dashboard的整合,对监控内容转化成可视化界面。
新建cloud-consumer-hystrix-dashboard9001
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloud</artifactId><groupId>com.yyds</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-consumer-hystrix-dashboard9001</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies></project>
yml文件
server:port: 9001
主启动类
package com.yyds.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;@SpringBootApplication
@EnableHystrixDashboard // 添加此注解
public class HystrixDashboardMain9001
{public static void main(String[] args) {SpringApplication.run(HystrixDashboardMain9001.class, args);}
}
访问: http://localhost:9001/hystrix
使用cloud-consumer-hystrix-dashboard9001监控cloud-provider-hystrix-payment8001
修改cloud-provider-hystrix-payment8001
package com.yyds.springcloud;import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class HystrixPaymentMain8001 {public static void main(String[] args) {SpringApplication.run(HystrixPaymentMain8001.class, args);System.out.println("*******************HystrixPaymentMain8001启动成功********************");}/***此配置是为了服务监控而配置,与服务容错本身无关,springcloud升级后的坑*ServletRegistrationBean因为springboot的默认路径不是"/hystrix.stream",*只要在自己的项目里配置上下面的servlet就可以了*/@Beanpublic ServletRegistrationBean getServlet() {HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);registrationBean.setLoadOnStartup(1);registrationBean.addUrlMappings("/hystrix.stream");registrationBean.setName("HystrixMetricsStreamServlet");return registrationBean;}}
填写监控地址
http://localhost:8001/hystrix.stream
测试
不断访问http://localhost:8001/payment/circuit/1
不断访问http://localhost:8001/payment/circuit/-1
7.5.2 Hystrix Dashboard参数介绍
Hystrix Dashboard面板可分上下两部分来查询,上面部分是断路器方法调用的相关信息,Circuit,下面部分是Hystrix为断路器方法提供的线程池相关信息,Thread Pools。
在图表中,左上角的圆圈代表了该方法的流量和状态:
- 圆圈越大代表方法流量越大
- 圆圈为绿色代表断路器健康、黄色代表断路器偶发故障、红色代表断路器故障
右上角的计数器(三列数字):
第一列从上到下
- 绿色代表当前成功调用的数量
- 蓝色代表短路请求的数量
- 蓝绿色代表错误请求的数量
第二列从上到下
- 黄色代表超时请求的数量
- 紫色代表线程池拒绝的数量
- 红色代表失败请求的数量
第三列
- 过去10s的错误请求百分比
Thread Pools:
Hystrix会针对一个受保护的类创建一个对应的线程池,这样做的目的是Hystrix的命令被调用的时候,不会受方法请求线程的影响(或者说Hystrix的工作线程和调用者线程相互之间不影响)
在图表中,左上角的圆圈代表了该线程池的流量和状态:
- 圆圈越大代表线程池越活越,流量越大
- 圆圈颜色代表的是线程池的健康状况
左下角从上至下:
- Active代表线程池中活跃线程的数量
- Queued代表排队的线程数量,该功能默认禁止,因此默认情况下始终为0
- Pool Size代表线程池中线程的数量
右下角从上至下:
- Max Active代表最大活跃线程,这里展示的数据是当前采用周期中,活跃线程的最大值
- Execcutions代表线程池中线程被调用执行Hystrix命令的次数
- Queue Size代表线程池队列的大小,默认禁用,无意义
相关文章:

SpringCloud(三)Hystrix断路器服务降级、服务熔断、服务监控案例详解
七、Hystrix断路器 7.1 简介 分布式系统面临的问题 复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败。 多个微服务之间调用的时候,假设微服务A调用微服务B和微服务C,微服务B和微服务C又调用其它的微…...

【超好用】自定义的mybatis-plus代码生成器
BACKGROUND你是否也有这样的烦恼:每次写代码都需要创建很多包很多层很多类很多接口?耗时且费力姑且不谈,有时可能还大意了没有闪,搞出一堆bug这谁顶得住啊都3202年了,让程序自力更生吧!!教程 le…...

Kubernetes学习笔记-计算资源管理(4)监控pod的资源使用量20230219
前面学了设置资源的requests和limits,这节课学习如何监控资源,根据监控资源使用情况,对requests和limits进行合理配置。收集、获取实际资源使用情况kubelet包含一个agent,名为cAdvisor,它会收集整个节点上运行的所有单…...

游戏开发 - 开发流程 - 收集
1.应用场景 主要用于了解,掌握游戏开发的整个流程。 2.学习/操作 1.文档阅读 复习课 | 带你梳理客户端开发的三个重点-极客时间 2.整理输出 2.1 游戏开发流程 -- 参考 按照游戏开发中的三大模块策划、程序、美术,画了一个图。 开发游戏的时候ÿ…...

LA@向量空间@坐标变换
文章目录向量空间向量空间的属性坐标例基变换坐标变换n维向量空间RnR^nRn子空间例线性组合与线性方程组生成子空间深度学习向量空间 设VVV是n维向量的非空集合,如果VVV对向量的加法和数乘运算封闭,即 ∀α,β∈V,∀k∈Rαβ,kα∈V\forall \alpha,\beta\in{V},\forall k\in{\ma…...

JSP脚本指令及标记学习笔记
好久没更新文章了,上次更新的文章还是一个学习笔记。本篇博文介绍的是JSP基本概念 1.JSP开发方法 一个jsp网页只需要加上<%%>就行了。 2.JSP运行机制 3.JSP脚本元素 3.1 JSP脚本代码 <% 脚本代码 %>实例 <% SimpleDateFormat df new SimpleDa…...

【C语言每日一题】——猜凶手
【C语言每日一题】——猜名次😎前言🙌猜凶手🙌解题思路分享:😍解题源码分享:😍总结撒花💞😎博客昵称:博客小梦 😊最喜欢的座右铭:全神…...

2019蓝桥杯真题完全二叉树的权值 C语言/C++
题目描述 给定一棵包含 N个节点的完全二叉树,树上每个节点都有一个权值,按从 上到下、从左到右的顺序依次是 A_1, A_2, A_N,如下图所示: 现在小明要把相同深度的节点的权值加在一起,他想知道哪个深度的节点 权值之和最…...

大数据之Phoenix环境搭建
文章目录前言一、下载Phoenix安装包二、上传并解压三、拷贝服务包到各个hbase的lib目录下四、修改hbase的配置文件五、重启HBase集群六、连接Phoenix客户端前言 #博学谷IT学习技术支持# 本篇文章主要介绍Phoenix的环境搭建,Phoenix支持使用SQL语句操作HBase&#x…...

62 一次 Promotion failed 的调试
前言 最近 有一个想法就是, 调试一下 DefNewGeneration 里面的晋升失败的情况 呵呵 对于这块的代码上面, 看着感觉有一些疑问的地方, 因此想通过 实际的调试, 来验证一下 实际的情况 然后 之前写了一个用例, 但是 和心中的期望差距甚大, 当然 主要的问题 还是自己对于 细…...

Git的基本操作
文章目录1.git的工作流程2.git的工作环境3.git的基本操作(1)git init(2)git status(3)git add(4)git commit4.版本控制(1)git reflog与git log(2)再增加两个版本(3)git reset --hard 版本号(4)两个指针4.分支管理(1)对分支的理解(2)git branch和git branch -v(3)git checkout 分…...

LeetCode初级算法题:两数之和+斐波拉契数列多种java解法
目录7 两数之和题目描述:解题思路与代码暴力解法:解法一:二分查找解法二:双指针2 斐波那契数列题目描述:解题思路与代码解法一&…...

测试1:测试相关概念
1.测试相关概念 1.1.测试概念 1.1.1.需求 符合正式文档规定的条件和权能,包括用户需求和软件需求 它们之间的的转换是:沟通 用户需求和软件需求的区别: 能否指导开发人员开发,测试人员编写测试用例 1.1.2.缺陷Bug 与正确的…...

2.19 索引和事务
一.联合查询面试问题:聚合查询与联合查询的区别聚合查询是行与行之间的数据加工聚合函数 :count,sum,avg...group by 进行分组,指定列的值,相同的记录合并到同一个组,每个组又可以分别进行聚合查询分组还可以指定条件筛选,如果分组之前指定条件 用where,如果对分组之后指定条件…...

算法导论【摊还分析】—聚合分析、核算法、势能法
算法导论【摊还分析】—聚合分析、核算法、势能法聚合分析核算法势能法假定我们对一个数据结构执行一个由 n 个操作组成的操作序列,当 i 严格为 2 的幂时,第 i 个操作的代价为 i,否则代价为 1 聚合分析 总共有n个操作,1,2,4.....…...

【LeetCode】剑指 Offer 08. 二叉树的下一个节点 p65 -- Java Version
题目链接:无题目链接,不知道为啥力扣上找不到这一题。 1. 题目介绍(08. 二叉树的下一个节点) 题目:给定一个二叉树和其中的一个节点,请找出中序遍历顺序的下一个节点并且返回。注意,树中的节点…...

Python 之 Pandas Series 数据结构
文章目录一、Series 结构二、数据结构 Series 创建1. 创建1.1 列表/数组作为数据源创建 Series1.2 字典作为数据源创建 Series1.3 通过标量创建2. 参数说明2.1 index 参数2.2 name 参数2.3 copy 参数三、Series 的索引/切片1. 下标索引2. 标签索引3. 切片四、Series 数据结构的…...

【java基础】Java常用类———包装类
包装类 wrapper 装箱与拆箱 装箱:基本类型->包装类; 拆箱: 包装类->基本类型 public class Integer01 {public static void main(String[] args) {//演示int <--> Integer 的装箱和拆箱//jdk5前是手动装箱和拆箱//手动装箱 in…...

linux shell 入门学习笔记3 shebang
shebang 计算机程序中,shebang指的是出现在文本文件的第一行前两个字符#! 在Unix系统中,程序会分析shebang后面的内容,作为解释器的指令,例如 以#!/bin/sh 开头的文件,程序在执行的时候会调用/bin/sh,也就…...

写作小课堂:简历模版【A4纸正反两面】(20230219)
文章目录 I 联系方式II 个人信息III 求职意向IV 工作经验2018年-11月-至今全城淘信息技术服务有限公司2017年07月-2018年-11月湖南微流网络科技有限公司2014年06月-2017年07月湖南高阳通联信息技术有限公司V 项目经验2018年11月-至今全城淘淘管家2017年10月-2018年11月ASO(机刷…...

一文搞懂 DevOps
前言 DevOps作为一个热门的概念,近年来频频出现在各大技术社区和媒体的文章中,备受行业大咖的追捧,也吸引了很多吃瓜群众的围观。 那么,DevOps是什么呢? 有人说它是一种方法,也有人说它是一种工具&#…...

深入讲解Kubernetes架构-租约
分布式系统通常需要租约(Lease);租约提供了一种机制来锁定共享资源并协调集合成员之间的活动。 在 Kubernetes 中,租约概念表示为 coordination.k8s.io API 组中的 Lease 对象, 常用于类似节点心跳和组件级领导者选举等…...

微信小程序学习第11天——Vant Weapp组件库、API Promise化、全局数据共享Mobx、分包
目录一、小程序对npm 的限制二、使用Vant Weapp组件库1、安装组件2、使用组件3、定制全局样式三、API Promise化1、下载miniprogram-api-promise2、引入3、使用四、全局数据共享五、分包1、分包概念2、使用分包3、独立分包4、分包预下载一、小程序对npm 的限制 在小程序中使用…...

Python3-基本数据类型
Python3 基本数据类型 Python 中的变量不需要声明。每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。 在 Python 中,变量就是变量,它没有类型,我们所说的"类型"是变量所指的内存中对象的类型。 等号&…...

RPA落地指南:什么是RPA
什么是RPA RPA在企业中起什么作用并扮演什么角色呢?想要充分了解RPA,我们需要知道RPA的相关概念、特点、功能以及能解决的问题。接下来对这些内容进行详细介绍。 1.1 RPA的3个核心概念 RPA的中文译名是“机器人流程自动化”,顾名思义&…...

跨域问题的三种解决办法
我们平时对于前后端联调的项目,以下的错误是经常常见的,我们查看浏览器报错: Access to XMLHttpRequest at http://localhost:63110/system/dictionary/all fromorigin http://localhost:8601 has been blocked by CORS policy: No Access…...

c++提高篇——string容器
一、string基本概念 string是C风格的字符串,而string本质上是一个类。 与c语言不同,string是一个类,类内部封装了char*,管理这个字符串,是一个char型的容器。在根本上与c语言字符串是一致的。 在string类内部封装了很…...

[软件工程导论(第六版)]第6章 详细设计(复习笔记)
文章目录6.1 结构程序设计6.2 人机界面设计6.3 过程设计的工具6.3.1 程序流程图(程序框图)6.3.2 盒图(N-S图)6.3.3 PAD图(问题分析图)6.3.4 判定表6.3.5 判断树6.3.6 过程设计语言6.4 面向数据结构的设计方…...

RabbitMQ核心内容:实战教程(java)
文章目录一、安装二、入门1.分类2.核心概念3.工作原理4.六大模式三、模式一:"Hello World!"1.依赖2.生产者代码3.消费者代码四、模式二:Work Queues1.工作原理2.工具类代码:连接工厂3.消费者代码4.生产者代码5.分发策略不公平分发预…...

RK356x U-Boot研究所(命令篇)3.7 pci与nvme命令的用法
平台U-Boot 版本Linux SDK 版本RK356x2017.09v1.2.3文章目录 一、设备树与config配置二、pci命令的定义三、nvme命令的定义四、pci与nvme命令的用法3.1 pci总线扫描3.2 nvme设备信息3.3 nvme设备读写一、设备树与config配置 RK3568支持PCIe接口,例如ROC-RK3568-PC: 原理图如…...