当前位置:网站首页>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.
边栏推荐
- Flink-使用流批一体API统计单词数量
- 双非本科生进大厂,而我还在底层默默地爬树(上)
- 【Go实战基础】gin 如何设置路由
- Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`
- Find the node with the smallest value range in the linked list and move it to the front of the linked list
- Use of libusb
- 选择排序和插入排序
- [go practical basis] how to customize and use a middleware in gin
- QT qtimer class
- Talk about the secret of high performance of message queue -- zero copy technology
猜你喜欢

小米电视不能访问电脑共享文件的解决方案

Redis zadd导致的一次线上问题排查和处理

盘点典型错误之TypeError: X() got multiple values for argument ‘Y‘

Troubleshooting and handling of an online problem caused by redis zadd
![[go practical basis] gin efficient artifact, how to bind parameters to structures](/img/c4/44b3bda826bd20757cc5afcc5d26a9.png)
[go practical basis] gin efficient artifact, how to bind parameters to structures

【Go实战基础】如何安装和使用 gin

以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化

Introduction to the basic concept of queue and typical application examples

微服务实战|熔断器Hystrix初体验
![[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](/img/dc/c0ea188ef353ded86759dbe9b29df3.jpg)
[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
随机推荐
Move a string of numbers backward in sequence
微服务实战|微服务网关Zuul入门与实战
Matplotlib剑客行——初相识Matplotlib
十年开发经验的程序员告诉你,你还缺少哪些核心竞争力?
C4D quick start tutorial - C4d mapping
双非本科生进大厂,而我还在底层默默地爬树(上)
Image transformation, transpose
机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女
数构(C语言)——第四章、矩阵的压缩存储(下)
京东高级工程师开发十年,编写出:“亿级流量网站架构核心技术”
微服务实战|手把手教你开发负载均衡组件
Installing Oracle database 19C RAC on Linux
[go practical basis] how to bind and use URL parameters in gin
Win10 uses docker to pull the redis image and reports an error read only file system: unknown
"Redis source code series" learning and thinking about source code reading
AMQ6126问题解决思路
Flink - use the streaming batch API to count the number of words
MYSQL安装出现问题(The service already exists)
Installing Oracle database 19C for Linux
Minecraft install resource pack