当前位置:网站首页>微服务实战|熔断器Hystrix初体验
微服务实战|熔断器Hystrix初体验
2022-07-02 06:33:00 【_时光煮雨】
什么是Hystrix
在日常生活用电中,如果我们的电路中正确地安置了保险丝,那么在电压异常升高时,保险丝就会熔断以便切断电流,从而起到保护电路安全运行的作用。
在货船中,为了防止漏水和火灾的扩散,一般会将货仓进行分割,避免了一个货仓出事导致整艘船沉没的悲剧,这就是舱壁保护机制。
Hystrix提供的熔断器也类似,在调用某个服务提供者时,当一定时间内请求总数超过配置的阈值,且窗口期内错误率过高,那Hystrix就会对调用请求熔断,后续的请求直接短路,进入降级逻辑,并执行本地的降级策略。
同时Hystrix也将系统中的服务提供者隔离起来,一个服务提供者延迟过高或者失败,并不会导致整个系统的失败,同时也能够控制调用这些服务的并发度。
总之,Hystrix 能使你的系统在出现依赖服务失效的时候,通过隔离系统所依赖的服务,防止服务级联失败,同时提供失败回退机制,更优雅地应对失效,并使你的系统能更快地从异常中恢复。
Hystrix实战
接着我们上篇文章中的dms服务,进行如下修改:
Feign 本身就支持Hystrix,不需要额外引入依赖。
1、首先修改调用方app服务的配置文件application.xml,开启hystrix
feign:
hystrix:
enabled: true
2、增加服务熔断处理类,实现DmsApi接口
/** * @Author:公众号:程序员965 * @create 2022-06-26 **/
@Component
public class DmsHystrixImpl implements DmsApi {
@Override
public String findNameByCode(String code) {
return "服务器故障";
}
}
3、修改接口类注解,增加熔断处理类:fallback = DmsHystrixImpl.class;
(Spring Cloud Feign HTTP请求异常Fallback容错机制,它是基于Hystrix实现的,所以要通过配置参数feign.hystrix.enabled=true开启该功能)
/** * @Author:公众号:程序员965 * @create 2022-06-20 **/
@FeignClient(value = "dms",fallback = DmsHystrixImpl.class)
public interface DmsApi {
@RequestMapping(value="/dict/{code}", method = RequestMethod.GET)
public String findNameByCode(@PathVariable("code") String code);
}
4、增加异常代码,以便验证
/** * @Author:公众号:程序员965 * @create 2022-06-20 **/
@RestController
public class DmsController implements DmsApi {
@Override
public String findNameByCode(String code) {
Integer.parseInt("a");
switch (code){
case "0" :
return "男";
case "1" :
return "女";
default:
return "未知";
}
}
}
5、启动项目,进行验证,返回正确结果。
总结
在微服务架构中,通常会有多个服务间相互调用,基础服务的故障可能会导致级联故障,进而造成整个系统不可用的情况,这种现象被称为服务雪崩效应。服务雪崩效应是一种因“服务提供者”的不可用导致“服务消费者”的不可用,并将不可用逐渐放大的过程。
如:A作为服务提供者,B为A的服务消费者,C和D是B的服务消费者。A不可用引起了B的不可用,并将不可用像滚雪球一样放大到C和D时,雪崩效应就形成了。为了避免雪崩效应的形成,就需要使用Hystrix。
边栏推荐
- Leetcode sword finger offer brush questions - day 22
- Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel
- Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
- C language implementation of mine sweeping game
- Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`
- Minecraft air Island service
- [staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)
- gocv图片裁剪并展示
- Leetcode sword finger offer brush questions - day 23
- Loadbalancer dynamically refreshes Nacos server
猜你喜欢

Jd.com interviewer asked: what is the difference between using on or where in the left join association table and conditions

Minecraft module service opening

Cloud computing in my eyes - PAAS (platform as a service)
![[go practical basis] how to set the route in gin](/img/23/f38d68c4fd238d453b9a7670483002.png)
[go practical basis] how to set the route in gin
![[staff] time mark and note duration (staff time mark | full note rest | half note rest | quarter note rest | eighth note rest | sixteenth note rest | thirty second note rest)](/img/7f/2cd789339237b7a881bfed7b7545a9.jpg)
[staff] time mark and note duration (staff time mark | full note rest | half note rest | quarter note rest | eighth note rest | sixteenth note rest | thirty second note rest)

Troubleshooting and handling of an online problem caused by redis zadd

Don't spend money, spend an hour to build your own blog website

【Go实战基础】gin 如何设置路由

During MySQL installation, mysqld Exe reports that the application cannot start normally (0xc000007b)`

WSL installation, beautification, network agent and remote development
随机推荐
Oracle related statistics
Qt QTimer类
Qunhui NAS configuring iSCSI storage
概率还不会的快看过来《统计学习方法》——第四章、朴素贝叶斯法
gocv图片读取并展示
Solution of Xiaomi TV's inability to access computer shared files
十年开发经验的程序员告诉你,你还缺少哪些核心竞争力?
C # save web pages as pictures (using WebBrowser)
C#钉钉开发:取得所有员工通讯录和发送工作通知
MYSQL安装出现问题(The service already exists)
Nacos download, start and configure MySQL database
C language implementation of mine sweeping game
[go practical basis] how to bind and use URL parameters in gin
【Go实战基础】gin 高效神器,如何将参数绑定到结构体
Complete solution of servlet: inheritance relationship, life cycle, container, request forwarding and redirection, etc
"Interview high frequency question" is 1.5/5 difficult, and the classic "prefix and + dichotomy" application question
C# 高德地图 根据经纬度获取地址
「面试高频题」难度大 1.5/5,经典「前缀和 + 二分」运用题
[staff] time mark and note duration (staff time mark | full note rest | half note rest | quarter note rest | eighth note rest | sixteenth note rest | thirty second note rest)
Finishing the interview essentials of secsha system!!!