.NET 反向代理 YARP 通过编码方式配置域名转发

打印 上一主题 下一主题

主题 873|帖子 873|积分 2619

  前面介绍了 YARP 通过配置文件的方式配置代理转发(传送门),而众所周知,微软的一贯作风就是能通过配置文件做的事情,通过编码的方式也能实现!YARP 也不例外,废话不多说,直接上代码!
  首先,参照官方文档,我们先新建一个 InMemoryConfigProvider 类,并且继承 IProxyConfigProvider 接口,类里面还包含了一个  IProxyConfig 的类,别看漏了噢!
  这里多嘴一下,下面的代码出现了 volatile 关键字,介绍一下它:volatile 是 C# 中用于控制同步的关键字,其意义是针对程序中一些敏感数据,不允许多线程同时访问,保证数据在任何访问时刻,最多有一个线程访问,以保证数据的完整性,volatile 是修饰变量的修饰符。
  1. public class InMemoryConfigProvider : IProxyConfigProvider
  2. {
  3.     private volatile InMemoryConfig _config;
  4.     public InMemoryConfigProvider(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters)
  5.     {
  6.         _config = new InMemoryConfig(routes, clusters);
  7.     }
  8.     public IProxyConfig GetConfig() => _config;
  9.     public void Update(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters)
  10.     {
  11.         var oldConfig = _config;
  12.         _config = new InMemoryConfig(routes, clusters);
  13.         oldConfig.SignalChange();
  14.     }
  15.     private class InMemoryConfig : IProxyConfig
  16.     {
  17.         private readonly CancellationTokenSource _cts = new();
  18.         public InMemoryConfig(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters)
  19.         {
  20.             Routes = routes;
  21.             Clusters = clusters;
  22.             ChangeToken = new CancellationChangeToken(_cts.Token);
  23.         }
  24.         public IReadOnlyList<RouteConfig> Routes { get; }
  25.         public IReadOnlyList<ClusterConfig> Clusters { get; }
  26.         public IChangeToken ChangeToken { get; }
  27.         internal void SignalChange()
  28.         {
  29.             _cts.Cancel();
  30.         }
  31.     }
  32. }
复制代码
 
  然后添加一个扩展 InMemoryConfigProviderExtensions
  1. public static class InMemoryConfigProviderExtensions
  2. {
  3.     public static IReverseProxyBuilder LoadFromMemory(this IReverseProxyBuilder builder, IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters)
  4.     {
  5.         builder.Services.AddSingleton<IProxyConfigProvider>(new InMemoryConfigProvider(routes, clusters));
  6.         return builder;
  7.     }
  8. }
复制代码
 
  接下来就是写配置了,我个人还是喜欢在配置文件中写,但是有动态配置需求的话,又不想登录服务器编辑 appsetting 文件,通过编码的方式确实更为方便,将配置写进库或者其它存储方式里面,那将是随心所欲啊!上代码:
Program.cs
  1. var routes = new[]
  2. {
  3.     new RouteConfig()
  4.     {
  5.         RouteId = "admin",
  6.         ClusterId = "admin",
  7.         Match = new RouteMatch
  8.         {
  9.             Hosts = new string[] {"test1.ysmc.net.cn" },
  10.             Path = "{**catch-all}"
  11.         }
  12.     },
  13.     new RouteConfig()
  14.     {
  15.         RouteId = "blazor",
  16.         ClusterId = "blazor",
  17.         Match = new RouteMatch
  18.         {
  19.             Hosts = new string[] {"test2.ysmc.net.cn" },
  20.             Path = "{**catch-all}"
  21.         }
  22.     }
  23. };
  24. var clusters = new[]
  25. {
  26.     new ClusterConfig()
  27.     {
  28.         ClusterId = "admin",
  29.         LoadBalancingPolicy = "RoundRobin",
  30.         Destinations = new Dictionary<string, DestinationConfig>(StringComparer.OrdinalIgnoreCase)
  31.         {
  32.             { "admin", new DestinationConfig() { Address = "https://admin.blazor.zone" } }
  33.         }
  34.     },
  35.     new ClusterConfig()
  36.     {
  37.         ClusterId = "blazor",
  38.         LoadBalancingPolicy = "RoundRobin",
  39.         Destinations = new Dictionary<string, DestinationConfig>(StringComparer.OrdinalIgnoreCase)
  40.         {
  41.             { "blazor", new DestinationConfig() { Address = "https://www.blazor.zone" } }
  42.         }
  43.     }
  44. };
  45. builder.Services.AddReverseProxy().LoadFromMemory(routes, clusters);
复制代码
  上面的配置代码,跟配置文件方式的节点和属性,都是对应的,照着写就是了
  1. "ReverseProxy": {
  2.   "Routes": {
  3.     "admin": {
  4.       "ClusterId": "admin",
  5.       "Match": {
  6.         "Hosts": [ "test1.ysmc.net.cn" ],
  7.         "Path": "{**catch-all}"
  8.       }
  9.     },
  10.     "blazor": {
  11.       "ClusterId": "blazor",
  12.       "Match": {
  13.         "Hosts": [ "test2.ysmc.net.cn" ],
  14.         "Path": "{**catch-all}"
  15.       }
  16.     }
  17.   },
  18.   "Clusters": {
  19.     "admin": {
  20.       "LoadBalancingPolicy": "RoundRobin",
  21.       "Destinations": {
  22.         "admin": {
  23.           "Address": "https://admin.blazor.zone/"
  24.         }
  25.       }
  26.     },
  27.     "blazor": {
  28.       "LoadBalancingPolicy": "RoundRobin",
  29.       "Destinations": {
  30.         "blazor": {
  31.           "Address": "https://www.blazor.zone/"
  32.         }
  33.       }
  34.     }
  35.   }
  36. }
复制代码
 
  最终效果还是依旧的完美,感谢大佬的观看,谢谢!

 原文链接:https://www.cnblogs.com/ysmc/p/16724763.html

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

罪恶克星

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表