资源准备:

下载开源项目

新建oracle表:

-- ----------------------------
-- Table structure for OcelotGlobalConfiguration
-- ---------------------------- CREATE TABLE OcelotGlobalConfiguration (
Id NUMBER(11) NOT NULL ,
GatewayName NVARCHAR2(200) NOT NULL ,
RequestIdKey NVARCHAR2(100) ,
BaseUrl NVARCHAR2(100) ,
DownstreamScheme NVARCHAR2(50) ,
ServiceDiscoveryProvider NVARCHAR2(300) ,
QoSOptions NVARCHAR2(300) ,
LoadBalancerOptions NVARCHAR2(300) ,
HttpHandlerOptions NVARCHAR2(300) ,
LastUpdateTime DATE ,
AddTime DATE NOT NULL ,
IsDefault NUMBER(4) NOT NULL
)
;
COMMENT ON COLUMN OcelotGlobalConfiguration.Id IS '主键';
COMMENT ON COLUMN OcelotGlobalConfiguration.GatewayName IS '网关名称';
COMMENT ON COLUMN OcelotGlobalConfiguration.AddTime IS '添加时间';
COMMENT ON COLUMN OcelotGlobalConfiguration.IsDefault IS '是否默认'; -- ----------------------------
-- Records of OcelotGlobalConfiguration
-- ----------------------------
INSERT INTO OcelotGlobalConfiguration VALUES ('', '第一个网关', 'FirstGateway', '', '', '', '', '', '', TO_DATE('1900-01-01 00:00:00.000', 'SYYYY-MM-DD HH24:MI:SS'), TO_DATE('2018-10-22 18:06:22.337', 'SYYYY-MM-DD HH24:MI:SS'), '');
COMMIT; -- ----------------------------
-- Table structure for OcelotReRoutes
-- ---------------------------- CREATE TABLE OcelotReRoutes (
Id NUMBER(11) NOT NULL ,
OcelotGlobalConfigurationId NUMBER(11) NOT NULL ,
UpstreamPathTemplate NVARCHAR2(150) NOT NULL ,
UpstreamHttpMethod NVARCHAR2(50) NOT NULL ,
UpstreamHost NVARCHAR2(100) NOT NULL ,
DownstreamScheme NVARCHAR2(50) ,
DownstreamPathTemplate NVARCHAR2(200) ,
DownstreamHostAndPorts NVARCHAR2(500) ,
AuthenticationOptions NVARCHAR2(300) ,
RequestIdKey NVARCHAR2(100) ,
CacheOptions NVARCHAR2(200) ,
ServiceName NVARCHAR2(100) ,
QoSOptions NVARCHAR2(200) ,
LoadBalancerOptions NVARCHAR2(200) ,
Key NVARCHAR2(100) ,
DelegatingHandlers NVARCHAR2(200) ,
Priority NUMBER(11) ,
Timeout NUMBER(11) ,
IsStatus NUMBER(11) NOT NULL ,
AddTime DATE NOT NULL
)
;
COMMENT ON COLUMN OcelotReRoutes.Id IS '主键';
COMMENT ON COLUMN OcelotReRoutes.OcelotGlobalConfigurationId IS '全局配置ID';
COMMENT ON COLUMN OcelotReRoutes.AddTime IS '添加时间'; -- ----------------------------
-- Records of OcelotReRoutes
-- ----------------------------
INSERT INTO OcelotReRoutes VALUES ('', '', '/connect/token', '[ "POST","GET" ]', ' ', 'http', '/connect/token', '[{"Host": "localhost","Port": "5003" }]', '', '', '', '', '', '', '', '', '', NULL, '', TO_DATE('2018-10-22', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO OcelotReRoutes VALUES ('', '', '/connect/authorize', '[ "POST","GET" ]', ' ', 'http', '/connect/authorize', '[{"Host": "localhost","Port": "5003" }]', '', '', '', '', '', '', '', '', '', NULL, '', TO_DATE('2018-10-22', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO OcelotReRoutes VALUES ('', '', '/.well-known/openid-configuration', '[ "POST","GET" ]', ' ', 'http', '/.well-known/openid-configuration', '[{"Host": "localhost","Port": "5003" }]', '', '', '', '', '', '', '', '', '', NULL, '', TO_DATE('2018-10-22', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO OcelotReRoutes VALUES ('', '', '/.well-known/openid-configuration/jwks', '[ "POST","GET" ]', ' ', 'http', '/.well-known/openid-configuration', '[{"Host": "localhost","Port": "5003" }]', '', '', '', '', '', '', '', '', '', NULL, '', TO_DATE('2018-10-22', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO OcelotReRoutes VALUES ('', '', '/connect/userinfo', '[ "POST","GET" ]', ' ', 'http', '/connect/userinfo', '[{"Host": "localhost","Port": "5003" }]', '', '', '', '', '', '', '', '', '', NULL, '', TO_DATE('2018-10-22', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO OcelotReRoutes VALUES ('', '', '/connect/endsession', '[ "POST","GET" ]', ' ', 'http', '/connect/endsession', '[{"Host": "localhost","Port": "5003" }]', '', '', '', '', '', '', '', '', '', NULL, '', TO_DATE('2018-10-22', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO OcelotReRoutes VALUES ('', '', '/connect/checksession', '[ "POST","GET" ]', ' ', 'http', '/connect/checksession', '[{"Host": "localhost","Port": "5003" }]', '', '', '', '', '', '', '', '', '', NULL, '', TO_DATE('2018-10-22', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO OcelotReRoutes VALUES ('', '', '/connect/revocation', '[ "POST","GET" ]', ' ', 'http', '/connect/revocation', '[{"Host": "localhost","Port": "5003" }]', '', '', '', '', '', '', '', '', '', NULL, '', TO_DATE('2018-10-22', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO OcelotReRoutes VALUES ('', '', '/connect/introspect', '[ "POST","GET" ]', ' ', 'http', '/connect/introspect', '[{"Host": "localhost","Port": "5003" }]', '', '', '', '', '', '', '', '', '', NULL, '', TO_DATE('2018-10-22', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO OcelotReRoutes VALUES ('', '', '/connect/authorize/callback', '[ "POST","GET" ]', ' ', 'http', '/connect/authorize/callback', '[{"Host": "localhost","Port": "5003" }]', '', '', '', '', '', '', '', '', '', NULL, '', TO_DATE('2018-10-22', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO OcelotReRoutes VALUES ('', '', '/connect/endsession/callback', '[ "POST","GET" ]', ' ', 'http', '/connect/endsession/callback', '[{"Host": "localhost","Port": "5003" }]', '', '', '', '', '', '', '', '', '', NULL, '', TO_DATE('2018-10-22', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO OcelotReRoutes VALUES ('', '', '/ss1/{everything}', '[ "POST","GET" ]', ' ', 'http', '/api/{everything}', ' [{"Host": "localhost","Port": "5003" },{"Host": "localhost","Port": "1002" }]', '', '', '', '', '', '', '', '', '', NULL, '', TO_DATE('2018-10-22', 'SYYYY-MM-DD HH24:MI:SS'));
COMMIT; -- ----------------------------
-- Primary Key structure for table OcelotGlobalConfiguration
-- ----------------------------
ALTER TABLE OcelotGlobalConfiguration ADD CONSTRAINT PK_OcelotGlobalConfiguration PRIMARY KEY (Id); -- ----------------------------
-- Primary Key structure for table OcelotReRoutes
-- ----------------------------
ALTER TABLE OcelotReRoutes ADD CONSTRAINT PK_OcelotReRoutes PRIMARY KEY (Id);

建表语句

注: 修改UPSTREAMHOST 字段为可为空

开始集成:

1.在网关项目中添加对Ocelot.ConfigAuthLimitCache的引用

2.在网关项目的Startup.cs 中修改ConfigureServices节点  标红部分

  services.AddOcelot(Configuration).AddAuthLimitCache(opt=> {
opt.DbConnectionStrings = Configuration.GetSection("Setting")["ConfigDBConnction"];
}).AddConsul();

3.在配置文件中添加数据库连接 蓝色部分

 "Setting": {
"Port": "",
"ConfigDBConnction": "Data Source=192.168.xxx.xxx/orcl;User ID=xxx;Password=xxx;"
},

4.在Program.cs中移除对原配置文件的依赖  注释部分

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls($"http://*:{StartPort}");
//.ConfigureAppConfiguration((hostingContext, builder) =>
//{
// builder.AddJsonFile("configuration.json", false, true);
//});

5.修改Ocelot.ConfigAuthLimitCache项目的数据仓储实现

增加文件

代码:

using Dapper;
using Ocelot.Cache;
using Ocelot.ConfigAuthLimitCache.Configuration;
using Ocelot.ConfigAuthLimitCache.Extensions;
using Ocelot.ConfigAuthLimitCache.Models;
using Ocelot.Configuration.File;
using Ocelot.Configuration.Repository;
using Ocelot.Logging;
using Ocelot.Responses;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Threading.Tasks; namespace Ocelot.ConfigAuthLimitCache.Repository
{
/// <summary>
/// nontracey
/// 2019.04-08
/// 实现从oracle数据库中提取配置信息
/// </summary>
public class OracleFileConfigurationRepository : IFileConfigurationRepository
{
private readonly IOcelotCache<FileConfiguration> _cache;
private readonly IOcelotLogger _logger;
private readonly ConfigAuthLimitCacheOptions _option;
public OracleFileConfigurationRepository(ConfigAuthLimitCacheOptions option, IOcelotCache<FileConfiguration> cache, IOcelotLoggerFactory loggerFactory)
{
_option = option;
_cache = cache;
_logger = loggerFactory.CreateLogger<OracleFileConfigurationRepository>();
} public Task<Response> Set(FileConfiguration fileConfiguration)
{
_cache.AddAndDelete(_option.CachePrefix + "FileConfiguration", fileConfiguration, TimeSpan.FromSeconds(), "");
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 * from OcelotGlobalConfiguration where IsDefault=1 and rownum=1";
//提取全局配置信息
using (var connection = new OracleConnection(_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 > )
{
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 ?? ;
m.RequestIdKey = model.RequestIdKey;
m.ServiceName = model.ServiceName;
m.Timeout = model.Timeout ?? ;
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 == )
{
return new OkResponse<FileConfiguration>(null);
}
return new OkResponse<FileConfiguration>(file);
}
}
}

增加oracle仓储

注:需要添加之前的CommonHelper引用或者 单独添加Oracle.ManagedDataAccess.Core nuget包引用

至此搭建完毕 将配置文件部分的内容移动到数据库即可。

参考链接:

更改到sqlserver

https://www.cnblogs.com/yilezhu/p/9839863.html

微服务网关从零搭建——(七)更改存储方式为oracle的更多相关文章

  1. 微服务网关从零搭建——(一)创建测试api以及api自动注入consul

    本系列编写目的纯属个人开发记录  以下代码均为demo级 如有需要 请自行优化 代码完整包由于公司电脑加密 无法上传整包的demo文件 consul 开发环境简易处理 consul 下载地址 : ht ...

  2. 微服务网关从零搭建——(三)Ocelot网关 + identity4

    增加验证服务 1.创建名为AuthService 的core 空项目 2.修改startup文件 using System; using System.Collections.Generic; usi ...

  3. 微服务网关从零搭建——(二)搭建api网关(不带验证)

    环境准备 创建空的core2.1 api项目  演示使用名称APIGateWay  过程参考上一篇 完成后在appsettings.json 添加节点 "Setting": { & ...

  4. 微服务网关从零搭建——(九)网关部署到linux服务器

    环境准备 公司电脑已安装core环境所以此处略过core环境安装 可参看此处 consul安装 如果没有wget命令 执行以下命令 yum install wget 下载consul wget htt ...

  5. 微服务网关从零搭建——(八)Ocelot网关中加入skywalking APM

    准备工作 一.下载skywalking 本例使用的是 注: 1.解压后执行完2,3步骤后运行\bin\startup.bat 2.默认后台端口为8080 如需修改则修改\webapp\webapp.y ...

  6. 微服务网关从零搭建——(六)ocelot配置追踪功能

    butterfly 准备工作 首先下载buterfly release版本 解压并通过命令启动:dotnet Butterfly.Web.dll --EnableHttpCollector=true ...

  7. 小D课堂 - 新版本微服务springcloud+Docker教程_6-06 zuul微服务网关集群搭建

    笔记 6.Zuul微服务网关集群搭建     简介:微服务网关Zull集群搭建 1.nginx+lvs+keepalive      https://www.cnblogs.com/liuyisai/ ...

  8. 微服务之:从零搭建ocelot网关和consul集群

    介绍 微服务中有关键的几项技术,其中网关和服务服务发现,服务注册相辅相成. 首先解释几个本次教程中需要的术语 网关 Gateway(API GW / API 网关),顾名思义,是企业 IT 在系统边界 ...

  9. Spring Cloud微服务安全实战_4-1_微服务网关安全_概述&微服务安全面临的挑战

      第四章  网关安全 这一章从简单的API的场景过渡到复杂的微服务的场景 4.1 概述 微服务安全面临的挑战:介绍中小企业的一个微服务架构,相比第三章的单体应用的简单的API所面临的哪些挑战 OAu ...

随机推荐

  1. 【Struts2五】ValueStack以及ognl表达式二(经常使用标签)

    Ognl经常使用标签:   1.s:debug       假设把该标签放入到s:iterator中能够看到当前正在迭代的元素的状态    2.s:property       1.输出       ...

  2. Codeforces(429D - Tricky Function)近期点对问题

    D. Tricky Function time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. 【bzoj1042】[HAOI2008]硬币购物

    首先使用DP预处理,先求出,在不考虑每种硬币个数的限制的情况下,每个钱数有多少种拼凑方案. 为了避免重复的方案被转移,所以我们以硬币种类为第一层循环,这样阶段性的增加硬币. 一定要注意这个第一层循环要 ...

  4. BSGS算法及拓展

    https://www.zybuluo.com/ysner/note/1299836 定义 一种用来求解高次同余方程的算法. 一般问题形式:求使得\(y^x\equiv z(mod\ p)\)的最小非 ...

  5. bzoj4873

    http://www.lydsy.com/JudgeOnline/problem.php?id=4873 最大权闭合子图... 建图: 1.d[i][j]:i->j区间的费用,d[i][j] & ...

  6. 30. extjs getEl方法 怎么用

    转自:https://blog.csdn.net/evilcry2012/article/details/50586861 2014-10-27 11:57 提问者采纳   getEl = compo ...

  7. C++中的常量(一) const限定符

    最近在重新看<<C++ Primer>>,第一遍的时候const和constexpr看得并不太懂,这次又有了些更新的理解,当然可能仍然有许多不对的地方... 首先,const限 ...

  8. E20170602-ts

    questionnaire  n. 调查问卷; 调查表;  アンケート不是英语 collection   n. 征收; 收集,采集; 收藏品; 募捐; association   n. 联想; 协会, ...

  9. bzoj 1635: [Usaco2007 Jan]Tallest Cow 最高的牛【差分】

    s[i]为差分后的"i这头牛前有几头比它高",计算答案的时候加成前缀和,假设第一头最高减一下即可 用map记录一下被加过的区间,避免重复 #include<iostream& ...

  10. redis在linux的安装和开机启动(二)

    编译 安装 makefile已经存在 执行make 即可 make之后, 自动创建可运行的脚本文件, 不需要再执行 install了. 将脚本文件, 拷贝到指定位置, 就可以了. 手动创建目录, 需要 ...