当前位置:网站首页>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.
边栏推荐
- How to realize asynchronous programming in a synchronous way?
- Matplotlib剑客行——没有工具用代码也能画图的造型师
- 【Go实战基础】gin 如何自定义和使用一个中间件
- [staff] the lines and spaces of the staff (the nth line and the nth space in the staff | the plus N line and the plus N space on the staff | the plus N line and the plus N space below the staff | the
- 【Go实战基础】gin 如何验证请求参数
- 寻找链表中值域最小的节点并移到链表的最前面
- Installing Oracle database 19C for Linux
- CSDN Q & A_ Evaluation
- Introduction to the basic concept of queue and typical application examples
- C language implementation of mine sweeping game
猜你喜欢
MYSQL安装出现问题(The service already exists)
西瓜书--第六章.支持向量机(SVM)
C language - Blue Bridge Cup - 7 segment code
[go practical basis] how can gin get the request parameters of get and post
Complete solution of servlet: inheritance relationship, life cycle, container, request forwarding and redirection, etc
微服务实战|手把手教你开发负载均衡组件
机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女
盘点典型错误之TypeError: X() got multiple values for argument ‘Y‘
微服务实战|声明式服务调用OpenFeign实践
Solution and analysis of Hanoi Tower problem
随机推荐
数构(C语言)——第四章、矩阵的压缩存储(下)
微服务实战|微服务网关Zuul入门与实战
C call system sound beep~
机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案
Matplotlib剑客行——布局指南与多图实现(更新)
随笔:RGB图像颜色分离(附代码)
Analysis and solution of a classical Joseph problem
Function ‘ngram‘ is not defined
WSL安装、美化、网络代理和远程开发
Minecraft air Island service
【Go实战基础】gin 如何自定义和使用一个中间件
QT qtimer class
Move a string of numbers backward in sequence
小米电视不能访问电脑共享文件的解决方案
[go practical basis] how to install and use gin
C4D quick start tutorial - Chamfer
Redis安装部署(Windows/Linux)
【Go实战基础】gin 如何设置路由
Oracle related statistics
QT drag event