当前位置:网站首页>Use and expansion of fault tolerance and fusing
Use and expansion of fault tolerance and fusing
2022-07-28 04:37:00 【InfoQ】
One 、 Introduction of fault tolerance mechanism
- Concept when the client invokes a microservice , There's a problem , Be able to fail over , Fault tolerance mechanism . Pictured :

- Aim to ensure high availability of microservices .
Two 、 Why use fault tolerance
- The failover mechanism is shown in the figure :
3、 ... and 、 How to apply fault tolerance mechanism in microservices
- Implementation conditions
- while
- Implementation steps
int count = 0;
// Fail over
for(int i=0;i<=3;i++)
{
// Judge whether the threshold is reached
if (count == 3)
{
// Exit loop , Return exception information
throw Exception(" The microservice retry operation exceeded the threshold ");
}
// Business code
try
{
// HttpClient Request microservice code
...................
}
catch(Exception ex)
{
// Capture exception information
count ++;
}
}
Four 、 Defects of fault-tolerant mechanism
- defects
- Low performance , Resource consumption
- Solution
- Set a fixed number of times
- Use circuit breakers
5、 ... and 、 How to use in micro service system Polly
- Fuse
- The concept of fusing is a request . When the request arrives at the server , The request was retried 3 Time 【 Number of tests 】, No request succeeded , Directly fuse the request , Please come in next time , Don't try again , Return exception information directly .
- Conditions
- Polly
- Code implementation
- Conditions
- install Polly
- step
- install
Microsoft.Extensions.Http.Polly
- stay Startup.cs Register in the file
- Method name ConfigureServices
// Abnormal degradation information
var fallBack = new HttpResponseMessage(){
Content = new StringContent(" The system is busy !"),
StatusCode = 504
};
//5: Circuit breaker threshold
//10: Melting time
//ExecutionRejectedException: Capture all abnormal information of fuse
//10: Total number of threads used
services.AddHttpClient("micro[ Request reuse : Custom name ]")
.AddPolicyHandler(Policy<HttpResponseMeaage>.Handle<ExecutionRejectedException>().Fallback(fallBack)) // Abnormal information degradation
.AddPolicyHandler(Policy<HttpResponseMeaage>.Handle<Exception>().CircuitBreakerAsync(5,TimeSpan.FromSeconds(10))) // Circuit breaker
.AddPolicyHandler(Policy.TimeoutAsync<HttpResponseMeaage>(60))// Set timeout
.AddPolicyHandler(Policy<HttpResponseMeaage>.Handle<Exception>().RetryAsync(1))// Retry count
.AddPolicyHandler(Policy.BulkheadAsync<HttpResponseMeaage>(10,100));// resources ( Threads ) Isolation //10: Total number of threads used 100: Total request cache
- Business code
private HttpClientFactory _httpClientFactory;
// Inject... Into the constructor
Constructors (HttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
int count = 0;
// Fail over
for(int i=0;i<=3;i++)
{
// Judge whether the threshold is reached
if (count == 3)
{
// Exit loop , Return exception information
throw Exception(" The microservice retry operation exceeded the threshold ");
}
// Business code
try
{
// Microservice request
HttpClient client = _httpClientFactory.CreateClient("micro");
.........
}
catch(HttpRequestException ex)
{
// Capture exception information
count ++;
}
}
6、 ... and 、 Microservice fault tolerance mechanism Polly Expand
- If two microservices are called at the same time in the aggregated microservice , When the first service hangs up , In order not to affect the subsequent service calls , We need to do service isolation , The code is as follows :
- stay Startup.cs Register in the file
- Method name ConfigureServices
services.AddHttpClient(" Define different names according to different services ")
.AddPolicyHandler(Policy<HttpResponseMeaage>.Handle<ExecutionRejectedException>().Fallback(fallBack)) // Abnormal information degradation
.AddPolicyHandler(Policy<HttpResponseMeaage>.Handle<Exception>().CircuitBreakerAsync(5,TimeSpan.FromSeconds(10))) // Circuit breaker
.AddPolicyHandler(Policy.TimeoutAsync<HttpResponseMeaage>(60))// Set timeout
.AddPolicyHandler(Policy<HttpResponseMeaage>.Handle<Exception>().RetryAsync(1))// Retry count
.AddPolicyHandler(Policy.Bulkhead<HttpResponseMeaage>(10,100));// Resource isolation
== Be careful : When http On request , The name of each service requested must be consistent with the registered name .==
- Caller code
HttpClient client = _httpClientFactory.CreateClient(" Define different names according to different services ");
边栏推荐
- Seamless support for hugging face community, colossal AI low-cost and easy acceleration of large model
- When import is introduced, sometimes there are braces, sometimes there are no braces. How should we understand this?
- [Sylar] framework Chapter 8 timer module
- Blooming old trees -- quickly build a map bed application with imageprocessor
- How to upgrade a pair of 12.2 RAC(primary) and a pair of 12.2 RAC(dataguard) to 19c
- [blood vessel detection] Based on MATLAB mom method, combined with Hessian and curve fitting, blood vessel diameter measurement [including Matlab source code, 1970]
- NAT基本原理与私有IP
- How to upgrade a pair of 12.2 RAC(primary) and a pair of 12.2 RAC(dataguard) to 19c
- Strlen introduction, and the difference between sizeof
- 关系数据库事务中的对象锁定
猜你喜欢

Harmony's Application on the shelves reported an error. The solution of "please use the API of the released version to develop the application and apply for listing"
![[II. Mobile web page development] 2D & 3D conversion and animation, mobile terminal layout, responsive layout](/img/9a/a3b36aa2e5bf53d9f8355ea3c18f1e.png)
[II. Mobile web page development] 2D & 3D conversion and animation, mobile terminal layout, responsive layout
![[kinematics] simulation of orbital angular momentum based on MATLAB [including Matlab source code 1971]](/img/5e/dfe029490183ee74687606941ce98e.jpg)
[kinematics] simulation of orbital angular momentum based on MATLAB [including Matlab source code 1971]

Reading of the paper "attentional encoder network for targeted sentimental classification"
![RuntimeError: stack expects each tensor to be equal size, but got [8] at entry 0 and [2] at entry 2](/img/66/27de1ac0f642fc91fca5196ea6e141.png)
RuntimeError: stack expects each tensor to be equal size, but got [8] at entry 0 and [2] at entry 2

Seamless support for hugging face community, colossal AI low-cost and easy acceleration of large model

Power consumption: leakage power

Internet of things industrial serial port to WiFi module wireless routing WiFi module selection

Pyqt based grouping tool

could only be written to 0 of the 1 minReplication nodes. There are 0 datanode(s) running and 0 node
随机推荐
重要的 SQL Server 函数 - 日期函数
Warning: file already exists but should not: c:\users\workmai\appdata\local\temp appears when Python packages exe\_ MEI13
Password key hard coding check
How to upgrade a pair of 12.2 RAC(primary) and a pair of 12.2 RAC(dataguard) to 19c
将数据库拿到的数据渲染到elementUI 中的table中去
High number_ Chapter 4__ curvilinear integral
mysql分区表改造
Important SQL server functions - date functions
bubble sort
关系数据库事务中的对象锁定
[Sylar] framework -chapter11 socket module
Sort - cardinal sort
Solana's "deceptive behavior": making mobile phones and opening stores
Select sorting method
Docking with Hang Seng express ― dolphin DB NSQ plug-in tutorial
物联网工业串口转WiFi模块 无线路由WiFi模块的选型
[Sylar] framework -chapter12 bytearray module
Nat fundamentals and private IP
[coding and decoding] Huffman coding and decoding based on Matlab GUI [including Matlab source code 1976]
20-Openwrt crond crontab
