当前位置:网站首页>Use and expansion of fault tolerance and fusing
Use and expansion of fault tolerance and fusing
2022-07-24 11:48:00 【@@Mr.Fu】
List of articles
- One 、 Introduction of fault tolerance mechanism
- Two 、 Why use fault tolerance
- 3、 ... and 、 How to apply fault tolerance mechanism in microservices
- Four 、 Defects of fault-tolerant mechanism
- 5、 ... and 、 How to use in micro service system Polly
- 6、 ... and 、 Microservice fault tolerance mechanism Polly Expand
One 、 Introduction of fault tolerance mechanism
Concept
When the client invokes the microservice , There's a problem , Be able to fail over , Fault tolerance mechanism .
Pictured :
Purpose
Ensure high availability of microservices .
Two 、 Why use fault tolerance
- Failover mechanism
Pictured :
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
- Concept
The fuse 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 .
- Concept
- 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 ++; } }
- Method name ConfigureServices
- install
- Conditions
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
Be careful : When http On request , The name of each service requested must be consistent with the registered name .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
- Method name ConfigureServices
- Caller code
HttpClient client = _httpClientFactory.CreateClient(" Define different names according to different services ");
- stay Startup.cs Register in the file
边栏推荐
- Agile? DevOps ?
- Use prometheus+grafana to monitor server performance in real time
- Easy to use example
- 动态内存管理
- 20000 words detailed explanation, thoroughly understand es!
- 2 万字详解,吃透 ES!
- SSH跨平台终端工具tabby推荐
- What is the difference between low code and no code?
- Shell script "< < EOF" my purpose and problems
- Hash - 242. valid alphabetic ectopic words
猜你喜欢
随机推荐
Script redis write project notes
栈顶与栈底
L1-059 敲笨钟
三、MFC消息映射机制实现原理
链表——142. 环形链表 II
scrapy-redis写项目备忘
Literature record (part109) -- self representation based unsupervised exemplar selection in a union of subspaces
stream流
Leetcode 257. all paths of binary tree
一周精彩内容分享(第13期)
哈希——242.有效的字母异位词
JVM visualvm: multi hop fault handling tool
IT圈中的Bug的类型与历史
L1-043 阅览室
在kuborad图形化界面中,操作Kubernetes 集群,实现mysql中的主从复制
源码分析Sentry用户行为记录实现过程
Share the typora tool
Record a garbage collection and analysis of gceasy
Is there any charge for PDF processing? impossible!
JMeter if controller
](/img/1f/37c5548ce84b6a217b4742431f1cc4.png)








