.NET使用YARP通过编码方式配置域名转发实现反向代理(.net core 3.0教程)干货满满

随心笔谈11个月前发布 admin
114 0

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

© 版权声明

相关文章