当前位置:网站首页>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 ");
边栏推荐
- 高数_第4章__曲线积分_习题解法
- 【牛客】求1+2+3+...+n
- [mathematical modeling] Based on MATLAB seismic exploration Marmousi model [including Matlab source code, 1977]
- MySQL partition table transformation
- Attempt method in laravel user authentication
- np. The data returned from delete details is the data after deleting the specified dimension
- When import is introduced, sometimes there are braces, sometimes there are no braces. How should we understand this?
- pytorch_ Lightning in lightning_ The output of hparams.yaml in logs is null
- 【sylar】框架篇-Chapter14-TcpServer 模块
- Fearless of side impact damage, Chery arize 8 fully protects the safety of passengers
猜你喜欢

Select sorting method

Blooming old trees -- quickly build a map bed application with imageprocessor

Space complexity calculation super full sorting!! (calculation of hand tearing complexity

031 log4j log framework

could only be written to 0 of the 1 minReplication nodes. There are 0 datanode(s) running and 0 node

Ma Yi, Shen Xiangyang, Cao Ying's latest AI overview is hot! It took 3 months to build, netizens: required papers

Important SQL server functions - string utilities

10 more advanced open source command line tools

Introduction to this pointer

Use Baidu developer tool 4.0 to build a dedicated applet IDE
随机推荐
Learn regular expressions (regexp)
校园流浪猫信息记录和分享的小程序源码
Bio annotation of emotion analysis aste triples extraction
[Sylar] framework -chapter7-io coordination scheduling module
High number_ Chapter 4__ curvilinear integral
【sylar】框架篇-Chapter24-支持业务模块化
Study of the Intel aria 10 Avalon mm DMA interface for PCI Express solutions User Guide
【sylar】框架篇-Chapter21-环境变量模块
Niuke, convert string to integer
Render the data obtained from the database to the table in elementui
Study notes of Gu Yujia on July 27, 2022
Password key hard coding check
transform: failed to synchronize: cudaErrorAssert: device-side assert triggered
[Sylar] framework -chapter11 socket module
重要的 SQL Server 函数 - 数字函数
Nat fundamentals and private IP
Docking with Hang Seng express ― dolphin DB NSQ plug-in tutorial
Cloud native Devops status survey questionnaire solicitation: kodelurover launched jointly with oschina
重要的 SQL Server 函数 - 日期函数
Information system project manager (2022) - key content: organization level project management, process management, project set management (18)
