项目作者: dianper

项目描述 :
POC using Polly framework.
高级语言: C#
项目地址: git://github.com/dianper/resilience-netcore.git
创建时间: 2020-04-08T11:46:48Z
项目社区:https://github.com/dianper/resilience-netcore

开源协议:MIT License

下载


Resilience .NETCore

A simple POC using Polly to deal with resilience.

  • Timeout
  • Retry
  • Circuit Breaker
  • Fallback

Running without API

With Powershell run dotnet run into ResilienceSample folder.

  1. Now listening on: http://localhost:5001

Request http://localhost:5001/api/values on browser and look at the logs on Powershell. It will try to connect on http://localhost:5880/api/products/:productId

  • It will try 3 request with an interval of 2 seconds each before open the circuit and will return fallback result.

Logs:

  1. 11:58:52 - Error: [ProductClient.GetAsync]: No connection could be made because the target machine actively refused it
  2. 11:58:52 - Request failed. Waiting 00:00:02 before next retry. Retry attempt 1
  3. 11:58:54 - Error: [ProductClient.GetAsync]: No connection could be made because the target machine actively refused it
  4. 11:58:54 - Request failed. Waiting 00:00:02 before next retry. Retry attempt 2
  5. 11:58:56 - Error: [ProductClient.GetAsync]: No connection could be made because the target machine actively refused it
  6. 11:58:56 - Request failed. Waiting 00:00:02 before next retry. Retry attempt 3
  7. 11:58:56 - Failed! Circuit open, waiting: 00:00:30
  8. 11:58:56 - Fallback method used due to: The circuit is now open and is not allowing calls.
  • Until 30 seconds, if you request again the logs will be the same and the service will not call the WebApi http://localhost:5880/api/products/:productId
  • After 30 seconds, if you request again, the circuit state will be half open and will try to request again

Logs:

  1. 12:36:02 - Circuit is half open.
  2. 12:36:04 - Error: [ProductClient.GetAsync]: No connection could be made because the target machine actively refused it
  3. 12:36:04 - Failed! Circuit open, waiting: 00:00:30
  4. 12:36:04 - Request failed. Waiting 00:00:02 before next retry. Retry attempt 1
  5. 12:36:06 - Request failed. Waiting 00:00:02 before next retry. Retry attempt 1
  6. 12:36:08 - Request failed. Waiting 00:00:02 before next retry. Retry attempt 1
  7. 12:36:10 - Fallback method used due to: The circuit is now open and is not allowing calls.
  • When WebApi is back, the circuit is reseted and the service works properly and return expected result

Logs:

  1. 12:40:10 - Circuit is reset.

Running with API

With Powershell run dotnet run into WebApi folder.

  1. Now listening on: http://localhost:5880
  2. Application started. Press Ctrl+C to shut down.

Using a new instance of Powershell, run dotnet run into ResilienceSample folder.

  1. Now listening on: http://localhost:5001

Request http://localhost:5001/api/values on browser and it will return the expect result from http://localhost:5880/api/products/:productId

  1. {
  2. "id": 123456,
  3. "name": "Product"
  4. }