【.NET Core项目实战-统一认证平台】第四章 网关篇-数据库存储配置(2) (4)

到此,我们网关就实现了2个方式更新配置信息,大家可以根据实际项目的情况从中选择适合自己的一种方式使用即可。

二、实现其他数据库扩展(以MySql为例)

我们实际项目应用过程中,经常会根据不同的项目类型选择不同的数据库,这时网关也要配合项目需求来适应不同数据库的切换,本节就以mysql为例讲解下我们的扩展网关怎么实现数据库的切换及应用,如果有其他数据库使用需求可以根据本节内容进行扩展。

【.NET Core项目实战-统一认证平台】第三章 网关篇-数据库存储配置信息(1)中介绍了网关的数据库初步设计,里面有我的设计的概念模型,现在使用mysql数据库,直接生成mysql的物理模型,然后生成数据库脚本,详细的生成方式请见上一篇,一秒搞定。是不是有点小激动,原来可以这么方便。

新建MySqlFileConfigurationRepository实现IFileConfigurationRepository接口,需要NuGet中添加MySql.Data.EntityFrameworkCore。

using Ctr.AhphOcelot.Configuration; using Ctr.AhphOcelot.Model; using Dapper; using MySql.Data.MySqlClient; using Ocelot.Configuration.File; using Ocelot.Configuration.Repository; using Ocelot.Responses; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace Ctr.AhphOcelot.DataBase.MySql { /// <summary> /// 金焰的世界 /// 2018-11-12 /// 使用MySql来实现配置文件仓储接口 /// </summary> public class MySqlFileConfigurationRepository : IFileConfigurationRepository { private readonly AhphOcelotConfiguration _option; public MySqlFileConfigurationRepository(AhphOcelotConfiguration option) { _option = option; } /// <summary> /// 从数据库中获取配置信息 /// </summary> /// <returns></returns> public async Task<Response<FileConfiguration>> Get() { #region 提取配置信息 var file = new FileConfiguration(); //提取默认启用的路由配置信息 string glbsql = "select * from AhphGlobalConfiguration where IsDefault=1 and InfoStatus=1"; //提取全局配置信息 using (var connection = new MySqlConnection(_option.DbConnectionStrings)) { var result = await connection.QueryFirstOrDefaultAsync<AhphGlobalConfiguration>(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 T2.* from AhphConfigReRoutes T1 inner join AhphReRoute T2 on T1.ReRouteId=T2.ReRouteId where AhphId=@AhphId and InfoStatus=1"; var routeresult = (await connection.QueryAsync<AhphReRoute>(routesql, new { result.AhphId }))?.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.RequestIdKey; m.Priority = model.Priority ?? 0; m.RequestIdKey = model.RequestIdKey; m.ServiceName = model.ServiceName; m.UpstreamHost = model.UpstreamHost; 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); } //由于数据库存储可不实现Set接口直接返回 public async Task<Response> Set(FileConfiguration fileConfiguration) { return new OkResponse(); } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwdfzp.html