Requirements: The previous project has been using Polly for the HttpClient retry mechanism, due to network fluctuations or the project may not be able to provide services normally during the release period, using Polly under the microservice architecture improves the availability of the service, and will not cause the entire transaction to fail due to a short exception, but requires interface idempotential.
Polly is a .NET resilient and transient fault handling library that allows developers to express policies such as retries, circuit breakers, timeouts, bulkhead isolation, rate limiting, and fallbacks in a smooth and thread-safe manner.
GitHub address:The hyperlink login is visible.
First, create a new website interface, and the normal status code will be returned only after the third request, the code is as follows:
.NET console app
Create a new console application and use the HttpClient request interface to attempt to request an error as follows:
Introducing Polly, adding a retry mechanism, the nuget command is as follows:
The code is as follows:
Retried 3 times, 4th request succeeded, successfully obtains the returned data, as shown in the figure below:
ASP.NET Core apps
For ASP.NET Core projects, you can directly reference Microsoft.Extensions.Http.Polly, as follows:
The code is as follows:
Reference:
The hyperlink login is visible.
The hyperlink login is visible. (End)
|