• .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. }

  • 相关阅读:
    Spring常见问题解决 - AOP调用被拦截类的属性报NPE
    Centos7 ElasticSearch集群搭建
    xxxxx
    VMware替换难?听听ZStack 的这3家制造业客户怎么说……
    FreeRtos于嵌入式环境的应用
    计算机竞赛 目标检测-行人车辆检测流量计数
    计算机毕业设计(附源码)python在线学习交流平台
    Qt音视频开发01-共享解码线程(耗时一年/性能凶残/至臻完美)
    javaScript编译器,Babel详解!
    力扣刷题-字符串-反转字符串Ⅱ
  • 原文地址:https://blog.csdn.net/xie__jin__cheng/article/details/136181784