微服务网关从零搭建——(七)更改存储方式为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 ...
随机推荐
- Woody的逻辑游戏--怎样换轮胎
题目:有一个做长途运输的司机要出发了,他用作运输的车是三轮车.轮胎的寿命是2万里,如今他要进行5万里的长途运输.计划用8个轮胎完毕运输任务,如何才干做到呢? 首先将轮胎从1-8依次编号,然后例如以下所 ...
- iOS开发——基础篇——get和post请求的区别
HTTP 定义了与服务器交互的不同方法,最常用的有4种,Get.Post.Put.Delete,如果我换一下顺序就好记了,Put(增),Delete(删),Post(改),Get(查),即增删改查,下 ...
- 在vs2017中创建Node.js项目
https://github.com/Microsoft/nodejstools/wiki/Projects 1. 安装vs2017的时候,需要勾选Node.js 2.新建项目的时候,选择其他语言,然 ...
- python - 使用traceback获取详细的异常信息
try: 1/0except Exception,e: print e 输出结果是integer division or modulo by zero,只知道是报了这个错,但是却不知道在哪个文件哪个函 ...
- 排名Top 100的Java类库
和去年一样,排名第一的类库,依旧是JUnit.基于它扩展的 JUnit Runner 占据第二名的位置,甚至是较旧的 junit.framework 此次也在第三名的位置.也就是说JUnit包揽了前三 ...
- 杂项-Java:Struct
ylbtech-杂项-Java:Struct Struts是Apache软件基金会(ASF)赞助的一个开源项目.它最初是Jakarta项目中的一个子项目,并在2004年3月成为ASF的顶级项目.它通过 ...
- Mysql中分页查询两个方法比较
mysql中分页查询有两种方式, 一种是使用COUNT(*)的方式,具体代码如下 1 2 3 SELECT COUNT(*) FROM foo WHERE b = 1; SELECT a FROM ...
- 18. 视图Ext.Viewport和窗口Ext.Window用法
转自:http://www.cnblogs.com/linjiqin/archive/2011/06/22/2087003.html 视图Ext.Viewport和窗口Ext.Window用法. 1. ...
- bzoj 2763: [JLOI2011]飞行路线【分层图+spfa】
为什么早年的题总是从0开始标号啊--又zz了一次WA 分层图的题只有这一个套路吧,建分层图,然后优化时间是分层跑spfa然后层与层之间单独跑即可 #include<iostream> #i ...
- Linux day01(二)虚拟机快照和克隆的用法介绍
一:快照 优点:运行虚拟机后不用担心系统会被弄崩溃了,点击快照会立即恢复到初始状态 缺点:回滚会带来数据的丢失,所以要考虑数据恢复的成本和找回数据时进行操作的成本 1. 在导航栏中找虚拟机快照的小图标 ...