Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use HttpClientFactory to realize simple fuse degradation

Shulou Source: shulou.com Published: 2022-06-02 05:12:18 10月03日 Update

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!

Tags: Method content state presentation reality object that is more knowledge results pages control practicality consistency success in learning and then in a word no tradition function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Technology Huawei Shulou Information Apple