Ocelot简易教程(七)之配置文件数据库存储插件源码解析

 作者:依乐祝

原文地址:https://www.cnblogs.com/yilezhu/p/9852711.html

上篇文章给大家分享了如何集成我写的一个Ocelot扩展插件把Ocelot的配置存储到数据库中。并没有对实现原理进行相应的阐述。今天抽空把实现的原理给大家说道说道。明白原理后,大家就可以自行改写进行扩展来满足自身需要了!
再次感觉张队的审稿,并给出的修改意见!

源码解析过程

大家可以自行分析Ocelot的源码,我通过分析ocelot的源码得出,如果要实现重写配置文件的方式,只需要写一个类来实现IFileConfigurationRepository这个接口即可。

代码如下:

 /// <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();                             
                        
关键字:
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信