【.NET Core專案實戰-統一認證平臺】第四章 閘道器篇-資料庫儲存配置(2)
【.NET Core專案實戰-統一認證平臺】開篇及目錄索引
上篇文章我們介紹瞭如何擴充套件Ocelot閘道器,並實現資料庫儲存,然後測試了閘道器的路由功能,一切都是那麼順利,但是有一個問題未解決,就是如果閘道器配置資訊發生變更時如何生效?以及我使用其他資料庫儲存如何快速實現?本篇就這兩個問題展開講解,用到的文件及原始碼將會在GitHub上開源,每篇的原始碼我將用分支的方式管理,本篇使用的分支為
course2
。
附文件及原始碼下載地址:[https://github.com/jinyancao/CtrAuthPlatform/tree/course2]
一、實現動態更新路由
上一篇我們實現了閘道器的配置資訊從資料庫中提取,專案釋出時可以把我們已有的閘道器配置都設定好並啟動,但是正式專案執行時,閘道器配置資訊隨時都有可能發生變更,那如何在不影響專案使用的基礎上來更新配置資訊呢?這篇我將介紹2種方式來實現閘道器的動態更新,一是後臺服務定期提取最新的閘道器配置資訊更新閘道器配置,二是閘道器對外提供安全介面,由我們需要更新時,呼叫此介面進行更新,下面就這兩種方式,我們來看下如何實現。
1、定時服務方式
閘道器的靈活性是設計時必須考慮的,實現定時服務的方式我們需要配置是否開啟和更新週期,所以我們需要擴充套件配置類AhphOcelotConfiguration
,增加是否啟用服務和更新週期2個欄位。
namespace Ctr.AhphOcelot.Configuration { /// <summary> /// 金焰的世界 /// 2018-11-11 /// 自定義配置資訊 /// </summary> public class AhphOcelotConfiguration { /// <summary> /// 資料庫連線字串,使用不同資料庫時自行修改,預設實現了SQLSERVER /// </summary> public string DbConnectionStrings { get; set; } /// <summary> /// 金焰的世界 /// 2018-11-12 /// 是否啟用定時器,預設不啟動 /// </summary> public bool EnableTimer { get; set; } = false; /// <summary> /// 金焰的世界 /// 2018-11.12 /// 定時器週期,單位(毫秒),預設30分鐘自動更新一次 /// </summary> public int TimerDelay { get; set; } = 30*60*1000; } }
配置檔案定義完成,那如何完成後臺任務隨著專案啟動而一起啟動呢?IHostedService
介面瞭解一下,我們可以通過實現這個介面,來完成我們後臺任務,然後通過Ioc容器注入即可。
新建DbConfigurationPoller
類,實現IHostedService
介面,詳細程式碼如下。
using Microsoft.Extensions.Hosting; using Ocelot.Configuration.Creator; using Ocelot.Configuration.Repository; using Ocelot.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Ctr.AhphOcelot.Configuration { /// <summary> /// 金焰的世界 /// 2018-11-12 /// 資料庫配置資訊更新策略 /// </summary> public class DbConfigurationPoller : IHostedService, IDisposable { private readonly IOcelotLogger _logger; private readonly IFileConfigurationRepository _repo; private readonly AhphOcelotConfiguration _option; private Timer _timer; private bool _polling; private readonly IInternalConfigurationRepository _internalConfigRepo; private readonly IInternalConfigurationCreator _internalConfigCreator; public DbConfigurationPoller(IOcelotLoggerFactory factory, IFileConfigurationRepository repo, IInternalConfigurationRepository internalConfigRepo, IInternalConfigurationCreator internalConfigCreator, AhphOcelotConfiguration option) { _internalConfigRepo = internalConfigRepo; _internalConfigCreator = internalConfigCreator; _logger = factory.CreateLogger<DbConfigurationPoller>(); _repo = repo; _option = option; } public void Dispose() { _timer?.Dispose(); } public Task StartAsync(CancellationToken cancellationToken) { if (_option.EnableTimer) {//判斷是否啟用自動更新 _logger.LogInformation($"{nameof(DbConfigurationPoller)} is starting."); _timer = new Timer(async x => { if (_polling) { return; } _polling = true; await Poll(); _polling = false; }, null, _option.TimerDelay, _option.TimerDelay); } return Task.CompletedTask; } public Task StopAsync(CancellationToken cancellationToken) { if (_option.EnableTimer) {//判斷是否啟用自動更新 _logger.LogInformation($"{nameof(DbConfigurationPoller)} is stopping."); _timer?.Change(Timeout.Infinite, 0); } return Task.CompletedTask; } private async Task Poll() { _logger.LogInformation("Started polling"); var fileConfig = await _repo.Get(); if (fileConfig.IsError) { _logger.LogWarning($"error geting file config, errors are {string.Join(",", fileConfig.Errors.Select(x => x.Message))}"); return; } else { var config = await _internalConfigCreator.Create(fileConfig.Data); if (!config.IsError) { _internalConfigRepo.AddOrReplace(config.Data); } } _logger.LogInformation("Finished polling"); } } }
專案程式碼很清晰,就是專案啟動時,判斷配置檔案是否開啟定時任務,如果開啟就根據啟動定時任務去從資料庫中提取最新的配置資訊,然後更新到內部配置並生效,停止時關閉並釋放定時器,然後再註冊後臺服務。
//註冊後端服務
builder.Services.AddHostedService<DbConfigurationPoller>();
現在我們啟動閘道器專案和測試服務專案,配置閘道器啟用定時器,程式碼如下。
public void ConfigureServices(IServiceCollection services)
{
services.AddOcelot().AddAhphOcelot(option=>
{
option.DbConnectionStrings = "Server=.;Database=Ctr_AuthPlatform;User ID=sa;Password=bl123456;";
option.EnableTimer = true; //啟用定時任務
option.TimerDelay = 10*000;//週期10秒
});
}
啟動後使用閘道器地址訪問http://localhost:7777/ctr/values
,可以得到正確地址。
然後我們在資料庫執行閘道器路由修改命令,等10秒後再重新整理頁面,發現原來的路由失效,新的路由成功。
UPDATE AhphReRoute SET UpstreamPathTemplate='/cjy/values' where ReRouteId=1
看到這個結果是不是很激動,只要稍微改造下我們的閘道器專案就實現了閘道器配置資訊的自動更新功能,剩下的就是根據我們專案後臺管理介面配置好具體的閘道器資訊即可。
2、介面更新的方式
對於良好的閘道器設計,我們應該是可以隨時控制閘道器啟用哪種配置資訊,這時我們就需要把閘道器的更新以介面的形式對外進行暴露,然後後臺管理介面在我們配置好閘道器相關資訊後,主動發起配置更新,並記錄操作日誌。
我們再回顧下Ocelot
原始碼,看是否幫我們實現了這個介面,查詢法Ctrl+F
搜尋看有哪些地方注入了IFileConfigurationRepository
這個介面,驚喜的發現有個FileConfigurationController
控制器已經實現了閘道器配置資訊預覽和更新的相關方法,檢視原始碼可以發現程式碼很簡單,跟我們之前寫的更新方式一模一樣,那我們如何使用這個方法呢?
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Ocelot.Configuration.File;
using Ocelot.Configuration.Setter;
namespace Ocelot.Configuration
{
using Repository;
[Authorize]
[Route("configuration")]
public class FileConfigurationController : Controller
{
private readonly IFileConfigurationRepository _repo;
private readonly IFileConfigurationSetter _setter;
private readonly IServiceProvider _provider;
public FileConfigurationController(IFileConfigurationRepository repo, IFileConfigurationSetter setter, IServiceProvider provider)
{
_repo = repo;
_setter = setter;
_provider = provider;
}
[HttpGet]
public async Task<IActionResult> Get()
{
var response = await _repo.Get();
if(response.IsError)
{
return new BadRequestObjectResult(response.Errors);
}
return new OkObjectResult(response.Data);
}
[HttpPost]
public async Task<IActionResult> Post([FromBody]FileConfiguration fileConfiguration)
{
try
{
var response = await _setter.Set(fileConfiguration);
if (response.IsError)
{
return new BadRequestObjectResult(response.Errors);
}
return new OkObjectResult(fileConfiguration);
}
catch(Exception e)
{
return new BadRequestObjectResult($"{e.Message}:{e.StackTrace}");
}
}
}
}
從原始碼中可以發現控制器中增加了授權訪問,防止非法請求來修改閘道器配置,Ocelot原始碼經過升級後,把不同的功能進行模組化,進一步增強專案的可配置性,減少冗餘,管理原始碼被移到了Ocelot.Administration裡,詳細的原始碼也就5個檔案組成,程式碼比較簡單,就不單獨講解了,就是配置管理介面地址,並使用IdentityServcer4進行認證,正好也符合我們我們專案的技術路線,為了把閘道器配置介面和閘道器使用介面區分,我們需要配置不同的Scope進行區分,由於本篇使用的IdentityServer4會在後續篇幅有完整介紹,本篇就直接列出實現程式碼,不做詳細的介紹。現在開始改造我們的閘道器程式碼,來整合後臺管理介面,然後測試通過授權介面更改配置資訊且立即生效。
public void ConfigureServices(IServiceCollection services)
{
Action<IdentityServerAuthenticationOptions> options = o =>
{
o.Authority = "http://localhost:6611"; //IdentityServer地址
o.RequireHttpsMetadata = false;
o.ApiName = "gateway_admin"; //閘道器管理的名稱,對應的為客戶端授權的scope
};
services.AddOcelot().AddAhphOcelot(option =>
{
option.DbConnectionStrings = "Server=.;Database=Ctr_AuthPlatform;User ID=sa;Password=bl123456;";
//option.EnableTimer = true;//啟用定時任務
//option.TimerDelay = 10 * 000;//週期10秒
}).AddAdministration("/CtrOcelot", options);
}
注意,由於
Ocelot.Administration
擴充套件使用的是OcelotMiddlewareConfigurationDelegate
中介軟體配置委託,所以我們擴充套件中介軟體AhphOcelotMiddlewareExtensions
需要增加擴充套件程式碼來應用此委託。
private static async Task<IInternalConfiguration> CreateConfiguration(IApplicationBuilder builder)
{
//提取檔案配置資訊
var fileConfig = await builder.ApplicationServices.GetService<IFileConfigurationRepository>().Get();
var internalConfigCreator = builder.ApplicationServices.GetService<IInternalConfigurationCreator>();
var internalConfig = await internalConfigCreator.Create(fileConfig.Data);
//如果配置檔案錯誤直接丟擲異常
if (internalConfig.IsError)
{
ThrowToStopOcelotStarting(internalConfig);
}
//配置資訊快取,這塊需要注意實現方式,因為後期我們需要改造下滿足分散式架構,這篇不做講解
var internalConfigRepo = builder.ApplicationServices.GetService<IInternalConfigurationRepository>();
internalConfigRepo.AddOrReplace(internalConfig.Data);
//獲取中介軟體配置委託(2018-11-12新增)
var configurations = builder.ApplicationServices.GetServices<OcelotMiddlewareConfigurationDelegate>();
foreach (var configuration in configurations)
{
await configuration(builder);
}
return GetOcelotConfigAndReturn(internalConfigRepo);
}
新建IdeitityServer
認證服務,並配置服務埠6666
,並新增二個測試客戶端,一個設定訪問scope為gateway_admin
,另外一個設定為其他,來分別測試認證效果。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IdentityServer4.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Ctr.AuthPlatform.TestIds4
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients());
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseIdentityServer();
}
}
public class Config
{
// scopes define the API resources in your system
public static IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>
{
new ApiResource("api1", "My API"),
new ApiResource("gateway_admin", "My admin API")
};
}
// clients want to access resources (aka scopes)
public static IEnumerable<Client> GetClients()
{
// client credentials client
return new List<Client>
{
new Client
{
ClientId = "client1",
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets =
{
new Secret("secret1".Sha256())
},
AllowedScopes = { "api1" }
},
new Client
{
ClientId = "client2",
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets =
{
new Secret("secret2".Sha256())
},
AllowedScopes = { "gateway_admin" }
}
};
}
}
}
配置好認證伺服器後,我們使用PostMan
來測試介面呼叫,首先使用有許可權的client2
客戶端,獲取access_token
,然後使用此access_token
訪問閘道器配置介面。
訪問http://localhost:7777/CtrOcelot/configuration
可以得到我們資料庫配置的結果。
我們再使用POST的方式修改配置資訊,使用PostMan測試如下,請求後返回狀態200(成功),然後測試修改前和修改後路由地址,發現立即生效,可以分別訪問http://localhost:7777/cjy/values
和http://localhost:7777/cjy/values
驗證即可。然後使用client1
獲取access_token,請求配置地址,提示401未授權,為預期結果,達到我們最終目的。
到此,我們閘道器就實現了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 [email protected] 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();
}
}
}
實現程式碼後如何擴充套件到我們的閘道器裡呢?只需要在注入時增加一個擴充套件即可。在ServiceCollectionExtensions
類中增加如下程式碼。
/// <summary>
/// 擴充套件使用Mysql儲存。
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IOcelotBuilder UseMySql(this IOcelotBuilder builder)
{
builder.Services.AddSingleton<IFileConfigurationRepository, MySqlFileConfigurationRepository>();
return builder;
}
然後修改網關注入程式碼。
public void ConfigureServices(IServiceCollection services)
{
Action<IdentityServerAuthenticationOptions> options = o =>
{
o.Authority = "http://localhost:6611"; //IdentityServer地址
o.RequireHttpsMetadata = false;
o.ApiName = "gateway_admin"; //閘道器管理的名稱,對應的為客戶端授權的scope
};
services.AddOcelot().AddAhphOcelot(option =>
{
option.DbConnectionStrings = "Server=localhost;Database=Ctr_AuthPlatform;User ID=root;Password=bl123456;";
//option.EnableTimer = true;//啟用定時任務
//option.TimerDelay = 10 * 000;//週期10秒
})
.UseMySql()
.AddAdministration("/CtrOcelot", options);
}
最後把mysql資料庫插入sqlserver一樣的路由測試資訊,然後啟動測試,可以得到我們預期的結果。為了方便大家測試,附mysql插入測試資料指令碼如下。
#插入全域性測試資訊
insert into AhphGlobalConfiguration(GatewayName,RequestIdKey,IsDefault,InfoStatus)
values('測試閘道器','test_gateway',1,1);
#插入路由分類測試資訊
insert into AhphReRoutesItem(ItemName,InfoStatus) values('測試分類',1);
#插入路由測試資訊
insert into AhphReRoute values(1,1,'/ctr/values','[ "GET" ]','','http','/api/Values','[{"Host": "localhost","Port": 9000 }]','','','','','','','',0,1);
#插入閘道器關聯表
insert into AhphConfigReRoutes values(1,1,1);
如果想擴充套件其他資料庫實現,直接參照此原始碼即可。
三、回顧與預告
本篇我們介紹了2種動態更新配置檔案的方法,實現訪問不同,各有利弊,正式使用時可以就實際情況選擇即可,都能達到我們的預期目標,也介紹了Ocelot擴充套件元件的使用和IdentityServer4的基礎入門資訊。然後又擴充套件了我們mysql資料庫的儲存方式,增加到了我們閘道器的擴充套件裡,隨時可以根據專案實際情況進行切換。
閘道器的儲存篇已經全部介紹完畢,有興趣的同學可以在此基礎上繼續拓展其他需求,下一篇我們將介紹使用redis來重寫Ocelot裡的所有快取,為我們後續的閘道器應用打下基礎。