当前位置:网站首页>10.Hystrix断路器
10.Hystrix断路器
2022-06-28 16:09:00 【苦 糖 果】
1. 概述
1.1分布式系统面临的问题
1.2.是什么
1.3.能干嘛
服务降级
服务熔断
服务限流
接近实时的监控
github地址:https://github.com/Netflix/Hystrix/wiki/How-To-Use
Hystrix官宣,停更进维 ,被动修复bugs,不再接受合并请求,不再发布新版本
2.hystrix案例
2.1 新建cloud-provider-hystrix-payment8001
2.1.1 改pom
<dependencies>
<!--新增hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.1.2改yml
server:
port: 8001
eureka:
client:
register-with-eureka: true #表识不向注册中心注册自己
fetch-registry: true #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
# defaultZone: http://eureka7002.com:7002/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://eureka7001.com:7001/eureka/
# server:
# enable-self-preservation: false
spring:
application:
name: cloud-provider-hystrix-payment
# eviction-interval-timer-in-ms: 2000
2.1.3主启动类与业务类
@SpringBootApplication
@EnableEurekaClient
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class,args);
}
}
@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_TimeOut(@PathVariable("id") Integer id){
String result = paymentService.paymentInfo_TimeOut(id);
log.info("*******result:"+result);
return result;
}
}
@Service
public class PaymentService {
//成功
public String paymentInfo_OK(Integer id){
return "线程池:"+Thread.currentThread().getName()+" paymentInfo_OK,id: "+id+"\t"+"哈哈哈" ;
}
//失败
public String paymentInfo_TimeOut(Integer id){
int timeNumber = 3;
try {
TimeUnit.SECONDS.sleep(timeNumber); }catch (Exception e) {
e.printStackTrace();}
return "线程池:"+Thread.currentThread().getName()+" paymentInfo_TimeOut,id: "+id+"\t"+"呜呜呜"+" 耗时(秒)"+timeNumber;
}
}
2.1.4正常测试
启动eureka7001,启动cloud-provider-hystrix-payment8001
访问:http://localhost:8001/payment/hystrix/ok/31 和 http://localhost:8001/payment/hystrix/timeout/31
2.1.5 高并发测试
开启Jmeter,来30000个并发压死8001,30000个请求都去访问paymentInfo_TimeOut服务
结果:ok和timeout这两个接口都在转圈圈
why?只是压测timeout接口,ok接口为何会变慢?
tomcat的默认的工作线程数被打满了,没有多余的线程来分解压力和处理。
2.2.cloud-consumer-feign-hystrix-order80
2.2.1改pom
<dependencies>
<!--新增hystrix-->
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.2.2改yml
server
port: 80
eureka:
client:
register-with-eureka: true #表识不向注册中心注册自己
fetch-registry: true #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
spring:
application:
name: cloud-provider-hystrix-order
2.2.3主启动类与业务类
@SpringBootApplication
@EnableFeignClients
public class OrderHystrixMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderHystrixMain80.class,args);
}
}
@Component
@FeignClient(value = "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_TimeOut(@PathVariable("id") Integer id);
}
@RestController
@Slf4j
public class OrderHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@Value("${server.port}")
private String serverPort;
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id){
String result = paymentHystrixService.paymentInfo_OK(id);
log.info("*******result:"+result);
return result;
}
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
String result = paymentHystrixService.paymentInfo_TimeOut(id);
log.info("*******result:"+result);
return result;
}
2.2.4测试
http://localhost/consumer/payment/hystrix/ok/31 和 http://localhost/consumer/payment/hystrix/timeout/31
2. 3服务降级
服务降级可以做在服务端,也可以做在客户端。
2.3.1服务端8001服务降级
主启动类添加注解:@EnableHystrix,具体方法添加@HystrixCommand
设置自身调用超时时间的峰值,峰值内可以正常运行,超过了需要有兜底的方法处理,作服务降级fallback
一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法
@Service
public class PaymentService {
//成功
public String paymentInfo_OK(Integer id){
return "线程池:"+Thread.currentThread().getName()+" paymentInfo_OK,id: "+id+"\t"+"哈哈哈" ;
}
//失败
@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000") //3秒钟以内就是正常的业务逻辑
})
public String paymentInfo_TimeOut(Integer id){
// int timeNumber = 5;
int age = 10/0;
// try { TimeUnit.SECONDS.sleep(timeNumber); }catch (Exception e) {e.printStackTrace();}
//return "线程池:"+Thread.currentThread().getName()+" paymentInfo_TimeOut,id: "+id+"\t"+"呜呜呜"+" 耗时(秒)"+timeNumber;
return "线程池:"+Thread.currentThread().getName()+" paymentInfo_TimeOut,id: "+id+"\t"+"呜呜呜"+" 耗时(秒)";
}
//兜底方法
public String paymentInfo_TimeOutHandler(Integer id){
return "线程池:"+Thread.currentThread().getName()+" 系统繁忙, 请稍候再试 ,id: "+id+"\t"+"哭了哇呜";
}
}
2.3.2客户端80服务降级
80订单微服务,也可以更好的保护自己,自己也依样画葫芦进行客户端降级保护
feign:
hystrix:
enabled: true #如果处理自身的容错就开启。开启方式与生产端不一样
主启动类添加注解:@EnableHystrix,具体方法添加@HystrixCommand
当前问题:和业务逻辑混一起???混乱
根据cloud-consumer-feign-hystrix-order80已经有的PaymentHystrixService接口,重新新建一个类(PaymentFallbackService)实现该接口,统一为接口里面的方法进行异常处理
@Component
public class PaymentFallbackService implements PaymentHystrixService {
@Override
public String paymentInfo_OK(Integer id) {
return "-----PaymentFallbackService fall back-paymentInfo_OK , (┬_┬)";
}
@Override
public String paymentInfo_TimeOut(Integer id) {
return "-----PaymentFallbackService fall back-paymentInfo_TimeOut , (┬_┬)";
}
}
在PaymentHystrixService 中注明fallback的类为PaymentFallbackService
@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id);
}
2.3.3 测试
总结:
在客户端和服务端同是配置了服务降级的情况下,如果是客户端设置超时时间在服务端看来是正常情况,则会走客户端的降级逻辑。反之,则走服务端降级逻辑。
如果服务端出现运行时异常,则会走服务端降级逻辑。
如果服务端宕机,则会走客户端降级逻辑。
边栏推荐
- FS2K人脸素描属性识别
- What are the most powerful small and medium-sized companies in Beijing?
- The new paradigm of AI landing is "hidden" in the next major upgrade of software infrastructure
- 5 minutes to make a bouncing ball game
- What you have to know under the digital collection boom
- Tiktok actual battle ~ list of bloggers I follow, follow and check
- Slim gain (sgain) introduction and code implementation -- missing data filling based on generated countermeasure network
- Tongziping, partner of Tongchuang Weiye: "what should yuan universe invest in?"
- STM32CubeMX使用方法及功能介绍
- MySQL auto - Connect Query recommended favorites
猜你喜欢
抖音实战~我关注的博主列表、关注、取关
Briefly introduce the conversion between tensorflow and pytorch (mainly tensorflow to pytorch)
逆向调试入门-PE结构详解02/07
今天睡眠质量记录80分
北京有哪些牛逼的中小型公司?
What are the most powerful small and medium-sized companies in Beijing?
Super automation and the future of network security
Slim gain (sgain) introduction and code implementation -- missing data filling based on generated countermeasure network
浅谈 SAP 软件里的价格折扣设计原理
The future of platform as code is kubernetes extension
随机推荐
STM32CubeMX使用方法及功能介绍
Mysql自連接查詢「建議收藏」
A little hesitant in the morning
Internet of things cloud convergence Security Guide
Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
leetcode:22. 括号生成
A new 25K byte from the Department showed me what the ceiling is
Convolutional neural network for machine learning uses cifar10 data set and alexnet network model to train classification model, install labelimg, and report error
一台服务器最大并发 tcp 连接数多少?65535?
使用 Open Connector 进行 HubSpot 和 SAP 系统的集成工作
24岁秃头程序员教你微服务交付下如何持续集成交付,学不会砍我
Use open connector to integrate hubspot and SAP systems
【Golang】安装 iris 的方法
MySQL auto - Connect Query recommended favorites
The first place on the list - brake by wire "new cycle", the market competitiveness of local suppliers is TOP10
China energy integration and Tianyi cloud create an "energy brain"
#夏日挑战赛#OHOS构建自定义服务实战
The future of platform as code is kubernetes extension
PostgreSQL异常处理
【Hot100】4. Find the median of two positive arrays