SpringCloud——中级(一)Hystrix断路器
作者:互联网
Hystrix断路器
一、概述
1、分布式系统面临的问题
复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败
服务雪崩
多个微服务之间调用的时候,假设微服务A调用微服务B和微服务C,微服务B和微服务C又调用其他的微服务,这就是所谓的“扇出”。
如果扇出的链路上某个微服务的调用响应时间过长或者不可用,对微服务A的调用就会占用越来越多的系统资源,进而引起系统崩溃,即“雪崩效应”
2、是什么
Hystrix是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,Hystrix能够保证在一个依赖出现问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。
“断路器”本身是一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间、不必要地占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。
3、能干嘛
1)服务降级
2)服务熔断
3)接近实时的监控
4)限流、隔离等
4、官网资料
5、Hystrix官宣,停更进维
二、Hystrix重要概念
1、服务降级(fallback)
对方的系统不可用了,你需要给我一个兜底的解决方法
)哪些情况会触发降级
1、程序运行异常
2、超时
3、服务熔断触发服务降级
4、线程池/信号量打满也会导致服务降级
2、服务熔断(break)
类比保险丝达到最大服务访问后,直接拒绝访问,拉闸限电,然后调用服务降级的发那个发并返回友好提示
3、服务限流(flowlimit)
秒杀高并发等操作,严禁一窝蜂的过来拥挤,大家排队,一秒钟n个,有序进行
三、hystrix案例
1、构建
1)新建cloud-provider-hystrix-payment8001
2)pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
3)yml
server:
port: 8001
spring:
application:
name: cloud-provider-hystrix-payment
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
4)主启动类
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class,args);
}
}
5)业务类
service:
package com.atguigu.springcloud.service;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class PaymentService {
/*
* 正常访问
* */
public String paymentInfo_OK(Integer id){
return "线程池: "+Thread.currentThread().getName()+" paymentInfo_OK,id: "+id+"\t "+"O(∩_∩)O哈哈~";
}
public String paymentInfo_TimeOut(Integer id){
int timeNumber=3;
try{
TimeUnit.SECONDS.sleep(timeNumber);
}catch (InterruptedException e){
e.printStackTrace();
}
return "线程池: "+Thread.currentThread().getName()+" paymentInfo_TimeOut,id: "+id+"\t "+"O(∩_∩)O哈哈~ 耗时(秒)"+timeNumber;
}
}
Controller:
package com.atguigu.springcloud.controller;
import com.atguigu.springcloud.service.PaymentService;
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 PaymentController {
@Resource
private 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_Out(@PathVariable("id") Integer id){
String result=paymentService.paymentInfo_TimeOut(id);
log.info("******result: "+result);
return result;
}
}
6)正常测试
http://localhost:8001/payment/hystrix/ok/35
http://localhost:8001/payment/hystrix/timeout/35
上述module均OK
以上述为根基平台,从正确->错误->降级熔断->恢复
2、高并发测试
上述在非高并发情形下进行,还能勉强满足,下面演示高并发情况
1)Jmeter压测测试
开启Jmeter,来20000个高并发压死8001,20000个请求都去访问paymentInfo_TimeOut服务
再来一个访问 http://localhost:8001/payment/hystrix/ok/35
看演示结果:
两个都在转圈圈
原因:tomcat的默认的工作线程数被打满了,没有多余的线程来分解压力和处理
2)Jmeter压测结论
上面还是服务提供者8001自己测试,假如此时外部的消费者80也来访问,那消费者只能干等,最终导致消费端80不满意,服务端8001直接被拖死。
3)80新建加入
1>新建cloud-consumer-feign-hystrix-order80
2>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>cloud2020</artifactId>
<groupId>com.atguigu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-feign-hystrix-order80</artifactId>
<dependencies>
<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>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
3>yml
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
4>主启动类
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class OrderHystrixMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderHystrixMain80.class,args);
}
}
5>业务类
Service:
package com.atguigu.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("CLOUD-PROVIDER-HYSTRIX-PAYMENT")
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_Out(@PathVariable("id") Integer id);
}
Controller:
package com.atguigu.springcloud.controller;
import com.atguigu.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 {
@Resource
private 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}")
public String paymentInfo_Out(@PathVariable("id") Integer id){
String result=paymentHystrixService.paymentInfo_Out(id);
return result;
}
}
6>正常测试
http://localhost/consumer/payment/hystrix/ok/35
7>高并发测试
20000个线程压8001
消费端80微服务再去访问正常的OK微服务8001地址
http://localhost/consumer/payment/hystrix/ok/35
消费者80:要么转 圈圈,偶尔会消费端报超时错误
3、故障现象和导致原因
8001同一层次的其它接口服务被困死,因为tomcat线程池里面的工作线程已经被挤占完毕
80此时调用8001,客户端访问响应缓慢,转圈圈。
4、上诉结论
正因为有上述故障或不佳表现,才有我们的降级/容错/限流等技术的诞生
5、如何解决?解决的要求
1)超时导致服务器变慢(转圈)
超时不再等待
2)出错(宕机或程序运行出错)
出错要有兜底
3)解决
对方服务(8001)超时了,调用者(80)不能一直卡死等待,必须有服务降级。
对方服务(8001)down机了,调用者(80)不能一直卡死等待,必须要有服务降级。
对方服务(8001)OK,调用者(80)自己出故障或有自我要求(自己的等待时间小于服务提供者)自己处理降级。
6、服务降级
1)降级配置
官网
使用注解:
@HystrixCommand
2)8001先从自身找问题
设置自身调用超时时间的峰值,峰值内可以正常运行,超过了需要有兜底的方法处理,作服务降级fallback
3)800fallback
业务类(使用@HystrixCommand注解):
一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法
package com.atguigu.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 {
/*
* 正常访问
* */
public String paymentInfo_OK(Integer id){
return "线程池: "+Thread.currentThread().getName()+" paymentInfo_OK,id: "+id+"\t "+"O(∩_∩)O哈哈~";
}
@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
//规定该线程的超时时间为3秒
//3秒以内走自己的逻辑,出错后运行paymentInfo_TimeOutHandler方法
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})
public String paymentInfo_TimeOut(Integer id){
int timeNumber=3;
//int age=10/0;
try{
TimeUnit.SECONDS.sleep(timeNumber);
}catch (InterruptedException e){
e.printStackTrace();
}
return "线程池: "+Thread.currentThread().getName()+" paymentInfo_TimeOut,id: "+id+"\t "+"O(∩_∩)O哈哈~ 耗时(秒)"+timeNumber;
}
//兜底的类
public String paymentInfo_TimeOutHandler(Integer id){
return "线程池: "+Thread.currentThread().getName()+" paymentInfo_TimeOut,id: "+id+"\t "+"o(╥﹏╥)o啊啊";
}
}
主启动类激活:
添加新注解@EnableCircuitBreaker
4)80fallback
同上,进行客户端的降级保护
PS:我们自己配置过的热部署方式对java代码的改动明显,但对@HystrixCommand内属性的修改建议重启微服务
YML:
feign:
hystrix:
enabled: true
主启动类:
@EnableHystrix
业务类(Controller):
package com.atguigu.springcloud.controller;
import com.atguigu.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
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 {
@Resource
private 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_Out(@PathVariable("id") Integer id){
String result=paymentHystrixService.paymentInfo_Out(id);
return result;
}
public String paymentTimeOutFallbackMethod(Integer id){
return "我是消费者80,对方支付系统繁忙,请稍后再试!!!";
}
}
测试:http://localhost/consumer/payment/hystrix/timeout/35
5)目前问题
1、每个业务方法对应一个兜底的方法,代码膨胀
Controller:
package com.atguigu.springcloud.controller;
import com.atguigu.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
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 {
@Resource
private 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")
// })
@HystrixCommand
public String paymentInfo_Out(@PathVariable("id") Integer id){
String result=paymentHystrixService.paymentInfo_Out(id);
return result;
}
public String paymentTimeOutFallbackMethod(Integer id){
return "我是消费者80,对方支付系统繁忙,请稍后再试!!!";
}
//全局fallback方法
public String payment_Global_FallbackMethod(){
return "我是消费者80全局默认服务降级方法,对方支付系统繁忙,请稍后再试!!!";
}
}
2、和业务逻辑混在一起,代码混乱
6)解决问题
1、代码膨胀
fiegn接口系列
@DefaultProperties(defaultFallback="")
设施默认的降级方法,通用的和独享的各自分开
1、代码混乱(耦合度高)
本次案例服务降级处理是在客户端80实现完成的,与服务端8001没有关系,只需要为Feign客户端定义的接口添加一个服务降级处理的实现类即可实现解耦
未来我们要面对的异常:
运行、超时、宕机
混合在一块,每个方法都要提供一个
解决:
根据cloud-consumer-feign-hystrix-order80已经偶的PaymentHystrixService接口,
重新建一个类(paymentFallbackService)实现该接口,统一为接口里面的方法进行异常处理
接口:
package com.atguigu.springcloud.service;
import org.springframework.stereotype.Component;
@Component
public class paymentFallbackService implements PaymentHystrixService {
@Override
public String paymentInfo_OK(Integer id) {
return "-------------paymentFallbackService fall back paymentInfo_OK,宕机";
}
@Override
public String paymentInfo_Out(Integer id) {
return "-------------paymentFallbackService fall back paymentInfo_Out,宕机";
}
}
修改接口(注解):
@FeignClient(value="CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback = paymentFallbackService.class)
测试:
正常访问:http://localhost:8001/payment/hystrix/ok/35
故意关闭微服务8001
测试例图:
7、服务熔断
1)断路器
就是家里的保险丝
2)熔断是什么
熔断机制概述:
熔断机制是应对雪崩效应的一种微服务保护机制。当删除链路的某个微服务出错不可用或者响应时间太长时,会进行服务的降级,进而熔断该节点微服务的调用,快速返回错误的响应信息。
当检测到该节点微服务调用响应正常后,恢复调用链路。
在Spring Cloud框架里,熔断机制通过Hystrix实现。Hystrix会监控微服务间调用的状况,
当失败的调用到一定阈值,缺省是5秒内调用失败,就会启动熔断机制。熔断机制的注解是@HystrixCommand
大神论文:
马丁福勒论文
3)实操
1>修改cloud-provider-hystrix-payment8001
2>PaymentService
package com.atguigu.springcloud.service;
import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.concurrent.TimeUnit;
@Service
public class PaymentService {
/*
* 正常访问
* */
public String paymentInfo_OK(Integer id){
return "线程池: "+Thread.currentThread().getName()+" paymentInfo_OK,id: "+id+"\t "+"O(∩_∩)O哈哈~";
}
@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
//规定该线程的超时时间为3秒
//3秒以内走自己的逻辑,出错后运行paymentInfo_TimeOutHandler方法
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "5000")
})
public String paymentInfo_TimeOut(Integer id){
int timeNumber=3;
//int age=10/0;
try{
TimeUnit.SECONDS.sleep(timeNumber);
}catch (InterruptedException e){
e.printStackTrace();
}
return "线程池: "+Thread.currentThread().getName()+" paymentInfo_TimeOut,id: "+id+"\t "+"O(∩_∩)O哈哈~ 耗时(秒)"+timeNumber;
}
//兜底的类
public String paymentInfo_TimeOutHandler(Integer id){
return "线程池: "+Thread.currentThread().getName()+" paymentInfo_TimeOut,id: "+id+"\t "+"o(╥﹏╥)o啊啊超时了超时了!!!";
}
//******服务熔断
@HystrixCommand(fallbackMethod = "PaymentCircuitBreaker_fallback",commandProperties = {
@HystrixProperty(name="circuitBreaker.enable",value = "true"),//是否开启断路器
//十秒钟,请求十次,失败率超过60%后跳闸
@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();//等价于UUID.randomUUID().toString()
return Thread.currentThread().getName()+"\t"+"调用成功,流水号: "+serialNumber;
}
public String PaymentCircuitBreaker_fallback(@PathVariable("id") Integer id){
return "id 不能为负数,请稍后再试, id: "+id;
}
}
3>PaymentController
//****服务熔断
@GetMapping("/payment/circuit/{id}")
public String paymentCircuitBreaker(@PathVariable("id") Integer id){
String result=paymentService.paymentCircuitBreaker(id);
log.info("******result: "+result);
return result;
}
4>测试
正确:http://localhost:8001/payment/circuit/35
错误:http://localhost:8001/payment/circuit/-35
重点测试:多次错误,然后慢慢正确,发现刚开始不满足条件,就算正确的访问也不能进行正确访问
4)原理
1>熔断类型:
1、熔断打开:请求不在进行调用 当前服务,内部设置时钟一般为MTTR(平均故障处理时间),当打开时长达到所设时钟则进入半熔断状态
2、熔断关闭:熔断关闭不会对服务进行熔断
3、熔断半开:部分请求根据规则调用当前服务,如果请求成功且符合规则则认为当前服务恢复正常,关闭熔断
2>官网断路器流程图:
步骤:
断路器在什么情况下起作用:
断路器开启或关闭的条件:
断路器打开之后:
所有配置:参考源码:com.netflix.hystrix.HystrixCommandProperties
8、服务限流
后续高级再讲。。。。
四、hystrix工作流程
五、服务监控hystrixDashboard
1、概述
除了隔离依赖服务的调用以外,Hystrix还提供了准实时的调用监控(Hystrix Dashboard),Hystrix会持续地记录所有通过Hystrix发起的请求的执行信息,并以统计报表和图形的形式展示给用户,包括美妙执行请求多少成功,多少失败等。Netflix通过hystrix-metrics-eent-stream项目实现了对以上指标的监控。Spring Cloud也提供了Hystrix Dashboard的整合,对监控内容转化成可视化界面。
2、仪表盘9001
1)新建cloud-consumer-hystrix-dashboard9001
2)pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
3)yml
server:
port: 9001
4)HystrixdashboardMain9001+新注解@EnableHystrixDashboard
package com.atguigu.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);
}
}
5)所有Provider微服务提供类(8001/8002/8003)都需要监控依赖配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
6)启动cloud-consumer-hystrix-dashboard9001 该微服务后续将监控微服务8001
测试(豪猪哥O(∩_∩)O哈哈~):http://localhost:9001/hystrix
3、断路器演示
1)修改cloud-provider-hystrix-payment8001
注意:新版本Hystrix 需要在主启动类MainAppHystrix8001中指定监控路径
在8001主启动类中添加如下代码:
/*
* 此配置是为了服务监控而配置,与服务容错本身无关,springcloud升级后的坑
* ServletRegistrationBean因为 SpringBoot 的默认路径不是"/htstrix.stream".
* 只要在自己的项目里配置上下面的servlet就可以了
* */
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet=new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean=new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
否则Unable to connect to Command Metric Stream 404
2)监控测试
1、启动eureka7001或者集群、启动8001
2、测试:
测试:http://localhost:8001/payment/circuit/35
http://localhost:8001/payment/circuit/-35
先访问正确地址,在访问错误地址
监控界面如何看:
圆圈:
曲线:用来记录2分钟内流量的相对变化,可以通过它来观察到流量的上升和下降趋势
标签:paymentInfo,Hystrix,hystrix,SpringCloud,public,断路器,org,import,id 来源: https://blog.csdn.net/qq_41307492/article/details/105310003