当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net core 使用HttpClientFactory Polly实现熔断降级

asp.net core 使用HttpClientFactory Polly实现熔断降级

2019年09月19日  | 移动技术网IT编程  | 我要评论

黄美珍图片,重说中国近代史,梦幻西游伏魔录1.1

前言

net core2.1后也是增加更新了很多东西,当然httpclientfactory更新中的一部分.虽然说httpclient这个实现了disposable,但使用它的时候用using包装块的方式通常不是最好的选择。处理httpclient,底层socket套接字不会立即释放。该httpclient类是未多个请求重复使用而创建的。需要不同的基地址,不同的http 标头和其他对请求个性化操作的场景时,需要动手管理多个httpclient实例,为了简化httpclient实例管理,.net core 2.1提供了一个新的httpclientfactory - 它可以创建,缓存和处理httpclient实例。

什么是httpclientfactory

aspnet core开始,pollyihttpclientfastory集成。httpclientfastory是一个简化管理和使用的httpclientory。用asp.net团队的话说:“an opinionated factory for creating httpclient instances”(一个用于创建httpclient实例的最佳实践的工厂

  • 提供命名和配置逻辑httpclient 对象的中心位置。例如,您可以配置预先配置为访问特定微服务的客户端(服务代理)。
  • 通过委派处理程序httpclient 并实施基于polly 的中间件来利用polly 的弹性策略,对传出中间件的概念进行编码。
  • httpclient 已经有了委托处理程序的概念,这些处理程序可以链接在一起用于传出http 请求。您将http 客户端注册到工厂中,并且可以使用polly处理程序将polly策略用于retrycircuitbreakers 等。
  • 管理生命周期,httpclientmessagehandlers 以避免在管理httpclient 自己的生命周期时可能发生的上述问题/问题。

httpclientfactory简单使用

  • startup添加
services.addhttpclient();
  • 通过ihttpclientfactory创建一个httpclient对象,后面操作如旧,但是不需要关心其资源释放
using microsoft.aspnetcore.mvc;
using system.net.http;
using system.threading.tasks;

namespace httpclientfactorypolly.controllers
{
    [route("api/[controller]")]
    [apicontroller]
    public class valuescontroller : controllerbase
    {
        private readonly ihttpclientfactory _httpclientfactory;
        public valuescontroller(ihttpclientfactory httpclientfactory)
        {
            this._httpclientfactory = httpclientfactory;
        }

        // get api/values
        [httpget]
        public async task<actionresult<string>> get()
        {
            var client = _httpclientfactory.createclient();
            var result =await client.getstringasync("https://www.microsoft.com/zh-cn/");
            return result;
        }


    }
}

配置httpclientfactory polly

这边采用命名客户端演示该栗子(如果应用需要有许多不同的 httpclient 用法(每种用法的配置都不同),可以视情况使用命名客户端。 可以在 httpclient 中注册时指定命名 startup.configureservices 的配置。)

  • package
pm> install-package microsoft.extensions.http.polly

startup

   services.addhttpclient("github",c=> {
                //基址
                c.baseaddress = new system.uri("https://api.github.com/");
                // github api versioning
                c.defaultrequestheaders.add("accept", "application/vnd.github.v3+json");
                // github requires a user-agent
                c.defaultrequestheaders.add("user-agent", "httpclientfactory-sample");
            });
[httpget("{id}")]
public async task<actionresult<string>> get(int id)
{
        var request = new httprequestmessage(httpmethod.get,
       "repos/aspnet/docs/pulls");

        var client = _httpclientfactory.createclient("github");

        var response = await client.sendasync(request);
        var result =await response.content.readasstringasync();
        return result;
}
  • 重试机制
services.addhttpclient("github", c =>
    {
        //基址
        c.baseaddress = new system.uri("https://api.github.com/");
        // github api versioning
        c.defaultrequestheaders.add("accept", "application/vnd.github.v3+json");
        // github requires a user-agent
       c.defaultrequestheaders.add("user-agent", "httpclientfactory-sample");
        //addtransienthttperrorpolicy主要是处理http请求的错误,如http 5xx 的状态码,http 408 的状态码 以及system.net.http.httprequestexception异常
        }).addtransienthttperrorpolicy(p =>
        //waitandretryasync参数的意思是:每次重试时等待的睡眠持续时间。
    p.waitandretryasync(3, _ => timespan.frommilliseconds(600)));

效果如下

  • 熔断降级超时
services.addhttpclient("test", c =>
  {
    //基址
    c.baseaddress = new system.uri("http://localhost:5000/");
    // github api versioning
    c.defaultrequestheaders.add("accept", "application/vnd.github.v3+json");
    // github requires a user-agent
    c.defaultrequestheaders.add("user-agent", "httpclientfactory-sample");
    })
     // 降级
    .addpolicyhandler(policy<httpresponsemessage>.handle<exception>().fallbackasync(fallbackresponse, async b =>
    {
        console.writeline($"fallback here {b.exception.message}");
    }))
    // 熔断
    .addpolicyhandler(policy<httpresponsemessage>.handle<exception>().circuitbreakerasync(2, timespan.fromseconds(4), (ex, ts) =>
    {
        console.writeline($"break here {ts.totalmilliseconds}");
    }, () =>
    {
        console.writeline($"reset here ");
    }))
    // 超时
    .addpolicyhandler(policy.timeoutasync<httpresponsemessage>(1));

    }

设置降级策略当出现任何异常返回fallback

设置熔断策略当连续出现异常异常 2 次,熔断 4s;

设置超时策略,请求超时为 1s,超时默认会抛出 timeoutrejectedexception;

效果如下

概要

示例地址:https://github.com/fhcodegit/httpclientfactorypolly
polly:https://github.com/app-vnext/polly
参考:

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网