当前位置:网站首页>Microservice practice | fuse hytrix initial experience
Microservice practice | fuse hytrix initial experience
2022-07-02 09:11:00 【_ Time boiled the rain】
What is? Hystrix
In daily electricity consumption , If the fuse is correctly placed in our circuit , Then when the voltage rises abnormally , The fuse will blow to cut off the current , So as to protect the safe operation of the circuit .
In the cargo ship , In order to prevent the spread of water leakage and fire , Generally, the warehouse will be divided , To avoid the tragedy of a warehouse accident leading to the sinking of the whole ship , This is the bulkhead protection mechanism .
Hystrix The fuse provided is similar , When calling a service provider , When the total number of requests exceeds the configured threshold in a certain period of time , And the error rate is too high in the window period , that Hystrix The call request will be fused , Subsequent requests are short circuited directly , Enter degradation logic , And implement the local degradation strategy .
meanwhile Hystrix It also isolates the service providers in the system , A service provider delays too much or fails , It doesn't lead to the failure of the whole system , At the same time, it can also control the concurrency of calling these services .
All in all ,Hystrix Can disable your system in the event of a dependency service failure , By isolating the services on which the system depends , Prevent service cascading failure , It also provides a failback mechanism , Deal with failures more gracefully , And enable your system to recover from exceptions more quickly .
Hystrix actual combat
Then, in our last article dms service , Make the following changes :
Feign Support by itself Hystrix, There is no need to introduce additional dependencies .
1、 First modify the caller app The configuration file for the service application.xml, Turn on hystrix
feign:
hystrix:
enabled: true
2、 Add service fuse processing , Realization DmsApi Interface
/** * @Author: official account : The programmer 965 * @create 2022-06-26 **/
@Component
public class DmsHystrixImpl implements DmsApi {
@Override
public String findNameByCode(String code) {
return " Server failure ";
}
}
3、 Modify the interface class annotation , Add fusing treatment :fallback = DmsHystrixImpl.class;
(Spring Cloud Feign HTTP Request exception Fallback Fault tolerance mechanism , It is based on Hystrix Realized , So you need to configure parameters feign.hystrix.enabled=true Enable this function )
/** * @Author: official account : The programmer 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、 Add exception code , In order to verify
/** * @Author: official account : The programmer 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 " male ";
case "1" :
return " Woman ";
default:
return " Unknown ";
}
}
}
5、 Start project , To verify , Return the correct result .
summary
In the microservices architecture , There are usually multiple services calling each other , A failure of the underlying service can lead to cascading failures , And then the whole system is not available , This phenomenon is called service avalanche effect . The service avalanche effect is a result of “ Service providers ” The unavailability of “ Serving consumers ” Not available , And will not be available for gradual amplification of the process .
Such as :A As a service provider ,B by A Service consumers ,C and D yes B Service consumers .A Unavailability caused B Not available , And enlarge the unavailable as snowball to C and D when , The avalanche effect is formed . In order to avoid the formation of avalanche effect , You need to use Hystrix.
边栏推荐
- 队列的基本概念介绍以及典型应用示例
- [go practical basis] how to set the route in gin
- 十年开发经验的程序员告诉你,你还缺少哪些核心竞争力?
- C# 高德地图 根据经纬度获取地址
- NPOI 导出Word 字号对应
- Win10 uses docker to pull the redis image and reports an error read only file system: unknown
- WSL安装、美化、网络代理和远程开发
- [go practical basis] how to verify request parameters in gin
- 【Go实战基础】gin 高效神器,如何将参数绑定到结构体
- 机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案
猜你喜欢
京东面试官问:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别
西瓜书--第五章.神经网络
小米电视不能访问电脑共享文件的解决方案
一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
Shengshihaotong and Guoao (Shenzhen) new energy Co., Ltd. build the charging pile industry chain
我服了,MySQL表500W行,居然有人不做分区?
十年开发经验的程序员告诉你,你还缺少哪些核心竞争力?
History of Web Technology
数构(C语言--代码有注释)——第二章、线性表(更新版)
【Go实战基础】gin 如何获取 GET 和 POST 的请求参数
随机推荐
C language implementation of mine sweeping game
I've taken it. MySQL table 500W rows, but someone doesn't partition it?
十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
汉诺塔问题的求解与分析
AMQ6126问题解决思路
一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
分布式服务架构精讲pdf文档:原理+设计+实战,(收藏再看)
C language - Blue Bridge Cup - 7 segment code
微服务实战|熔断器Hystrix初体验
盘点典型错误之TypeError: X() got multiple values for argument ‘Y‘
libusb的使用
十年开发经验的程序员告诉你,你还缺少哪些核心竞争力?
Find the node with the smallest value range in the linked list and move it to the front of the linked list
C nail development: obtain all employee address books and send work notices
Image transformation, transpose
微服务实战|负载均衡组件及源码分析
Solution and analysis of Hanoi Tower problem
【Go实战基础】gin 如何获取 GET 和 POST 的请求参数
【Go实战基础】gin 如何自定义和使用一个中间件
gocv opencv exit status 3221225785