.NET 反向代理 YARP 通過編碼方式設定域名轉發

2022-09-24 09:00:35

  前面介紹了 YARP 通過組態檔的方式設定代理轉發(傳送門),而眾所周知,微軟的一貫作風就是能通過組態檔做的事情,通過編碼的方式也能實現!YARP 也不例外,廢話不多說,直接上程式碼!

  首先,參照官方檔案,我們先新建一個 InMemoryConfigProvider 類,並且繼承 IProxyConfigProvider 介面,類裡面還包含了一個  IProxyConfig 的類,別看漏了噢!

  這裡多嘴一下,下面的程式碼出現了 volatile 關鍵字,介紹一下它:volatile 是 C# 中用於控制同步的關鍵字,其意義是針對程式中一些敏感資料,不允許多執行緒同時存取,保證資料在任何存取時刻,最多有一個執行緒存取,以保證資料的完整性,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;
    }
}

 

  接下來就是寫設定了,我個人還是喜歡在組態檔中寫,但是有動態設定需求的話,又不想登入伺服器編輯 appsetting 檔案,通過編碼的方式確實更為方便,將設定寫進庫或者其它儲存方式裡面,那將是隨心所欲啊!上程式碼:

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);

  上面的設定程式碼,跟組態檔方式的節點和屬性,都是對應的,照著寫就是了

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

 

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

 原文連結:https://www.cnblogs.com/ysmc/p/16724763.html