欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.Net Core實(shí)現(xiàn)選擇數(shù)據(jù)熱更新讓服務(wù)感知配置的變化

 更新時(shí)間:2021年03月04日 10:34:03   作者:吳政恒  
這篇文章主要介紹了.Net Core實(shí)現(xiàn)選擇數(shù)據(jù)熱更新讓服務(wù)感知配置的變化,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1、說(shuō)明

當(dāng)一些配置需要修改在進(jìn)行獲取時(shí),通常做法是修改完配置文件后再重啟web服務(wù)器或者docker進(jìn)行完成,下面我介紹一種熱更新方法,修改完配置文件后,不需要重啟服務(wù)器即可獲取最新的配置文件,讓服務(wù)感知配置的變化。

2、實(shí)踐

下面我通過(guò)二種方式來(lái)講解一下.Net Core實(shí)現(xiàn)選擇數(shù)據(jù)熱更新,讓服務(wù)感知配置的變化。

2.1 通過(guò)AddSingleton單例方式注入,然后使用 IOptionsMonitor實(shí)現(xiàn)數(shù)據(jù)熱更新

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

//通過(guò)讀取配置文件加載到SystemPath類(lèi)中
services.Configure<SystemPath>(Configuration.GetSection("SystemPath"));
//添加服務(wù)注入
services.AddSingleton<IPathService, PathService>();
public class SystemPath
  {
    public string FilePath { get; set; }
  }

2.1.2 在PathService構(gòu)造器中注入IOptionsMonitor<SystemPath>實(shí)現(xiàn)數(shù)據(jù)熱更新

public class PathService : IPathService
  {
    IOptionsMonitor<SystemPath> _options;
    /// <summary>
    /// 構(gòu)造函數(shù)
    /// </summary>
    /// <param name="blogData"></param>
    public PathService(IOptionsMonitor<SystemPath> options)
    {
      _options = options;
    }
    public string GetPath()
    {
      return _options.CurrentValue.FilePath;
    }
  }

2.1.3 在PathController中通過(guò)調(diào)用接口方式讀取最新配置路徑

/// <summary>
  /// 路徑
  /// </summary>
  [Route("api/[controller]/[action]")]
  [ApiController]
  public class PathController : ControllerBase
  {
    private readonly IPathService _pathService;
    /// <summary>
    /// 構(gòu)造函數(shù)
    /// </summary>
    /// <param name="pathService"></param>
    public PathController(IPathService pathService)
    {
      _pathService = pathService;
    }
    /// <summary>
    /// 獲取系統(tǒng)路徑
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public MethodResult GetSystemPath()
    {
      return new MethodResult(_pathService.GetPath());
    }
  }

運(yùn)行看一下效果:

現(xiàn)在讀取到的路徑是D:/File/2.jpg,我們修改一下配置文件然后重新調(diào)用接口看一下,這時(shí)會(huì)更新最新的路徑。

 

2.2 通過(guò)AddScoped 方式注入,然后使用 IOptionsSnapshot 實(shí)現(xiàn)數(shù)據(jù)熱更新

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

//通過(guò)讀取配置文件加載到SystemPath類(lèi)中
services.Configure<SystemPath>(Configuration.GetSection("SystemPath"));
//添加服務(wù)注入
services.AddScoped<IPathService, PathService>();
public class SystemPath
  {
    public string FilePath { get; set; }
  }

2.2.2 在PathService構(gòu)造器中注入IOptionsMonitor<SystemPath>實(shí)現(xiàn)數(shù)據(jù)熱更新

public class PathService : IPathService
  {
    IOptionsSnapshot<SystemPath> _options;
    /// <summary>
    /// 構(gòu)造函數(shù)
    /// </summary>
    /// <param name="blogData"></param>
    public PathService(IOptionsSnapshot<SystemPath> options)
    {
      _options = options;
    }
    public string GetPath()
    {
      return _options.Value.FilePath;
    }
  }

2.2.3 在PathController中通過(guò)調(diào)用接口方式讀取最新配置路徑

/// <summary>
  /// 路徑
  /// </summary>
  [Route("api/[controller]/[action]")]
  [ApiController]
  public class PathController : ControllerBase
  {
    private readonly IPathService _pathService;
    /// <summary>
    /// 構(gòu)造函數(shù)
    /// </summary>
    /// <param name="pathService"></param>
    public PathController(IPathService pathService)
    {
      _pathService = pathService;
    }
    /// <summary>
    /// 獲取系統(tǒng)路徑
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public MethodResult GetSystemPath()
    {
      return new MethodResult(_pathService.GetPath());
    }
  }

運(yùn)行看一下效果:

現(xiàn)在讀取到的路徑是D:/File/2.jpg,我們修改一下配置文件然后重新調(diào)用接口看一下,這時(shí)會(huì)更新最新的路徑。

 

到此這篇關(guān)于.Net Core實(shí)現(xiàn)選擇數(shù)據(jù)熱更新讓服務(wù)感知配置的變化的文章就介紹到這了,更多相關(guān).Net Core數(shù)據(jù)熱更新內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論