当前位置:网站首页>微服务实战|熔断器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。
边栏推荐
- 查看was发布的应用程序的端口
- Complete solution of servlet: inheritance relationship, life cycle, container, request forwarding and redirection, etc
- Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers
- Solution of Xiaomi TV's inability to access computer shared files
- Openshift container platform community okd 4.10.0 deployment
- Shengshihaotong and Guoao (Shenzhen) new energy Co., Ltd. build the charging pile industry chain
- 队列的基本概念介绍以及典型应用示例
- During MySQL installation, mysqld Exe reports that the application cannot start normally (0xc000007b)`
- 【Go实战基础】如何安装和使用 gin
- Matplotlib剑客行——布局指南与多图实现(更新)
猜你喜欢

Cloud computing in my eyes - PAAS (platform as a service)

Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel
![[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)](/img/ae/1ecb352c51a101f237f244da5a2ef7.jpg)
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)

How to realize asynchronous programming in a synchronous way?

整理秒杀系统的面试必备!!!

win10使用docker拉取redis镜像报错read-only file system: unknown

2022/2/13 summary

kubernetes部署loki日志系统

What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?

Linux二进制安装Oracle Database 19c
随机推荐
gocv opencv exit status 3221225785
随笔:RGB图像颜色分离(附代码)
「面试高频题」难度大 1.5/5,经典「前缀和 + 二分」运用题
Matplotlib剑客行——容纳百川的艺术家教程
队列的基本概念介绍以及典型应用示例
Synchronize files using unison
2022/2/13 summary
First week of JS study
聊聊消息队列高性能的秘密——零拷贝技术
[go practical basis] how to set the route in gin
C Gaode map obtains the address according to longitude and latitude
Using recursive functions to solve the inverse problem of strings
Image transformation, transpose
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
Oracle 相关统计
【Go实战基础】gin 如何自定义和使用一个中间件
Gocv image reading and display
WSL installation, beautification, network agent and remote development
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
Jd.com interviewer asked: what is the difference between using on or where in the left join association table and conditions