.Net Core實(shí)現(xiàn)選擇數(shù)據(jù)熱更新讓服務(wù)感知配置的變化
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)文章
ASP.NET MVC小結(jié)之基礎(chǔ)篇(一)
本文是ASP.NET MVC系列的第一篇文章,跟其他學(xué)習(xí)系列一樣,咱們先來(lái)點(diǎn)基礎(chǔ)知識(shí),之后再循序漸進(jìn)。我們先從asp.net mvc的概念開(kāi)始吧。2014-11-11詳解在.net core中完美解決多租戶分庫(kù)分表的問(wèn)題
這篇文章主要介紹了詳解在.net core中完美解決多租戶分庫(kù)分表的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04GridView_RowUpdating取不到新值的解決方法
GridView_RowUpdating取不到新值的解決方法,需要的朋友可以參考一下2013-05-05.net后臺(tái)頁(yè)面統(tǒng)一驗(yàn)證是否登錄
這篇文章主要為大家詳細(xì)介紹了.net后臺(tái)頁(yè)面統(tǒng)一驗(yàn)證是否登錄的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04asp.net中Session緩存與Cache緩存的區(qū)別分析
實(shí)現(xiàn)數(shù)據(jù)的緩存有很多種方法,有客戶端的Cookie,有服務(wù)器端的Session和Application2013-02-02.NET 6開(kāi)發(fā)TodoList應(yīng)用之實(shí)現(xiàn)查詢(xún)排序
這篇文章主要介紹了如何通過(guò).NET 6實(shí)現(xiàn)查詢(xún)排序功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí).NET 6有一定的幫助,感興趣的同學(xué)可以了解一下2022-01-01.Net Core官方JWT授權(quán)驗(yàn)證的全過(guò)程
這篇文章主要給大家介紹了關(guān)于.Net Core官方JWT授權(quán)驗(yàn)證的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12