Asp.Net Core自定义配置中心客户端( 二 )

NiuBConfigSource把Service传递进去
public class NiuBConfigSource : NiuBOption, IConfigurationSource{private INiuBService service;public NiuBConfigSource(INiuBService service){this.service = service;}public IConfigurationProvider Build(IConfigurationBuilder builder){return new NiuBConfigProvider(service);}}事情搞完了,下面来测试一下,由于注册中心没有实现,下面来模拟一下
// 配置中心的代码没有实现,通过该方法模拟监听private void grpcAccept(string host, string port, string configSectionName){ConfigureCenter.ConfigureChanged += configuration=> Configure(configuration);}ConfigureCenter的实现
public class ConfigureCenter{public ConfigureCenter() { }public event Action<Dictionary<string, string>> ConfigureChanged;public void PublishConfig(Dictionary<string, string> config){if (this.ConfigureChanged != null){ConfigureChanged(config);}}}模拟配置中心发布了新的配置
var service= app.Services.GetService<INiuBService>();service.ConfigureCenter.PublishConfig(new Dictionary<string, string>() {{ "OrderService","{"Host":"http://niubi.com/api/order","descript":"订单服务地址"}"}});通过API查看是否能获取到配置中心发布的配置
[Route("api/[Controller]")]public class OrderController : ControllerBase{public IConfiguration Configuration { get; set; }public OrderController(IConfiguration configuration ){this.Configuration = configuration;}[HttpGet]public string Get(){var value= https://www.isolves.com/it/cxkf/yy/net/2023-02-24/Configuration["OrderService"];return value == null ?"Empty": value;}}请求结果

Asp.Net Core自定义配置中心客户端

文章插图
 




推荐阅读