ocelot 配置文件的动态更新

       本文意在通过ocelot 了解asp.net core 对配置文件的动态更新机制。

一 配置文件与对象绑定

     UseOcelot方法调用时会执行配置文件对象创建,CreateConfiguration方法有个重要的对象  IOptionsMonitor<FileConfiguration> 通过 

fileConfig.onchange 注册回调方法 更改存储库。实现对内存对象FileConfiguration 的更新。
   public static async Task<IApplicationBuilder> UseOcelot(this IApplicationBuilder builder, OcelotPipelineConfiguration pipelineConfiguration)
        {
            var configuration = await CreateConfiguration(builder);
// ***
} private static async Task<IInternalConfiguration> CreateConfiguration(IApplicationBuilder builder) { // make configuration from file system? // earlier user needed to add ocelot files in startup configuration stuff, asp.net will map it to this var fileConfig = builder.ApplicationServices.GetService<IOptionsMonitor<FileConfiguration>>(); // now create the config // *** fileConfig.OnChange(async (config) => { var newInternalConfig = await internalConfigCreator.Create(config); internalConfigRepo.AddOrReplace(newInternalConfig.Data); }); }
  先来看看IOptionsMonitor<T>  定义如下:
  public interface IOptionsMonitor<out TOptions>
    {
        TOptions CurrentValue { get; }
        TOptions Get(string name);
        IDisposable OnChange(Action<TOptions, string> listener);
    }
IOptionsMonitor 从下面的服务注册可知 是OptionsMonitor<>
  public static IServiceCollection AddOptions(this IServiceCollection services)
        {
// ***
services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptionsMonitor<>), typeof(OptionsMonitor<>)));return services; }

 

 

 

 

 

 

一 物理文件最终处理

      public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
     WebHost.CreateDefaultBuilder(args)
         .ConfigureAppConfiguration((context, builder) => {
             builder.SetBasePath(context.HostingEnvironment.ContentRootPath)
             .AddJsonFile("Ocelot.json");
         })
         .UseUrls("http://localhost:6000")
         .UseStartup<Startup>()
         .Build();

webhost build的过程中都会绑定物理文件,如上的AddJsonFile。AddJsonFile的定义如下

public static IConfigurationBuilder AddJsonFile(this IConfigurationBuilder builder, IFileProvider provider, string path, bool optional, bool reloadOnChange)

  通过IFileProvider的实现,通过system.io对文件系统监控 实现最终的物理文件更改通知到上层。

  二 

 

转载于:https://www.cnblogs.com/RunStone/p/9531159.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值