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渚嬪瓙
- 榛樿鏋勯€犲櫒
var httpClient = new HttpClient();
...浣犵殑浠g爜...
httpClient.Dispose();
- HttpClient鎺у埗HttpMessageHandler
var handler = new HttpClientHandler();
var httpClient = new HttpClient(handler, true);
...浣犵殑浠g爜...
httpClient.Dispose();
- HttpClient涓嶆帶鍒跺閮℉ttpMessageHandler
var handler = 浠庡閮ㄦ潵鐨凥ttpMessageHandler;
var httpClient = new HttpClient(handler, false);
...浣犵殑浠g爜...
// 杩欓噷璋冪敤httpClient.Dispose()鏄棤鏁堢殑
// handler鐨勭敓鍛藉懆鏈熷簲璇ョ敱瀹冪殑鍒涘缓鑰呮潵缁存姢
// 濡傛灉杩欓噷Dispose鎺塰andler锛屽叾瀹冧娇鐢ㄤ簡杩欎釜handler鐨凥ttpClient瀹炰緥鍙楀奖鍝?/code>
涓嶆纭殑鍒涘缓鍜岄噴鏀綡ttpClient鐨勪緥瀛?/strong>
- 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鏈変笁绉嶄娇鐢ㄦ柟寮忥細
- Using HttpClientFactory Directly
- Named Clients
- 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";
}
}