微服务网关从零搭建——(七)更改存储方式为oracle
资源准备:
下载开源项目
新建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的更多相关文章
- 微服务网关从零搭建——(一)创建测试api以及api自动注入consul
本系列编写目的纯属个人开发记录 以下代码均为demo级 如有需要 请自行优化 代码完整包由于公司电脑加密 无法上传整包的demo文件 consul 开发环境简易处理 consul 下载地址 : ht ...
- 微服务网关从零搭建——(三)Ocelot网关 + identity4
增加验证服务 1.创建名为AuthService 的core 空项目 2.修改startup文件 using System; using System.Collections.Generic; usi ...
- 微服务网关从零搭建——(二)搭建api网关(不带验证)
环境准备 创建空的core2.1 api项目 演示使用名称APIGateWay 过程参考上一篇 完成后在appsettings.json 添加节点 "Setting": { & ...
- 微服务网关从零搭建——(九)网关部署到linux服务器
环境准备 公司电脑已安装core环境所以此处略过core环境安装 可参看此处 consul安装 如果没有wget命令 执行以下命令 yum install wget 下载consul wget htt ...
- 微服务网关从零搭建——(八)Ocelot网关中加入skywalking APM
准备工作 一.下载skywalking 本例使用的是 注: 1.解压后执行完2,3步骤后运行\bin\startup.bat 2.默认后台端口为8080 如需修改则修改\webapp\webapp.y ...
- 微服务网关从零搭建——(六)ocelot配置追踪功能
butterfly 准备工作 首先下载buterfly release版本 解压并通过命令启动:dotnet Butterfly.Web.dll --EnableHttpCollector=true ...
- 小D课堂 - 新版本微服务springcloud+Docker教程_6-06 zuul微服务网关集群搭建
笔记 6.Zuul微服务网关集群搭建 简介:微服务网关Zull集群搭建 1.nginx+lvs+keepalive https://www.cnblogs.com/liuyisai/ ...
- 微服务之:从零搭建ocelot网关和consul集群
介绍 微服务中有关键的几项技术,其中网关和服务服务发现,服务注册相辅相成. 首先解释几个本次教程中需要的术语 网关 Gateway(API GW / API 网关),顾名思义,是企业 IT 在系统边界 ...
- Spring Cloud微服务安全实战_4-1_微服务网关安全_概述&微服务安全面临的挑战
第四章 网关安全 这一章从简单的API的场景过渡到复杂的微服务的场景 4.1 概述 微服务安全面临的挑战:介绍中小企业的一个微服务架构,相比第三章的单体应用的简单的API所面临的哪些挑战 OAu ...
随机推荐
- 图像处理之基础---滤波器之高斯低通滤波器3c代码实现yuv,rgb
()高斯理论简介 () ()代码实现 四 使用高斯滤波器进行图像的平滑 ()高斯简介 http://research.microsoft.com/en-us/um/people/kahe/eccv10 ...
- 湖南省第九届大学生计算机程序设计竞赛 Interesting Calculator
Interesting Calculator Time Limit: 2 Sec Memory Limit: 128 MB Submit: 163 Solved: 49 Description T ...
- c#实现播放器的集中方式
http://www.cnblogs.com/iskyoole/archive/2012/03/25/2417181.html(原文链接地址) 一.使用vs自带的windows media play控 ...
- Python执行系统命令并获得输出的几种方法
[root@a upfc]# ./ffmpeg-linux64-v3.3.1 -i a.mp3 ffmpeg version N-86111-ga441aa90e8-static http://joh ...
- 【Dairy】2016.11.5
橘子洲一游
- [翻译]NUnit---Equality Asserts&& Identity Asserts (四)
Equality Asserts 这些方法测试两个参数是否相等.语言不自动装修的普通类型可以使用对应的重载的方法. Comparing Numerics of Different Types 比较两个 ...
- JsonFormat和DateTimeFormate格式化参数
JsonFormat :出参 DateTimeFormate : 入参 http://www.iteye.com/problems/53816 @DateTimeFormat(pattern = &q ...
- sql让时间调前,调后的语句
时间调前,调后 select billid,DATEADD(mm,2,billdate) from bi_Bill 注:用dateadd(/时间年/月/日,调前或后多少,字段) mm为月份,2为调前两 ...
- bzoj 1684: [Usaco2005 Oct]Close Encounter【数学(?)】
枚举分母,然后离他最近的分子只有两个,分别判断一下能不能用来更新答案即可 #include<iostream> #include<cstdio> #include<cma ...
- [Swift]圆周率π
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...