• .net 微服务 服务保护 自动重试 Polly


    1. 概要

    实验服务保护,自动重新连接功能。

    2.代码

    2.1 重复工具 

    1. using Polly;
    2. using Polly.Retry;
    3. using System;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6. using System.Net.Http;
    7. using System.Threading.Tasks;
    8. namespace WebApplication2
    9. {
    10. public class ClientPolicy
    11. {
    12. public AsyncRetryPolicy<HttpResponseMessage> asyncRetryPolicy { get; set; }
    13. public ClientPolicy()
    14. {
    15. asyncRetryPolicy = Policy.HandleResult<HttpResponseMessage>(p=>!p.IsSuccessStatusCode).WaitAndRetryAsync(5,retryAttemp=>TimeSpan.FromSeconds(Math.Pow(2,retryAttemp)));
    16. }
    17. }
    18. }

    2.2 调用位置

    1. using Microsoft.AspNetCore.Mvc;
    2. using Microsoft.Extensions.Logging;
    3. using System;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6. using System.Net.Http;
    7. using System.Threading.Tasks;
    8. namespace WebApplication2.Controllers
    9. {
    10. [ApiController]
    11. [Route("[controller]")]
    12. public class WeatherForecastController : ControllerBase
    13. {
    14. private static readonly string[] Summaries = new[]
    15. {
    16. "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    17. };
    18. private readonly ILogger<WeatherForecastController> _logger;
    19. public WeatherForecastController(ILogger<WeatherForecastController> logger)
    20. {
    21. _logger = logger;
    22. }
    23. [HttpGet]
    24. public IEnumerable<WeatherForecast> Get()
    25. {
    26. var rng = new Random();
    27. ClientPolicy clientPolicy = new ClientPolicy();
    28. HttpClient httpClient = new HttpClient();
    29. clientPolicy.asyncRetryPolicy.ExecuteAsync(() => httpClient.GetAsync($"https://localhost:44367/test"));
    30. return Enumerable.Range(1, 5).Select(index => new WeatherForecast
    31. {
    32. Date = DateTime.Now.AddDays(index),
    33. TemperatureC = rng.Next(-20, 55),
    34. Summary = Summaries[rng.Next(Summaries.Length)]
    35. })
    36. .ToArray();
    37. }
    38. [HttpGet("/test")]
    39. public IActionResult test()
    40. {
    41. var randomNumber = new Random().Next(1, 100);
    42. if(randomNumber > 20)
    43. {
    44. //Console.WriteLine("请求成功 200");
    45. //return Ok("请求成功");
    46. }
    47. Console.WriteLine("请求失败");
    48. return BadRequest("请求失败");
    49. }
    50. }
    51. }

    2.实验结果

    如果失败下面的函数会重复调用5次

    1. [HttpGet("/test")]
    2. public IActionResult test()
    3. {
    4. var randomNumber = new Random().Next(1, 100);
    5. if(randomNumber > 20)
    6. {
    7. //Console.WriteLine("请求成功 200");
    8. //return Ok("请求成功");
    9. }
    10. Console.WriteLine("请求失败");
    11. return BadRequest("请求失败");
    12. }

  • 相关阅读:
    C++ 11
    店外营销吸睛,店内体验升级丨餐饮品牌如何「吃」透数据?
    黑客帝国代码雨
    如何设置微信自动回复?教你快速上手!
    恶补 Java 基础
    Java实现Excel导入和导出,看这一篇就够了(珍藏版2.0)
    .NET 跨平台应用开发动手教程 |用 Uno Platform 构建一个 Kanban-style Todo App
    【华为OD机试真题 JAVA】靠谱的车
    【金九银十必问面试题】站在架构师角度分析问题,如何解决TCC中的悬挂问题
    山东企业ITSS认证条件
  • 原文地址:https://blog.csdn.net/xie__jin__cheng/article/details/136181784