最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
.NET使用YARP通过编码方式配置域名转发实现反向代理教程
时间:2022-10-10 编辑:坚强 来源:一聚教程网
本文为大家介绍的是关于.NET使用YARP通过编码方式配置域名转发实现反向代理教程,感兴趣的小伙伴不要错过了,一起来看看吧。
首先,参照官方文档,我们先新建一个 InMemoryConfigProvider 类,并且继承 IProxyConfigProvider 接口,类里面还包含了一个 IProxyConfig 的类,别看漏了噢!
这里多嘴一下,下面的代码出现了 volatile 关键字,介绍一下它:volatile 是 C# 中用于控制同步的关键字,其意义是针对程序中一些敏感数据,不允许多线程同时访问,保证数据在任何访问时刻,最多有一个线程访问,以保证数据的完整性,volatile 是修饰变量的修饰符。
public class InMemoryConfigProvider : IProxyConfigProvider { private volatile InMemoryConfig _config; public InMemoryConfigProvider(IReadOnlyListroutes, IReadOnlyList clusters) { _config = new InMemoryConfig(routes, clusters); } public IProxyConfig GetConfig() => _config; public void Update(IReadOnlyList routes, IReadOnlyList clusters) { var oldConfig = _config; _config = new InMemoryConfig(routes, clusters); oldConfig.SignalChange(); } private class InMemoryConfig : IProxyConfig { private readonly CancellationTokenSource _cts = new(); public InMemoryConfig(IReadOnlyList routes, IReadOnlyList clusters) { Routes = routes; Clusters = clusters; ChangeToken = new CancellationChangeToken(_cts.Token); } public IReadOnlyList Routes { get; } public IReadOnlyList Clusters { get; } public IChangeToken ChangeToken { get; } internal void SignalChange() { _cts.Cancel(); } } }
然后添加一个扩展 InMemoryConfigProviderExtensions
public static class InMemoryConfigProviderExtensions { public static IReverseProxyBuilder LoadFromMemory(this IReverseProxyBuilder builder, IReadOnlyListroutes, IReadOnlyList clusters) { builder.Services.AddSingleton (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(StringComparer.OrdinalIgnoreCase) { { "admin", new DestinationConfig() { Address = "https://admin.blazor.zone" } } } }, new ClusterConfig() { ClusterId = "blazor", LoadBalancingPolicy = "RoundRobin", Destinations = new Dictionary (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/" } } } } }
最终效果还是依旧的完美,感谢大佬的观看,谢谢!
以上就是关于.NET使用YARP通过编码方式配置域名转发实现反向代理教程的全部内容了,感兴趣的小伙伴记得点击关注哦。
-
上一个: ASP.NET Core 配置文件教程
-
下一个: .Net反向代理组件Yarp用法介绍
相关文章
- .Net反向代理组件Yarp用法介绍 10-10
- .Net中Task Parallel Library的基本用法介绍 10-10
- .NET Core Dapper操作mysql数据库的实现教程 05-06
- 易语言给组件上加载图片方法步骤 12-26
- Asp.Net Core使用SignalR进行服务间调用方法示例 12-24
- Asp.Net Core轻松学之利用日志监视进行服务遥测详解 12-19