当前位置:网站首页>容错、熔断的使用与扩展
容错、熔断的使用与扩展
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 文件中注册
边栏推荐
猜你喜欢

DevOps及DevOps常用的工具介绍

Imeta view | is short reading long amplicon sequencing applicable to the prediction of microbiome function?

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

cgo+gSoap+onvif学习总结:9、go和c进行socket通信进行onvif协议处理

Robot framework official tutorial (I) getting started

The third day of hcip mGRE experiment

Semaphore详解

stream流

Directional crawling Taobao product name and price (teacher Songtian)
![Detailed OSPF configuration of layer 3 switch / router [Huawei ENSP experiment]](/img/a9/f080940ec7bf94ab83c922990efa62.png)
Detailed OSPF configuration of layer 3 switch / router [Huawei ENSP experiment]
随机推荐
Svn server and client installation (Chinese package) and simple use
One week's wonderful content sharing (issue 13)
Online customer service chat system source code_ Beautiful and powerful golang kernel development_ Binary operation fool installation_ Construction tutorial attached
Experience of redis deepwater area -- Interview reference
String -- 344. Reverse string
CCF 1-2 question answering record (2)
MOS tube - Notes on rapid recovery application (I) [principle]
运算放大器 —— 快速复苏笔记[壹](参数篇)
Basic syntax of MySQL DDL and DML and DQL
Two important laws about parallelism
Semaphore details
Cgo+gsoap+onvif learning summary: 9. Go and C conduct socket communication and onvif protocol processing
字符串——344.反转字符串
Hash - 1. Sum of two numbers - some people fall in love during the day, some people watch the sea at night, and some people can't do the first question
L2-011 玩转二叉树
LogBack & MDC & a simple use
Microservice - eruka
[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)
Leetcode 112. path sum
哈希——202. 快乐数