当前位置:网站首页>微服务 - Hystrix 熔断器
微服务 - Hystrix 熔断器
2022-07-25 05:19:00 【从零开始的JAVA世界】
1. Hystrix简介
Hystrix是Netflix开源的一个延迟和容错库,用于隔离远程服务、第三方库,防止出现级联失败。
2. 雪崩问题
服务A调用服务B,如果服务B出现问题,那么服务A就需要等待。如果高并发请求,那么就会造成多线程阻塞,导致服务器资源耗尽,从而导致其他所有服务都不可用。
3. 级联失败
某个服务不可用,其他依赖它的服务都不可用。
4. 服务降级
找到出问题的服务或依赖,把执行优先级降低,让用户请求可以立刻获取失败信息,不用等待。
Hystrix解决雪崩问题的两种方式:
- 线程隔离
- 服务熔断
1. 线程隔离
使用多线程的特性,子线程的失败,不会影响主线程。
Hystrix为每个依赖服务调用分配一个小的线程池,用户的请求将不再直接访问服务,而是通过线程池中的空闲线程来访问服务,如果线程池已满调用将被立即拒绝,否则使用线程来处理请求,可以在主线程中设置超时时间,如果超过这个时间子线程还没有执行完或者子线程执行出现异常,则会进行服务降级。
服务降级:优先保证核心服务,非核心服务不可用或弱可用。
- 引入依赖
<!--Hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- 启动类开启注解
@SpringBootApplication
@EnableCircuitBreaker //开启Hystrix
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
- 在需要保护的方法上添加注解@HystrixCommand
使用自定义降级逻辑 fallbackMethod
方法的参数、返回值必须和保护的方法一致
使用默认降级逻辑 defaultMethod
方法的返回值必须和保护的方法一致
类上要加注解@DefaultProperties(defaultFallback = “defaultFallback”)
修改超时时长 (默认一秒)
2. 服务熔断
熔断器也叫短路器,英文单词 CircuitBreaker ,可以自动断开,也可以自动打开。
短路器的三种状态:
closed: 闭合,所有请求可以通过断路器
open:打开,对某个服务的请求不能通过,返回失败。
halfOpen: 中间状态,释放一个请求探测远端服务是否可用。
三种状态的转化:
开始时断路器闭合,服务A调用服务B时,断路器会统计请求的失败比例,如果超过一定比例,断路器打开,短路器会阻断对此问题服务的请求,直接返回失败。
Open状态并不是永久的,默认保持5秒。5秒后状态改为HalfOpen,
HalfOpen状态,短路器会放过一个请求去探测远端的服务是否可用,可用则转为Close状态,所有请求可以通过。如果仍不可用则转为Open状态再等5秒。

hystrix:
command:
default:
#服务熔断,熔断器配置
circuitBreaker:
requestVolumeThreshold: 3 #触发熔断的最小请求次数
sleepWindowInMilliseconds: 10000 #Open状态的维持时间
errorThresholdPercentage: 50 #失败百分比
边栏推荐
- JWT(json web token)
- Blog Description & message board
- LCP插件创建对等802.1ad接口
- Your technical leader doesn't understand this? Without it, there is no complete thinking process of design
- 2022-7-15 summary
- 自己实现is_class
- Bypass XSS filters in Web Applications
- HMS Core Discovery第16期直播预告|与虎墩一起,玩转AI新“声”态
- Solution of win11 blue screen code 0x0000001a
- Logu p3398 hamsters find sugar solution
猜你喜欢

Build keyword driven automated testing framework

Unity LOD

Pikachu vulnerability platform exercise

Go language function

An article takes you to understand the sentinel mode of redis

Docker builds MySQL master-slave replication

2022-7-13 summary

Wechat applet related operation examples

小红书携手HMS Core,畅玩高清视界,种草美好生活
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]
随机推荐
绕过 Web 应用程序中的 XSS 过滤器
STM32 development note 117: generate IIR low-pass filter coefficients using MATLAB
Preliminary understanding of Panda3D particle system
HMS Core Discovery第16期直播预告|与虎墩一起,玩转AI新“声”态
When image component in wechat applet is used as background picture
STL notes (VII): container deque
Thesis reading | which is the best multilingual pre training technology for machine translation? See the latest progress!
Solution of win11 blue screen code 0x0000001a
Ownership in rust -- introduction of rust language Xiaobai 11
Luogu p4281 [ahoi2008] emergency gathering / gathering solution
Document collaboration tool recommendation
Go language function
Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life
搭建私有CA服务器
OpenFegin远程调用丢失请求头问题
How do novices open accounts for stock speculation? Is it safe for securities companies to open accounts online?
Anshi semiconductor management paid a visit to Wentai technology and Gree Electric appliance
Learning records [email protected] R & D effectiveness measurement indicators
JS common code questions array de duplication - Custom New throttling and anti shake - deep copy - instanceof URL parameter extraction - thousand separator - array to tree structure - array flattening
Redis cluster setup (Windows)