做者:依樂祝
原文地址:https://www.cnblogs.com/yilezhu/p/9852711.htmlhtml
上篇文章給你們分享瞭如何集成我寫的一個Ocelot擴展插件把Ocelot的配置存儲到數據庫中。並無對實現原理進行相應的闡述。今天抽空把實現的原理給你們說道說道。明白原理後,你們就能夠自行改寫進行擴展來知足自身須要了!
再次感受張隊的審稿,並給出的修改意見!git
你們能夠自行分析Ocelot的源碼,我經過分析ocelot的源碼得出,若是要實現重寫配置文件的方式,只須要寫一個類來實現IFileConfigurationRepository這個接口便可。github
代碼以下:sql
/// <summary> /// yilezhu /// 2018.10.22 /// 實現從SQLSERVER數據庫中提取配置信息 /// </summary> public class SqlServerFileConfigurationRepository : IFileConfigurationRepository { private readonly IOcelotCache<FileConfiguration> _cache; private readonly IOcelotLogger _logger; private readonly ConfigAuthLimitCacheOptions _option; public SqlServerFileConfigurationRepository(ConfigAuthLimitCacheOptions option, IOcelotCache<FileConfiguration> cache, IOcelotLoggerFactory loggerFactory) { _option = option; _cache = cache; _logger = loggerFactory.CreateLogger<SqlServerFileConfigurationRepository>(); } public Task<Response> Set(FileConfiguration fileConfiguration) { _cache.AddAndDelete(_option.CachePrefix + "FileConfiguration", fileConfiguration, TimeSpan.FromSeconds(1800), ""); return Task.FromResult((Response)new OkResponse()); } /// <summary> /// 提取配置信息 /// </summary> /// <returns></returns> public async Task<Response<FileConfiguration>> Get() { var config = _cache.Get(_option.CachePrefix + "FileConfiguration", ""); if (config != null) { return new OkResponse<FileConfiguration>(config); } #region 提取配置信息 var file = new FileConfiguration(); string glbsql = "select top 1 * from OcelotGlobalConfiguration where IsDefault=1"; //提取全局配置信息 using (var connection = new SqlConnection(_option.DbConnectionStrings)) { var result = await connection.QueryFirstOrDefaultAsync<OcelotGlobalConfiguration>(glbsql); if (result != null) { var glb = new FileGlobalConfiguration(); glb.BaseUrl = result.BaseUrl; glb.DownstreamScheme = result.DownstreamScheme; glb.RequestIdKey = result.RequestIdKey; if (!String.IsNullOrEmpty(result.HttpHandlerOptions)) { glb.HttpHandlerOptions = result.HttpHandlerOptions.ToObject<FileHttpHandlerOptions>(); } if (!String.IsNullOrEmpty(result.LoadBalancerOptions)) { glb.LoadBalancerOptions = result.LoadBalancerOptions.ToObject<FileLoadBalancerOptions>(); } if (!String.IsNullOrEmpty(result.QoSOptions)) { glb.QoSOptions = result.QoSOptions.ToObject<FileQoSOptions>(); } if (!String.IsNullOrEmpty(result.ServiceDiscoveryProvider)) { glb.ServiceDiscoveryProvider = result.ServiceDiscoveryProvider.ToObject<FileServiceDiscoveryProvider>(); } file.GlobalConfiguration = glb; //提取路由信息 string routesql = "select * from OcelotReRoutes where OcelotGlobalConfigurationId=@OcelotGlobalConfigurationId and IsStatus=1"; var routeresult = (await connection.QueryAsync<OcelotReRoutes>(routesql, new { OcelotGlobalConfigurationId=result.Id })).AsList(); if (routeresult != null && routeresult.Count > 0) { var reroutelist = new List<FileReRoute>(); foreach (var model in routeresult) { var m = new FileReRoute(); if (!String.IsNullOrEmpty(model.AuthenticationOptions)) { m.AuthenticationOptions = model.AuthenticationOptions.ToObject<FileAuthenticationOptions>(); } if (!String.IsNullOrEmpty(model.CacheOptions)) { m.FileCacheOptions = model.CacheOptions.ToObject<FileCacheOptions>(); } if (!String.IsNullOrEmpty(model.DelegatingHandlers)) { m.DelegatingHandlers = model.DelegatingHandlers.ToObject<List<string>>(); } if (!String.IsNullOrEmpty(model.LoadBalancerOptions)) { m.LoadBalancerOptions = model.LoadBalancerOptions.ToObject<FileLoadBalancerOptions>(); } if (!String.IsNullOrEmpty(model.QoSOptions)) { m.QoSOptions = model.QoSOptions.ToObject<FileQoSOptions>(); } if (!String.IsNullOrEmpty(model.DownstreamHostAndPorts)) { m.DownstreamHostAndPorts = model.DownstreamHostAndPorts.ToObject<List<FileHostAndPort>>(); } //開始賦值 m.DownstreamPathTemplate = model.DownstreamPathTemplate; m.DownstreamScheme = model.DownstreamScheme; m.Key = model.Key; m.Priority = model.Priority ?? 0; m.RequestIdKey = model.RequestIdKey; m.ServiceName = model.ServiceName; m.Timeout = model.Timeout ?? 0; m.UpstreamHost = model.UpstreamHost; if (!String.IsNullOrEmpty(model.UpstreamHttpMethod)) { m.UpstreamHttpMethod = model.UpstreamHttpMethod.ToObject<List<string>>(); } m.UpstreamPathTemplate = model.UpstreamPathTemplate; reroutelist.Add(m); } file.ReRoutes = reroutelist; } } else { throw new Exception("未監測到配置信息"); } } #endregion if (file.ReRoutes == null || file.ReRoutes.Count == 0) { return new OkResponse<FileConfiguration>(null); } return new OkResponse<FileConfiguration>(file); } }
固然,既然咱們已經從新實現了這個接口,那麼就得進行相應的DI了。這裏咱們擴展下IOcelotBuilder方法,代碼以下,主要就是進行相應的服務的DI:數據庫
/// <summary> /// yilezhu /// 2018.10.22 /// 基於Ocelot擴展的依賴注入 /// </summary> public static class ServiceCollectionExtensions { /// <summary> /// 添加默認的注入方式,全部須要傳入的參數都是用默認值 /// </summary> /// <param name="builder"></param> /// <returns></returns> public static IOcelotBuilder AddAuthLimitCache(this IOcelotBuilder builder, Action<ConfigAuthLimitCacheOptions> option) { builder.Services.Configure(option); builder.Services.AddSingleton( resolver => resolver.GetRequiredService<IOptions<ConfigAuthLimitCacheOptions>>().Value); #region 注入其餘配置信息 //重寫提取Ocelot配置信息, builder.Services.AddSingleton(DataBaseConfigurationProvider.Get); //builder.Services.AddHostedService<FileConfigurationPoller>(); builder.Services.AddSingleton<IFileConfigurationRepository, SqlServerFileConfigurationRepository>(); //注入自定義限流配置 //注入認證信息 #endregion return builder; } }
接下來就是重寫,OcelotBuild裏面配置文件的獲取方式了。這裏我選擇的是對IApplicationBuilder進行擴展,由於這樣方便作一些其餘的事情,好比,重寫限流,集成自定義的驗證等等。具體代碼以下:json
/// <summary> /// yilezhu /// 2018.10.22 /// 擴展IApplicationBuilder,新增use方法 /// </summary> public static class OcelotMiddlewareExtensions { /// <summary> /// 擴展UseOcelot /// </summary> /// <param name="builder"></param> /// <returns></returns> public static async Task<IApplicationBuilder> UseAhphOcelot(this IApplicationBuilder builder) { await builder.UseAhphOcelot(new OcelotPipelineConfiguration()); return builder; } /// <summary> /// 重寫Ocelot,帶參數 /// </summary> /// <param name="builder"></param> /// <param name="pipelineConfiguration"></param> /// <returns></returns> public static async Task<IApplicationBuilder> UseAhphOcelot(this IApplicationBuilder builder, OcelotPipelineConfiguration pipelineConfiguration) { var configuration = await CreateConfiguration(builder); ConfigureDiagnosticListener(builder); return CreateOcelotPipeline(builder, pipelineConfiguration); } private static async Task<IInternalConfiguration> CreateConfiguration(IApplicationBuilder builder) { // make configuration from file system? // earlier user needed to add ocelot files in startup configuration stuff, asp.net will map it to this //var fileConfig = builder.ApplicationServices.GetService<IOptionsMonitor<FileConfiguration>>(); var fileConfig = await builder.ApplicationServices.GetService<IFileConfigurationRepository>().Get(); // now create the config var internalConfigCreator = builder.ApplicationServices.GetService<IInternalConfigurationCreator>(); var internalConfig = await internalConfigCreator.Create(fileConfig.Data); //Configuration error, throw error message if (internalConfig.IsError) { ThrowToStopOcelotStarting(internalConfig); } // now save it in memory var internalConfigRepo = builder.ApplicationServices.GetService<IInternalConfigurationRepository>(); internalConfigRepo.AddOrReplace(internalConfig.Data); //fileConfig.OnChange(async (config) => //{ // var newInternalConfig = await internalConfigCreator.Create(config); // internalConfigRepo.AddOrReplace(newInternalConfig.Data); //}); var adminPath = builder.ApplicationServices.GetService<IAdministrationPath>(); var configurations = builder.ApplicationServices.GetServices<OcelotMiddlewareConfigurationDelegate>(); // Todo - this has just been added for consul so far...will there be an ordering problem in the future? Should refactor all config into this pattern? foreach (var configuration in configurations) { await configuration(builder); } if (AdministrationApiInUse(adminPath)) { //We have to make sure the file config is set for the ocelot.env.json and ocelot.json so that if we pull it from the //admin api it works...boy this is getting a spit spags boll. var fileConfigSetter = builder.ApplicationServices.GetService<IFileConfigurationSetter>(); // await SetFileConfig(fileConfigSetter, fileConfig.Data); } return GetOcelotConfigAndReturn(internalConfigRepo); } private static bool AdministrationApiInUse(IAdministrationPath adminPath) { return adminPath != null; } //private static async Task SetFileConfig(IFileConfigurationSetter fileConfigSetter, IOptionsMonitor<FileConfiguration> fileConfig) //{ // var response = await fileConfigSetter.Set(fileConfig.CurrentValue); // if (IsError(response)) // { // ThrowToStopOcelotStarting(response); // } //} private static bool IsError(Response response) { return response == null || response.IsError; } private static IInternalConfiguration GetOcelotConfigAndReturn(IInternalConfigurationRepository provider) { var ocelotConfiguration = provider.Get(); if (ocelotConfiguration?.Data == null || ocelotConfiguration.IsError) { ThrowToStopOcelotStarting(ocelotConfiguration); } return ocelotConfiguration.Data; } private static void ThrowToStopOcelotStarting(Response config) { throw new Exception($"Unable to start Ocelot, errors are: {string.Join(",", config.Errors.Select(x => x.ToString()))}"); } private static IApplicationBuilder CreateOcelotPipeline(IApplicationBuilder builder, OcelotPipelineConfiguration pipelineConfiguration) { var pipelineBuilder = new OcelotPipelineBuilder(builder.ApplicationServices); //重寫自定義管道 pipelineBuilder.BuildAhphOcelotPipeline(pipelineConfiguration); var firstDelegate = pipelineBuilder.Build(); /* inject first delegate into first piece of asp.net middleware..maybe not like this then because we are updating the http context in ocelot it comes out correct for rest of asp.net.. */ builder.Properties["analysis.NextMiddlewareName"] = "TransitionToOcelotMiddleware"; builder.Use(async (context, task) => { var downstreamContext = new DownstreamContext(context); await firstDelegate.Invoke(downstreamContext); }); return builder; } private static void ConfigureDiagnosticListener(IApplicationBuilder builder) { var env = builder.ApplicationServices.GetService<IHostingEnvironment>(); var listener = builder.ApplicationServices.GetService<OcelotDiagnosticListener>(); var diagnosticListener = builder.ApplicationServices.GetService<DiagnosticListener>(); diagnosticListener.SubscribeWithAdapter(listener); } }
這其中最主要的代碼就是,重寫配置文件獲取這塊。我在下面進行了截圖,並圈出來了,你們自行查看吧。c#
代碼重寫好了。因爲咱們服務註冊時經過擴展IOcelotBuilder,因此,咱們須要在ConfigureServices方法引入Ocelot服務的時候比Ocelot多寫一個方法,並傳入相關的配置信息,以下所示:api
services.AddOcelot()//注入Ocelot服務 .AddAuthLimitCache(option=> { option.DbConnectionStrings = "Server=.;Database=Ocelot;User ID=sa;Password=1;"; });
這裏的目的就是爲了注入咱們實現了IFileConfigurationRepository接口的SqlServerFileConfigurationRepository這個類。緩存
接下來就是在管道中使用咱們重寫的Ocelot服務了。以下所示,在Configure方法中按以下代碼進行使用:app
app.UseAhphOcelot().Wait();
好了,以上就是實現的整個過程了。通過這麼一分析是否是以爲很簡單呢。固然具體爲何按照上面處理就可以從數據庫獲取配置了呢,這個還須要你分析了源碼後才能瞭解。我也只是給你引路,傳達我實現的思路。
https://github.com/yilezhu/Ocelot.ConfigAuthLimitCache
今天抽空對上篇文章進行了補充說明,目的是給你們闡述下,配置文件存儲到數據庫中的實現過程及原理。讓你可以根據自身須要來進行改寫來知足你的業務需求。固然我也只是給你引路,具體爲何這樣實現下就可以成功呢?答案在Ocelot的源碼中。
若是你想了解更多Ocelot的定製相關的·內容能夠看我一個朋友寫的系列博客,博客地址:https://www.cnblogs.com/jackcao/p/9928879.html 【.NET Core微服務實戰-統一身份認證】網關篇,這裏給你詳細介紹瞭如何進行自定義限流以及客戶端受權並重寫Ocelot緩存成Redis!有興趣的能夠看下!