【ocelot】ocelot第一步配置(配置文件到数据库中)
配置环境.net core2.2
数据库sql server2010
vs2019
第一步:然后在sqlserver上(在自己库中也可以创建一个新的库)执行如下两个sql创建表
USE [database] GO /****** Object: Table [dbo].[OcelotGlobalConfiguration] Script Date: 2019-11-22 10:36:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[OcelotGlobalConfiguration]( [Id] [int] IDENTITY(1,1) NOT NULL, [GatewayName] [nvarchar](200) NOT NULL, [RequestIdKey] [nvarchar](100) NULL, [BaseUrl] [nvarchar](100) NULL, [DownstreamScheme] [nvarchar](50) NULL, [ServiceDiscoveryProvider] [nvarchar](300) NULL, [QoSOptions] [nvarchar](300) NULL, [LoadBalancerOptions] [nvarchar](300) NULL, [HttpHandlerOptions] [nvarchar](300) NULL, [LastUpdateTime] [datetime] NULL, [AddTime] [datetime] NOT NULL, [IsDefault] [bit] NOT NULL, CONSTRAINT [PK_OcelotGlobalConfiguration] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[OcelotGlobalConfiguration] ADD CONSTRAINT [DF_OcelotGlobalConfiguration_AddTime] DEFAULT (getdate()) FOR [AddTime] GO ALTER TABLE [dbo].[OcelotGlobalConfiguration] ADD CONSTRAINT [DF_OcelotGlobalConfiguration_IsDefault] DEFAULT ((1)) FOR [IsDefault] GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OcelotGlobalConfiguration', @level2type=N'COLUMN',@level2name=N'Id' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'网关名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OcelotGlobalConfiguration', @level2type=N'COLUMN',@level2name=N'GatewayName' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'添加时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OcelotGlobalConfiguration', @level2type=N'COLUMN',@level2name=N'AddTime' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'是否默认' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OcelotGlobalConfiguration', @level2type=N'COLUMN',@level2name=N'IsDefault' GO
USE [database] GO /****** Object: Table [dbo].[OcelotReRoutes] Script Date: 2019-11-22 10:36:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[OcelotReRoutes]( [Id] [int] IDENTITY(1,1) NOT NULL, [OcelotGlobalConfigurationId] [int] NOT NULL, [UpstreamPathTemplate] [nvarchar](150) NOT NULL, [UpstreamHttpMethod] [nvarchar](50) NOT NULL, [UpstreamHost] [nvarchar](100) NOT NULL, [DownstreamScheme] [nvarchar](50) NULL, [DownstreamPathTemplate] [nvarchar](200) NULL, [DownstreamHostAndPorts] [nvarchar](500) NULL, [AuthenticationOptions] [nvarchar](300) NULL, [RequestIdKey] [nvarchar](100) NULL, [CacheOptions] [nvarchar](200) NULL, [ServiceName] [nvarchar](100) NULL, [QoSOptions] [nvarchar](200) NULL, [LoadBalancerOptions] [nvarchar](200) NULL, [Key] [nvarchar](100) NULL, [DelegatingHandlers] [nvarchar](200) NULL, [Priority] [int] NULL, [Timeout] [int] NULL, [IsStatus] [int] NOT NULL, [AddTime] [date] NOT NULL, CONSTRAINT [PK_OcelotReRoutes] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[OcelotReRoutes] ADD CONSTRAINT [DF_OcelotReRoutes_IsStatus] DEFAULT ((1)) FOR [IsStatus] GO ALTER TABLE [dbo].[OcelotReRoutes] ADD CONSTRAINT [DF_OcelotReRoutes_AddTime] DEFAULT (getdate()) FOR [AddTime] GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OcelotReRoutes', @level2type=N'COLUMN',@level2name=N'Id' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'全局配置ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OcelotReRoutes', @level2type=N'COLUMN',@level2name=N'OcelotGlobalConfigurationId' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'添加时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OcelotReRoutes', @level2type=N'COLUMN',@level2name=N'AddTime' GO
第二步:安装 Ocelot.Encapsulation包,主要是给ocelot的封装。该nuget包是基于 https://github.com/yilezhu/Ocelot.ConfigAuthLimitCache,其中做了一些改动,具体包还是以包指定的源码连接。
然后在Startup>ConfigureServices中插入如下代码
services.AddOcelot() .AddAuthLimitCache(option => { option.DbConnectionStrings = "Server=.;Database=Ocelot;User ID=sa;Password=sasa;";//Database是自己刚刚生成那个数据的库 });
以及在Startup>Configure插入如下代码
app.UseAhphOcelot((ocelotBuilder, pipelineConfiguration) => { }).Wait();
请根据如上图的配置规则结束。