.NET Core 3.0之創(chuàng)建基于Consul的Configuration擴展組件
經(jīng)過前面三篇關(guān)于.NET Core Configuration的文章之后,本篇文章主要討論如何擴展一個Configuration組件出來。
了解了Configuration的源碼后,再去擴展一個組件就會比較簡單,接下來我們將在.NET Core 3.0-preview5的基礎(chǔ)上創(chuàng)建一個基于Consul的配置組件。
相信大家對Consul已經(jīng)比較了解了,很多項目都會使用Consul作為配置中心,此處也不做其他闡述了,主要是講一下,創(chuàng)建Consul配置擴展的一些思路。使用Consul配置功能時,我們可以將信息轉(zhuǎn)成JSON格式后再存儲,那么我們在讀取的時候,在體驗上就像是從讀取JSON文件中讀取一樣。
開發(fā)前的準(zhǔn)備初始化Consul
假設(shè)你已經(jīng)安裝并啟動了Consul,我們打開Key/Value功能界面,創(chuàng)建兩組配置選項出來,分別是commonservice和userservice,如下圖所示
配置值采用JSON格式
實現(xiàn)思路
我們知道在Configuration整個的設(shè)計框架里,比較重要的類ConfigurationRoot,內(nèi)部又有一個IConfigurationProvider集合屬性,也就是說我們追加IConfigurationProvider實例最終也會被放到到該集合中,如下圖所示
該項目中,我使用到了一個已經(jīng)封裝好的Consul(V0.7.2.6)類庫,同時基于.NET Core關(guān)于Configuration的設(shè)計風(fēng)格,做如下的框架設(shè)計
考慮到我會在該組件內(nèi)部創(chuàng)建ConsulClient實例,所以對ConsulClient構(gòu)造函數(shù)的一部分參數(shù)做了抽象提取,并添加到了IConsulConfigurationSource中,以增強該組件的靈活性。
之前說過,Consul中的配置信息是以JSON格式存儲的,所以此處使用到了Microsoft.Extensions.Configuration.Json.JsonConfigurationFileParser,用以將JSON格式的信息轉(zhuǎn)換為Configuration的通用格式Key/Value。
核心代碼 IConsulConfigurationSource
/// <summary> /// ConsulConfigurationSource /// </summary> public interface IConsulConfigurationSource : IConfigurationSource { /// <summary> /// CancellationToken /// </summary> CancellationToken CancellationToken { get; } /// <summary> /// Consul構(gòu)造函數(shù)實例,可自定義傳入 /// </summary> Action<ConsulClientConfiguration> ConsulClientConfiguration { get; set; } /// <summary> /// Consul構(gòu)造函數(shù)實例,可自定義傳入 /// </summary> Action<HttpClient> ConsulHttpClient { get; set; } /// <summary> /// Consul構(gòu)造函數(shù)實例,可自定義傳入 /// </summary> Action<HttpClientHandler> ConsulHttpClientHandler { get; set; } /// <summary> /// 服務(wù)名稱 /// </summary> string ServiceKey { get; } /// <summary> /// 可選項 /// </summary> bool Optional { get; set; } /// <summary> /// Consul查詢選項 /// </summary> QueryOptions QueryOptions { get; set; } /// <summary> /// 重新加載延遲時間,單位是毫秒 /// </summary> int ReloadDelay { get; set; } /// <summary> /// 是否在配置改變的時候重新加載 /// </summary> bool ReloadOnChange { get; set; } }
ConsulConfigurationSource
該類提供了一個構(gòu)造函數(shù),用于接收ServiceKey和CancellationToken實例
public ConsulConfigurationSource(string serviceKey, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(serviceKey)) { throw new ArgumentNullException(nameof(serviceKey)); } this.ServiceKey = serviceKey; this.CancellationToken = cancellationToken; }
其build()方法也比較簡單,主要是初始化ConsulConfigurationParser實例
public IConfigurationProvider Build(IConfigurationBuilder builder) { ConsulConfigurationParser consulParser = new ConsulConfigurationParser(this); return new ConsulConfigurationProvider(this, consulParser); }
ConsulConfigurationParser
該類比較復(fù)雜,主要實現(xiàn)Consul配置的獲取、監(jiān)控以及容錯處理,公共方法源碼如下
/// <summary> /// 獲取并轉(zhuǎn)換Consul配置信息 /// </summary> /// <param name="reloading"></param> /// <param name="source"></param> /// <returns></returns> public async Task<IDictionary<string, string>> GetConfig(bool reloading, IConsulConfigurationSource source) { try { QueryResult<KVPair> kvPair = await this.GetKvPairs(source.ServiceKey, source.QueryOptions, source.CancellationToken).ConfigureAwait(false); if ((kvPair?.Response == null) && !source.Optional) { if (!reloading) { throw new FormatException(Resources.Error_InvalidService(source.ServiceKey)); } return new Dictionary<string, string>(); } if (kvPair?.Response == null) { throw new FormatException(Resources.Error_ValueNotExist(source.ServiceKey)); } this.UpdateLastIndex(kvPair); return JsonConfigurationFileParser.Parse(source.ServiceKey, new MemoryStream(kvPair.Response.Value)); } catch (Exception exception) { throw exception; } } /// <summary> /// Consul配置信息監(jiān)控 /// </summary> /// <param name="key"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public IChangeToken Watch(string key, CancellationToken cancellationToken) { Task.Run(() => this.RefreshForChanges(key, cancellationToken), cancellationToken); return this.reloadToken; }
另外,關(guān)于Consul的監(jiān)控主要利用了QueryResult.LastIndex屬性,該類緩存了該屬性的值,并與實獲取的值進行比較,以判斷是否需要重新加載內(nèi)存中的緩存配置
ConsulConfigurationProvider
該類除了實現(xiàn)Load方法外,還會根據(jù)ReloadOnChange屬性,在構(gòu)造函數(shù)中注冊O(shè)nChange事件,用于重新加載配置信息,源碼如下:
public sealed class ConsulConfigurationProvider : ConfigurationProvider { private readonly ConsulConfigurationParser configurationParser; private readonly IConsulConfigurationSource source; public ConsulConfigurationProvider(IConsulConfigurationSource source, ConsulConfigurationParser configurationParser) { this.configurationParser = configurationParser; this.source = source; if (source.ReloadOnChange) { ChangeToken.OnChange( () => this.configurationParser.Watch(this.source.ServiceKey, this.source.CancellationToken), async () => { await this.configurationParser.GetConfig(true, source).ConfigureAwait(false); Thread.Sleep(source.ReloadDelay); this.OnReload(); }); } } public override void Load() { try { this.Data = this.configurationParser.GetConfig(false, this.source).ConfigureAwait(false).GetAwaiter().GetResult(); } catch (AggregateException aggregateException) { throw aggregateException.InnerException; } } }
調(diào)用及運行結(jié)果
此處調(diào)用在Program中實現(xiàn)
public class Program { public static void Main(string[] args) { CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration( (hostingContext, builder) => { builder.AddConsul("userservice", cancellationTokenSource.Token, source => { source.ConsulClientConfiguration = cco => cco.Address = new Uri("http://localhost:8500"); source.Optional = true; source.ReloadOnChange = true; source.ReloadDelay = 300; source.QueryOptions = new QueryOptions { WaitIndex = 0 }; }); builder.AddConsul("commonservice", cancellationTokenSource.Token, source => { source.ConsulClientConfiguration = cco => cco.Address = new Uri("http://localhost:8500"); source.Optional = true; source.ReloadOnChange = true; source.ReloadDelay = 300; source.QueryOptions = new QueryOptions { WaitIndex = 0 }; }); }).UseStartup<Startup>().Build().Run(); } }
以上就是本次介紹的全部知識點內(nèi)容,感謝大家對腳本之家的支持。
- ASP.NET?Core中的Configuration配置二
- ASP.NET?Core中的Configuration配置一
- 淺析.netcore中的Configuration具體使用
- ASP.NET Core Web API 教程Project Configuration
- .Net Core配置Configuration具體實現(xiàn)
- 如何在ASP.NET Core 的任意類中注入Configuration
- .Net Core3.0 配置Configuration的實現(xiàn)
- Net Core全局配置讀取管理方法ConfigurationManager
- 詳解ASP.NET Core實現(xiàn)強類型Configuration讀取配置數(shù)據(jù)
相關(guān)文章
VB.NET調(diào)用MySQL存儲過程并獲得返回值的方法
這篇文章主要介紹了VB.NET調(diào)用MySQL存儲過程并獲得返回值的方法,涉及基于VB.NET操作MySQL數(shù)據(jù)庫的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07ASP.NET Core MVC學(xué)習(xí)教程之路由(Routing)
這篇文章主要給大家介紹了關(guān)于ASP.NET Core MVC學(xué)習(xí)教程之路由(Routing)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用ASP.NET Core MVC具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Asp.net core實現(xiàn)PushStream視頻流推送
這篇文章介紹了Asp.net core實現(xiàn)PushStream視頻流推送的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07