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

  • 相关阅读:
    看完这篇,所有JAVA并发问题你都能应对自如
    vlcplayer for android 源码编译log打印
    小白学java进阶工程师路线图
    某房产网站登录RSA加密分析
    Xorm 使用手册,面向工作学习
    超详细 | 实验室linux服务器非root账号 | 安装pip | 安装conda
    windows docker 容器启动报错:Ports are not available
    JavaScript大作业(餐厅美食网站设计与实现)
    uniapp学习笔记 真机运行遇到的问题
    外汇天眼:店大欺客!capital.com能盈利竟全靠封锁账户鲸吞资产?
  • 原文地址:https://blog.csdn.net/xie__jin__cheng/article/details/136181784