In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use HttpClientFactory to achieve simple fuse degradation". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Simple use of HttpClientFactory
Use a simple console program to demonstrate
This is just getting the status code, not the actual content.
In fact, the main operation is AddHttpClient, and then create a HttpClient object through HttpClientFactory, with the HttpClient object, the following operation should be needless to say.
Then call the Main method
Console.WriteLine ($"BasicUsage, StatusCode = {BasicUsage () .GetAwaiter () .GetResult ()}")
There is not much difference in usage. Let's take a look at the combination with Polly.
The combination of HttpClientFactory and Polly
The wiki page of Polly already has documentation for a combination of the two.
Https://github.com/App-vNext/Polly/wiki/Polly-and-HttpClientFactory
In fact, it is now very easy for us to use some of the features of Polly for http requests.
We need to add the Nuget package of Microsoft.Extensions.Http.Polly when we use it.
Let's first take a look at three extension methods for using Polly
The extension method shows that AddTransientHttpErrorPolicy mainly deals with errors in Http requests, such as the status code of HTTP 5XX, the status code of HTTP 408 and System.Net.Http.HttpRequestException exceptions. AddPolicyHandler customization is consistent with the traditional way of defining Polly. AddPolicyHandlerFromRegistry chooses what it wants from the Policy collection (which is also custom).
The latter operation is done with AddPolicyHandler.
Since we are going to achieve fuse degradation, we must use CircuitBreakerPolicy and FallbackPolicy, and add a TimeoutPolicy to facilitate the demonstration.
Since multiple Policy are involved, we have to determine the order in which they are executed!
Polly's wiki page has an example, along with a very detailed timing diagram.
In a word, it is the first to work, or the last to add.
Let's create a new API project to demonstrate.
Modify the ConfigureServices method as follows
And then use it in the controller.
The effect is as follows
In the previous requests, the result is fallback because of a timeout or a circuit breaker.
After 4 seconds after the request, because there is no timeout, the normal got the result, so the fuse will be reset.
Take a look at the log.
I can see all the operations clearly.
"how to use HttpClientFactory to achieve simple fuse degradation" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.