亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

.NET使用YARP通過編碼方式配置域名轉(zhuǎn)發(fā)實現(xiàn)反向代理

 更新時間:2022年09月24日 10:50:06   作者:一事冇誠  
這篇文章介紹了.NET使用YARP通過編碼方式配置域名轉(zhuǎn)發(fā)實現(xiàn)反向代理的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

前面介紹了 YARP 通過配置文件的方式配置代理轉(zhuǎn)發(fā)(傳送門),而眾所周知,微軟的一貫作風(fēng)就是能通過配置文件做的事情,通過編碼的方式也能實現(xiàn)!YARP 也不例外,廢話不多說,直接上代碼!

首先,參照官方文檔,我們先新建一個 InMemoryConfigProvider 類,并且繼承 IProxyConfigProvider 接口,類里面還包含了一個  IProxyConfig 的類,別看漏了噢!

這里多嘴一下,下面的代碼出現(xiàn)了 volatile 關(guān)鍵字,介紹一下它:volatile 是 C# 中用于控制同步的關(guān)鍵字,其意義是針對程序中一些敏感數(shù)據(jù),不允許多線程同時訪問,保證數(shù)據(jù)在任何訪問時刻,最多有一個線程訪問,以保證數(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();
        }
    }
}

然后添加一個擴展 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;
    }
}

接下來就是寫配置了,我個人還是喜歡在配置文件中寫,但是有動態(tài)配置需求的話,又不想登錄服務(wù)器編輯 appsetting 文件,通過編碼的方式確實更為方便,將配置寫進庫或者其它存儲方式里面,那將是隨心所欲?。∩洗a:

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é)點和屬性,都是對應(yīng)的,照著寫就是了

"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/"
        }
      }
    }
  }
}

最終效果還是依舊的完美,感謝大佬的觀看,謝謝!

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

最新評論