CondenserDotNet - 使用 Kestrel 和 Consul 的 API 反向代理库!
簡(jiǎn)介
CondenserDotNet - 使用 Kestrel 和 Consul 的 API 反向代理庫(kù)!
特點(diǎn)
?Consul 客戶(hù)端庫(kù),包括服務(wù)注冊(cè)、發(fā)現(xiàn)和配置?反向代理?交互式 UI,用于查看有關(guān)代理的統(tǒng)計(jì)信息
配置示例
配置遵循 LIFO 策略,以最后注冊(cè)的配置為準(zhǔn)
var?configValue?=?configRegistry["MyKey"];如果密鑰不存在,它將引發(fā)異常。您也可以使用普通的 tryget 模式訪(fǎng)問(wèn)它
if(configRegistry.TryGetValue("MyKey",?out?string?configValue)) {//do?something?with?the?key }靜態(tài)配置
要注冊(cè)一組靜態(tài)鍵,請(qǐng)使用以下代碼
var?result?=?await?configRegistry.AddStaticKeyPathAsync("my/keys/path");動(dòng)態(tài)配置
可以注冊(cè)動(dòng)態(tài)配置的關(guān)鍵路徑,代碼如下
var?registry?=?new?ConsulRegistry();await?registry.AddUpdatingPathAsync("org/test5/");還可以注冊(cè)回調(diào)以提醒您是否更新了特定鍵或配置中的任何鍵是否已更新。
var?singleCallBack?=?registry.AddWatchOnSingleKey("test1",?()?=>?Console.Writeline("Key?Changed!");如果您想要任何更新來(lái)觸發(fā)您的回調(diào),只需執(zhí)行
var?multipleCallBack?=?registry.AddWatchOnEntireConfig(()?=>?Console.Writeline("Some?key?changed,?or?multiple?keys?changed,?or?maybe?none?");請(qǐng)求服務(wù)
獲取要連接的服務(wù)實(shí)例的示例
var?serviceInstance?=?await?serviceRegistry.GetServiceInstanceAsync("ServiceLookup"); if(serviceInsance?==?null) {//you?need?to?handle?no?service?instance?available } //connect?to?service?via?your?method var?serverUrl?=?$"http://{serviceInstance.Address}:{serviceInsance.Port}";//your?method?for?calling?servers服務(wù)端口
在動(dòng)態(tài)端口上配置 Kestrel 的示例
var?host?=?new?WebHostBuilder().UseKestrel().UseUrls($"http://*:{ServiceManagerOptions.GetNextAvailablePort()}").UseStartup<Startup>().Build();host.Run();服務(wù)注冊(cè)
在動(dòng)態(tài)端口上配置 Kestrel 的示例
var?host?=?new?WebHostBuilder().UseKestrel().UseUrls($"http://*:{serviceManager.ServicePort}").UseStartup<Startup>().Build();host.Run();Github地址
https://github.com/Drawaes/CondenserDotNet
最后大家如果喜歡我的文章,還麻煩給個(gè)關(guān)注并點(diǎn)個(gè)贊, 希望net生態(tài)圈越來(lái)越好!
總結(jié)
以上是生活随笔為你收集整理的CondenserDotNet - 使用 Kestrel 和 Consul 的 API 反向代理库!的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 我将 20 年前开发的操作系统迁移到 .
- 下一篇: 如何使用GeneralUpdte构建客户