.NET使用YARP通過(guò)編碼方式配置域名轉(zhuǎn)發(fā)實(shí)現(xiàn)反向代理
前面介紹了 YARP 通過(guò)配置文件的方式配置代理轉(zhuǎn)發(fā)(傳送門),而眾所周知,微軟的一貫作風(fēng)就是能通過(guò)配置文件做的事情,通過(guò)編碼的方式也能實(shí)現(xiàn)!YARP 也不例外,廢話不多說(shuō),直接上代碼!
首先,參照官方文檔,我們先新建一個(gè) InMemoryConfigProvider 類,并且繼承 IProxyConfigProvider 接口,類里面還包含了一個(gè) IProxyConfig 的類,別看漏了噢!
這里多嘴一下,下面的代碼出現(xiàn)了 volatile 關(guān)鍵字,介紹一下它:volatile 是 C# 中用于控制同步的關(guān)鍵字,其意義是針對(duì)程序中一些敏感數(shù)據(jù),不允許多線程同時(shí)訪問(wèn),保證數(shù)據(jù)在任何訪問(wèn)時(shí)刻,最多有一個(gè)線程訪問(wèn),以保證數(shù)據(jù)的完整性,volatile 是修飾變量的修飾符。
public class InMemoryConfigProvider : IProxyConfigProvider
{
private volatile InMemoryConfig _config;
public InMemoryConfigProvider(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters)
{
_config = new InMemoryConfig(routes, clusters);
}
public IProxyConfig GetConfig() => _config;
public void Update(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters)
{
var oldConfig = _config;
_config = new InMemoryConfig(routes, clusters);
oldConfig.SignalChange();
}
private class InMemoryConfig : IProxyConfig
{
private readonly CancellationTokenSource _cts = new();
public InMemoryConfig(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters)
{
Routes = routes;
Clusters = clusters;
ChangeToken = new CancellationChangeToken(_cts.Token);
}
public IReadOnlyList<RouteConfig> Routes { get; }
public IReadOnlyList<ClusterConfig> Clusters { get; }
public IChangeToken ChangeToken { get; }
internal void SignalChange()
{
_cts.Cancel();
}
}
}然后添加一個(gè)擴(kuò)展 InMemoryConfigProviderExtensions
public static class InMemoryConfigProviderExtensions
{
public static IReverseProxyBuilder LoadFromMemory(this IReverseProxyBuilder builder, IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters)
{
builder.Services.AddSingleton<IProxyConfigProvider>(new InMemoryConfigProvider(routes, clusters));
return builder;
}
}接下來(lái)就是寫(xiě)配置了,我個(gè)人還是喜歡在配置文件中寫(xiě),但是有動(dòng)態(tài)配置需求的話,又不想登錄服務(wù)器編輯 appsetting 文件,通過(guò)編碼的方式確實(shí)更為方便,將配置寫(xiě)進(jìn)庫(kù)或者其它存儲(chǔ)方式里面,那將是隨心所欲??!上代碼:
Program.cs
var routes = new[]
{
new RouteConfig()
{
RouteId = "admin",
ClusterId = "admin",
Match = new RouteMatch
{
Hosts = new string[] {"test1.ysmc.net.cn" },
Path = "{**catch-all}"
}
},
new RouteConfig()
{
RouteId = "blazor",
ClusterId = "blazor",
Match = new RouteMatch
{
Hosts = new string[] {"test2.ysmc.net.cn" },
Path = "{**catch-all}"
}
}
};
var clusters = new[]
{
new ClusterConfig()
{
ClusterId = "admin",
LoadBalancingPolicy = "RoundRobin",
Destinations = new Dictionary<string, DestinationConfig>(StringComparer.OrdinalIgnoreCase)
{
{ "admin", new DestinationConfig() { Address = "https://admin.blazor.zone" } }
}
},
new ClusterConfig()
{
ClusterId = "blazor",
LoadBalancingPolicy = "RoundRobin",
Destinations = new Dictionary<string, DestinationConfig>(StringComparer.OrdinalIgnoreCase)
{
{ "blazor", new DestinationConfig() { Address = "https://www.blazor.zone" } }
}
}
};
builder.Services.AddReverseProxy().LoadFromMemory(routes, clusters);上面的配置代碼,跟配置文件方式的節(jié)點(diǎn)和屬性,都是對(duì)應(yīng)的,照著寫(xiě)就是了
"ReverseProxy": {
"Routes": {
"admin": {
"ClusterId": "admin",
"Match": {
"Hosts": [ "test1.ysmc.net.cn" ],
"Path": "{**catch-all}"
}
},
"blazor": {
"ClusterId": "blazor",
"Match": {
"Hosts": [ "test2.ysmc.net.cn" ],
"Path": "{**catch-all}"
}
}
},
"Clusters": {
"admin": {
"LoadBalancingPolicy": "RoundRobin",
"Destinations": {
"admin": {
"Address": "https://admin.blazor.zone/"
}
}
},
"blazor": {
"LoadBalancingPolicy": "RoundRobin",
"Destinations": {
"blazor": {
"Address": "https://www.blazor.zone/"
}
}
}
}
}最終效果還是依舊的完美,感謝大佬的觀看,謝謝!

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
淺談.NET反射機(jī)制的性能優(yōu)化 附實(shí)例下載
在進(jìn)入解釋型模版引擎的探討之前,我決定先分享一下這篇博客。因?yàn)樵诮忉屝鸵胬飳?huì)引入反射的概念來(lái)實(shí)現(xiàn)更多、更復(fù)雜的功能2012-04-04
asp.net下讓Gridview鼠標(biāo)滑過(guò)光棒變色效果
Gridview光棒效果 鼠標(biāo)滑過(guò)2010-07-07
asp.net 刪除MFC單文檔默認(rèn)菜單欄的兩種方法
新建一個(gè)MFC單文檔程序,默認(rèn)都有四個(gè)菜單欄:文件、編輯、視圖和幫助。怎么把這四個(gè)菜單欄刪除掉呢?2010-03-03
通過(guò)Web Service實(shí)現(xiàn)IP地址查詢功能的示例
下面小編就為大家分享一篇通過(guò)Web Service實(shí)現(xiàn)IP地址查詢功能的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
ASP.NET實(shí)現(xiàn)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了ASP.NET實(shí)現(xiàn)簡(jiǎn)單的進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
ASP.Net開(kāi)發(fā)常見(jiàn)的一些問(wèn)題總結(jié)
ASP.Net開(kāi)發(fā)常見(jiàn)的一些問(wèn)題總結(jié),需要的朋友可以參考一下2013-02-02

