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,我们修改一下配置文件然后重新调用接口看一下,这时会更新最新的路径。

     

  • 相关阅读:
    Navicat Premium 12.1.12.0破解版激活
    vConsole调试器
    使用DbFunctions来解决asp.net mvc ef按照日期分组数据
    谷歌浏览器如何安装CRX插件?crx离线插件安装方法
    ASP.NET MVC——CodeFirst开发模式
    Sql server 事务的两种用法
    SQL Server 存储过程
    JqueryMobile新手问题大全
    .net core 轻量级容器 ServiceProvider 源码分析
    Centos7 使用Docker 部署mssql 2017
  • 原文地址:https://www.cnblogs.com/ZhengHengWU/p/13197820.html
Copyright © 2011-2022 走看看