当前位置:网站首页>容错、熔断的使用与扩展
容错、熔断的使用与扩展
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 文件中注册
边栏推荐
- How to choose sentinel vs. hystrix current limiting?
- MySql的DDL和DML和DQL的基本语法
- 16 tips for system administrators to use iptables
- Win10 icon turns white, recovery method
- Detailed OSPF configuration of layer 3 switch / router [Huawei ENSP experiment]
- Recommended SSH cross platform terminal tool tabby
- 字符串——344.反转字符串
- 哈希——15. 三数之和
- The difference between YPbPr and YCbCr
- Semaphore详解
猜你喜欢

Linked list - 142. Ring linked list II
![Operational amplifier - Notes on rapid recovery [1] (parameters)](/img/1f/37c5548ce84b6a217b4742431f1cc4.png)
Operational amplifier - Notes on rapid recovery [1] (parameters)

How to use a third party without obtaining root permission topic: MIUI chapter

C language programming code

Fiddler packet capture tool summary

What is the charm of CSDN members? What's the use of him?

字符串——344.反转字符串

HCIP MGRE实验 第三天

视频回放 | 如何成为一名优秀的地学和生态学领域的国际期刊审稿人?
![08 [AIO programming]](/img/a6/156cb97e653190c76f22c88b758fef.png)
08 [AIO programming]
随机推荐
运算放大器 —— 快速复苏笔记[贰](应用篇)
Optimization method of "great mathematics for use" -- optimal design of Cascade Reservoir Irrigation
Introduction to Devops and common Devops tools
Experience of redis deepwater area -- Interview reference
【C和指针第11章】动态内存分配
Performance test summary (I) -- basic theory
Easy to understand ES6 (IV): template string
LogBack & MDC & a simple use
Fiddler packet capture tool summary
Tensor and numpy convert "suggested collection" to each other
[deserialization vulnerability-01] Introduction to serialization and deserialization
Jmeter-While控制器
L1-059 敲笨钟
[QNX Hypervisor 2.2用户手册]9.2 cmdline
scrapy-redis写项目备忘
Use prometheus+grafana to monitor server performance in real time
链表——剑指offer面试题 02.07. 链表相交
20000 words detailed explanation, thoroughly understand es!
Fastcgi operation principle and PHP FPM parameter configuration
L1-043 阅览室