WebApiClient鐨凷teeltoeOSS.Discovery鎵╁睍

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebApiClient鐨凷teeltoeOSS.Discovery鎵╁睍相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e9%85%8d%e7%bd%ae%e5%88%b0' title='閰嶇疆鍒?>閰嶇疆鍒?/a>   core   cto   values   message   nta   dex   鑷姩绠$悊   

1 鑳屾櫙

浠庡洯瀛愰噷鐪嬪埌涓€浜涙湅鍙嬪湪鏌愪簺椤圭洰寮€鍙戜腑锛岄€夋嫨鐨勬灦鏋勬槸spring cloud鎼缓搴曞眰寰湇鍔℃鏋讹紝dotnet core鏉ョ紪鍐欎笟鍔¢€昏緫锛?a href="https://github.com/SteeltoeOSS/Discovery">SteeltoeOSS.Discovery鏄痙otnet鍜宻pingcloud鐨勬ˉ姊侊紝涓篸otnet鎻愪緵鏈嶅姟娉ㄥ唽鍜屾湇鍔″彂鐜扮浉鍏冲姛鑳姐€傚湪闃呰鏈嬪弸浠枃绔犵殑鏃跺€欙紝鎴戝彂鐜扮浉鍏充唬鐮侀噷鐨勪竴浜汬ttpClient鐩稿叧闂锛屽悓鏃跺dotnet瀵勫眳浜巗pingcloud涓嬬敱浜庢病鏈塅eign鑰屼骇鐢熺殑閭d簺涓戦檵鐨刪ttp璇锋眰浠g爜杩涜鎬濊€冦€傛湰鏂囧皢鍥寸粫鍘熺敓鐨凥ttpClient鐨勫垱寤轰笌閲婃斁鐨勬纭Э鍔垮拰浣跨敤WebApiClient璁ヾotnet涔熸湁濯茬編Feign鐨勬湇鍔″鎴风涓や釜闈㈠睍寮€銆?/p>

2 姝g‘浣跨敤HttpClient

2.1 HttpClient鐨勫垱寤哄拰閲婃斁

HttpClient鏈変笁涓瀯閫犲嚱鏁帮紝鏈€缁堥兘鏄皟鐢ㄥ埌public HttpClient(HttpMessageHandler handler, bool disposeHandler)杩欎釜鍑芥暟锛孒ttpClient闄や簡鍏秇andler鍙傛暟涔嬪锛屾湰韬病鏈変娇鐢ㄥ埌闇€瑕丏ispose鐨勮祫婧愶紝鍏跺疄鐜扮殑IDispose涔熸槸涓轰簡Dispose鎺塰andler鍙傛暟鑰屽凡銆?/p>

HttpMessageHandler鏄竴涓娊璞$被锛岀洰鍓嶄富瑕佺殑HttpMessageHandler鍏蜂綋绫诲瀷鏈塇ttpClientHanlder銆丼ocketsHttpHandler鍜學ebRequestHandler锛屼絾HttpClientHanlder鍦╠otnet core2.1涓嬫槸瀵瑰拰SocketsHttpHandler鐨勫寘瑁呭疄鐜般€傞櫎浜嗚繖浜涗富瑕丠ttpMessageHandler锛岃繕鏈変竴涓娊璞$殑DelegatingHandler绫诲瀷锛岀敤浜庡疄鐜拌姹傜閬擄紝褰卞搷璇锋眰鍓嶅悗鐨勬暟鎹€昏緫銆侶ttpClient鐨勯粯璁ゆ瀯閫犲櫒锛屼娇鐢ㄤ簡HttpClientHanlder绫诲瀷锛屽悓鏃禿isposeHandler涓簍rue锛岃繖鏃跺鏋滃HttpClient瀹炰緥Dispose浜嗭紝鍏跺唴閮ㄧ殑HttpClientHanlder鑷劧涔熻Dispose浜嗭紝

姝g‘鐨勫垱寤哄拰閲婃斁HttpClient渚嬪瓙

  1. 榛樿鏋勯€犲櫒
var httpClient = new HttpClient();
...浣犵殑浠g爜...
httpClient.Dispose();
  1. HttpClient鎺у埗HttpMessageHandler
var handler = new HttpClientHandler();
var httpClient = new HttpClient(handler, true);
...浣犵殑浠g爜...
httpClient.Dispose();
  1. HttpClient涓嶆帶鍒跺閮℉ttpMessageHandler
var handler = 浠庡閮ㄦ潵鐨凥ttpMessageHandler;
var httpClient = new HttpClient(handler, false);
...浣犵殑浠g爜...

// 杩欓噷璋冪敤httpClient.Dispose()鏄棤鏁堢殑
// handler鐨勭敓鍛藉懆鏈熷簲璇ョ敱瀹冪殑鍒涘缓鑰呮潵缁存姢
// 濡傛灉杩欓噷Dispose鎺塰andler锛屽叾瀹冧娇鐢ㄤ簡杩欎釜handler鐨凥ttpClient瀹炰緥鍙楀奖鍝?/code>

涓嶆纭殑鍒涘缓鍜岄噴鏀綡ttpClient鐨勪緥瀛?/strong>

  1. HttpMessageHandler琚垱寤轰簡锛屼絾娌℃湁閲婃斁
private readonly DiscoveryHttpClientHandler _handler;
private const string ProductUrl = "http://product/api/values";

public ValuesController(IDiscoveryClient client, ILoggerFactory logFactory)
{
    _handler = new DiscoveryHttpClientHandler(client);
}

[HttpGet("product")]
public async Task<string> GoProductAsync()
{
    var client = new HttpClient(_handler, false);
    return await client.GetStringAsync(ProductUrl);
}

HttpClientFactory椤圭洰锛岀敤浜庢彁渚汬ttpClient鐨勫垱寤哄拰鐢熷懡鍛ㄦ湡鑷姩绠$悊锛屽畬缇庤В鍐冲埌搴曢€夋嫨鍗曚緥杩樻槸姣忎釜璇锋眰鍒涘缓鍜岄噴鏀綡ttpClient杩欎釜宸﹀彸闅句负鐨勯棶棰樸€傛墍浠ュ湪asp.net core椤圭洰寮€鍙戜腑锛岃鍒啀鍐欐墜鍔╪ew HttpClient浜嗭紝鎵€鏈塇ttpClient鐨勫疄渚嬶紝閮借鐢盚ttpClientFactory鏉ュ垱寤猴紝鎵€鏈夌殑澶栭儴HttpMessageHandler锛屼篃搴旇閰嶇疆鍒癏ttpClientFactory锛岃瀹冧笌HttpClient鍏宠仈璧锋潵銆?/p>

HttpClientFactory鏈変笁绉嶄娇鐢ㄦ柟寮忥細

  1. Using HttpClientFactory Directly
  2. Named Clients
  3. Typed Clients

鍏蜂綋鐨勪娇鐢紝鍙互鏌ョ湅3 ways to use HTTPClientFactory in ASP.NET Core 2.1杩欑瘒濂芥枃銆?/p>

3 瀵勫眳涓嬩篃鏈塅eign

铏界劧宸茬粡璁茶В浜嗘€庝箞new涓€涓狧ttpClient锛屾€庝箞鍒╃敤HttpClientFactory锛屼絾濡傛灉瑕佸瘎灞呭湪spingcloud涓嬶紝浣犺繕鏄緱涓鸿姹備竴涓湇鍔℃帴鍙g紪鍐欏ぇ閲忕殑浠g爜锛岃繖鍦╦ava鐨凢eign鍓嶉潰濡傚悓椹溅瑙佸埌瀹濋┈銆傚鏋滆兘鍒╃敤WebApiClient杩欐妸鍒╁墤锛岀粨鍚圚ttpClientFactory涓嶥iscoveryHttpClientHandler锛屼綘涔熻兘鍙樻垚瀹濋┈銆傚皢杩欎笁鑰呮湁鏈虹粨鍚堣捣鏉ョ殑椤圭洰锛屽彨WebApiClient.Extensions.DiscoveryClient锛屽畠鏄疻ebApiClient鐨凷teeltoeOSS.Discovery鎵╁睍椤圭洰锛屼娇鐢ㄩ潪甯哥畝鍗曘€?/p>

3.1 Nuget寮曠敤

PM> install-package WebApiClient.Extensions.DiscoveryClient

3.2 澹版槑寰湇鍔$殑WebApiClient璋冪敤鎺ュ彛

[HttpHost("http://NET-API")]
public interface INetApi : IHttpApi
{
    [HttpGet("api/values")]
    ITask<string[]> GetValuesAsync();

    [HttpGet("api/values/{id}")]
    ITask<string> GetValuesAsync(int id);
}

3.3 Startup鐩稿叧閰嶇疆

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddDiscoveryClient(Configuration);
    services.AddDiscoveryTypedClient<INetApi>();
    ...
}


// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    ...
    app.UseDiscoveryClient();
}
3.4 Controller
public class HomeController : Controller
{
    public async Task<string> Index([FromServices]INetApi netApi, int id = 0)
    {
        var values = await netApi.GetValuesAsync();
        var value = await netApi.GetValuesAsync(id);
        return "ok";
    }
}

杂谈WebApiClient的性能优化

swagger文档转换为WebApiClient声明式代码

聊一聊声明式接口调用与Nacos的结合使用

C# - POST 请求中的正文内容

c# webclient.downloadData的问题

webclient buffer异常

(c)2006-2024 SYSTEM All Rights Reserved IT常识