当前位置:网站首页>容错、熔断的使用与扩展
容错、熔断的使用与扩展
2022-07-24 11:39:00 【@@Mr.Fu】
一、容错机制的介绍
概念
当客户端调用微服务的时候,出现了故障,能够进行故障转移,就是容错机制。
如图:
目的
保证微服务的高可用。
二、为什么使用容错机制
- 故障转移机制
如图:
三、如何在微服务中应用容错机制
- 实现条件
- while
- 实现步骤
int count = 0; //故障转移 for(int i=0;i<=3;i++) { //判断是否达到阀值 if (count == 3) { //退出循环,返回异常信息 throw Exception("微服务重试操作超出阀值"); } //业务代码 try { // HttpClient 请求微服务代码 ................... } catch(Exception ex) { //捕获异常信息 count ++; } }
四、容错机制的缺陷
- 缺陷
- 性能低,耗资源
- 解决方案
- 设置固定的次数
- 使用熔断机制
五、如何在微服务系统中使用Polly
- 熔断
- 概念
熔断的是一个请求。
当请求到服务器的时候,该请求重试了3次【测试次数】,没有请求成功,直接熔断该请求,下次请求进来,不必再去重试,直接返回异常信息。
- 概念
- 条件
- Polly
- 代码实现
- 条件
- 安装 Polly
- 步骤
- 安装
Microsoft.Extensions.Http.Polly - 在Startup.cs 文件中注册
- 方法名 ConfigureServices
//异常降级信息 var fallBack = new HttpResponseMessage(){ Content = new StringContent("系统忙!"), StatusCode = 504 }; //5:断路器阀值 //10:熔断时间 //ExecutionRejectedException:捕获熔断的所有异常信息 //10:使用的线程总数 services.AddHttpClient("micro[请求复用:自定义名称]") .AddPolicyHandler(Policy<HttpResponseMeaage>.Handle<ExecutionRejectedException>().Fallback(fallBack)) //异常信息降级 .AddPolicyHandler(Policy<HttpResponseMeaage>.Handle<Exception>().CircuitBreakerAsync(5,TimeSpan.FromSeconds(10))) //断路器 .AddPolicyHandler(Policy.TimeoutAsync<HttpResponseMeaage>(60))//设置超时时间 .AddPolicyHandler(Policy<HttpResponseMeaage>.Handle<Exception>().RetryAsync(1))//重试次数 .AddPolicyHandler(Policy.BulkheadAsync<HttpResponseMeaage>(10,100));//资源(线程)隔离 //10:使用的线程总数 100:请求缓存总数 - 业务代码
private HttpClientFactory _httpClientFactory; //构造函数中注入 构造函数(HttpClientFactory httpClientFactory) { _httpClientFactory = httpClientFactory; } int count = 0; //故障转移 for(int i=0;i<=3;i++) { //判断是否达到阀值 if (count == 3) { //退出循环,返回异常信息 throw Exception("微服务重试操作超出阀值"); } //业务代码 try { //微服务请求 HttpClient client = _httpClientFactory.CreateClient("micro"); ......... } catch(HttpRequestException ex) { //捕获异常信息 count ++; } }
- 方法名 ConfigureServices
- 安装
- 条件
六、微服务容错机制Polly扩展
- 如果在聚合微服务中同时调用了两个微服务,当第一个服务挂掉,为了不影响后面的服务调用,我们要做服务隔离,代码如下:
- 在Startup.cs 文件中注册
- 方法名 ConfigureServices
注意:当http请求的时候,请求各自服务的名称必须和注册名称一致。services.AddHttpClient("根据不同的服务定义不同的名称") .AddPolicyHandler(Policy<HttpResponseMeaage>.Handle<ExecutionRejectedException>().Fallback(fallBack)) //异常信息降级 .AddPolicyHandler(Policy<HttpResponseMeaage>.Handle<Exception>().CircuitBreakerAsync(5,TimeSpan.FromSeconds(10))) //断路器 .AddPolicyHandler(Policy.TimeoutAsync<HttpResponseMeaage>(60))//设置超时时间 .AddPolicyHandler(Policy<HttpResponseMeaage>.Handle<Exception>().RetryAsync(1))//重试次数 .AddPolicyHandler(Policy.Bulkhead<HttpResponseMeaage>(10,100));//资源隔离
- 方法名 ConfigureServices
- 调用方代码
HttpClient client = _httpClientFactory.CreateClient("根据不同的服务定义不同的名称");
- 在Startup.cs 文件中注册
边栏推荐
- [deserialization vulnerability-02] principle test and magic method summary of PHP deserialization vulnerability
- Remember to optimize my personal blog once
- 生信周刊第37期
- One week's wonderful content sharing (issue 13)
- Database operation through shell script
- What is the charm of CSDN members? What's the use of him?
- [QNX Hypervisor 2.2用户手册]9.2 cmdline
- L1-049 天梯赛座位分配
- The art of management - driving software R & D efficiency through leadership
- Dry goods sharing - taking over a new data team as a lead - Problem Inventory and insights findings
猜你喜欢

20000 words detailed explanation, thoroughly understand es!
![MOS tube - Notes on rapid recovery application (I) [principle]](/img/a1/8427c9b1d0ea0cecce820816510045.png)
MOS tube - Notes on rapid recovery application (I) [principle]

Types and history of bugs in it circle

Robot framework official tutorial (I) getting started
![Operational amplifier - Notes on rapid recovery [1] (parameters)](/img/1f/37c5548ce84b6a217b4742431f1cc4.png)
Operational amplifier - Notes on rapid recovery [1] (parameters)

Remember to optimize my personal blog once

Svn server and client installation (Chinese package) and simple use

Two important laws about parallelism

C language programming code

Semaphore详解
随机推荐
The difference between YPbPr and YCbCr
gcc -l参数和-L参数的区别
【反序列化漏洞-02】PHP反序列化漏洞原理测试及魔术方法总结
Sorting out the ideas of data processing received by TCP server, and the note of select: invalid argument error
Paging query of employee information of black maredge takeout
Shell script "< < EOF" my purpose and problems
[deserialization vulnerability-02] principle test and magic method summary of PHP deserialization vulnerability
Detailed explanation of stat function
Imeta view | is short reading long amplicon sequencing applicable to the prediction of microbiome function?
JVM visualvm: multi hop fault handling tool
Dry goods sharing - taking over a new data team as a lead - Problem Inventory and insights findings
Idea runs the wordcount program (detailed steps)
哈希——349. 两个数组的交集
黑马瑞吉外卖之员工信息分页查询
哈希——242.有效的字母异位词
20000 words detailed explanation, thoroughly understand es!
Nacos permissions and databases
oracle 11.2.0.4 asm单实例不随系统启动而自动开启
Tensor and numpy convert "suggested collection" to each other
Best practice | using Tencent cloud AI character recognition to realize enterprise qualification certificate recognition