zoukankan      html  css  js  c++  java
  • .Net Core实现选择数据热更新,让服务感知配置的变化

    1、说明

    当一些配置需要修改在进行获取时,通常做法是修改完配置文件后再重启web服务器或者docker进行完成,下面我介绍一种热更新方法,修改完配置文件后,不需要重启服务器即可获取最新的配置文件,让服务感知配置的变化。

    2、实践

    下面我通过二种方式来讲解一下.Net Core实现选择数据热更新,让服务感知配置的变化。

    2.1  通过AddSingleton单例方式注入,然后使用 IOptionsMonitor实现数据热更新

     2.1.1 首先在Startup.cs文件中的ConfigureServices方法添加配置

    //通过读取配置文件加载到SystemPath类中
    services.Configure<SystemPath>(Configuration.GetSection("SystemPath"));
    //添加服务注入
    services.AddSingleton<IPathService, PathService>();
    public class SystemPath
        {
            public string FilePath { get; set; }
        }

    2.1.2 在PathService构造器中注入IOptionsMonitor<SystemPath>实现数据热更新

    public class PathService : IPathService
        {
            IOptionsMonitor<SystemPath> _options;
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="blogData"></param>
            public PathService(IOptionsMonitor<SystemPath> options)
            {
                _options = options;
            }
            public string GetPath()
            {
                return _options.CurrentValue.FilePath;
            }
        }

    2.1.3 在PathController中通过调用接口方式读取最新配置路径

    /// <summary>
        /// 路径
        /// </summary>
        [Route("api/[controller]/[action]")]
        [ApiController]
        public class PathController : ControllerBase
        {
            private readonly IPathService _pathService;
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="pathService"></param>
            public PathController(IPathService pathService)
            {
                _pathService = pathService;
            }
            /// <summary>
            /// 获取系统路径
            /// </summary>
            /// <returns></returns>
            [HttpGet]
            public MethodResult GetSystemPath()
            {
                return new MethodResult(_pathService.GetPath());
            }
        }

    运行看一下效果:

    现在读取到的路径是D:/File/2.jpg,我们修改一下配置文件然后重新调用接口看一下,这时会更新最新的路径。

     

    2.2  通过AddScoped 方式注入,然后使用 IOptionsSnapshot 实现数据热更新

     2.2.1 首先在Startup.cs文件中的ConfigureServices方法添加配置

    //通过读取配置文件加载到SystemPath类中
    services.Configure<SystemPath>(Configuration.GetSection("SystemPath"));
    //添加服务注入
    services.AddScoped<IPathService, PathService>();
    public class SystemPath
        {
            public string FilePath { get; set; }
        }

    2.2.2 在PathService构造器中注入IOptionsMonitor<SystemPath>实现数据热更新

    public class PathService : IPathService
        {
            IOptionsSnapshot<SystemPath> _options;
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="blogData"></param>
            public PathService(IOptionsSnapshot<SystemPath> options)
            {
                _options = options;
            }
            public string GetPath()
            {
                return _options.Value.FilePath;
            }
        }

    2.2.3 在PathController中通过调用接口方式读取最新配置路径

    /// <summary>
        /// 路径
        /// </summary>
        [Route("api/[controller]/[action]")]
        [ApiController]
        public class PathController : ControllerBase
        {
            private readonly IPathService _pathService;
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="pathService"></param>
            public PathController(IPathService pathService)
            {
                _pathService = pathService;
            }
            /// <summary>
            /// 获取系统路径
            /// </summary>
            /// <returns></returns>
            [HttpGet]
            public MethodResult GetSystemPath()
            {
                return new MethodResult(_pathService.GetPath());
            }
        }

    运行看一下效果:

    现在读取到的路径是D:/File/2.jpg,我们修改一下配置文件然后重新调用接口看一下,这时会更新最新的路径。

     

  • 相关阅读:
    用小程序&#183;云开发两天搭建mini论坛丨实战
    巧用小程序&#183;云开发实现邮件发送功能丨实战
    借助云开发,10行代码实现小程序支付功能!丨实战
    你的心事我全知晓——心情日记小程序丨实战
    只需20小时,让0基础的你掌握小程序云开发!这个暑假,约否?
    诗词歌赋,样样精通!诗词古语小程序带你领略魅力古风丨实战
    在线教育项目-day02【后台讲师管理模块】
    在线教育项目-day02【搭建项目结构】
    在线教育项目-day01【wrapper条件构造器】
    在线教育项目-day01【性能分析插件】
  • 原文地址:https://www.cnblogs.com/ZhengHengWU/p/13197820.html
Copyright © 2011-2022 走看看