Ocelot简易教程(三)之主要特性及路由详解
上篇《Ocelot简易教程(二)之快速开始2》教大家如何快速跑起来一个ocelot实例项目,也只是简单的对Ocelot进行了配置,这篇文章会给大家详细的介绍一下Ocelot的配置信息。希望能对大家深入使用Ocelot有所帮助。
上篇中也提到了,最简单的Ocelot如下面所示,只有简单的两个节点,一个是ReRoutes
,另一个就是GlobalConfiguration
关于这两个节点的作用,上篇也已经讲述了,这里再简单的讲下ReRoutes:告诉Ocelot如何处理上游的请求。GlobalConfiguration:顾名思义就是全局配置,此节点的配置允许覆盖ReRoutes里面的配置,你可以在这里进行通用的一些配置信息。
{
"ReRoutes": [],
"GlobalConfiguration": {}
}
下面呢给出ReRoute 的所有的配置信息,当然在实际使用的时候你没有必要全部进行配置,只需要根据你项目的实际需要进行相关的配置就可以了。
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{everything}",//下游路由模板
"UpstreamPathTemplate": "/good/{everything}",//上游路由模板
"UpstreamHttpMethod": [ "Get", "Post" ],//上游请求方法
"AddHeadersToRequest": {},
"UpstreamHeaderTransform": {},
"DownstreamHeaderTransform": {},
"AddClaimsToRequest": {},
"RouteClaimsRequirement": {},
"AddQueriesToRequest": {},
"RequestIdKey": null,
"FileCacheOptions": {
"TtlSeconds": 0,
"Region": null
},
"ReRouteIsCaseSensitive": false,
"ServiceName": null,
"DownstreamScheme": "http",
"QoSOptions": {//Qos相关配置
"ExceptionsAllowedBeforeBreaking": 0,
"DurationOfBreak": 0,
"TimeoutValue": 0
},
"LoadBalancerOptions": {//负载均衡相关选项
"Type": "RoundRobin",
"Key": null,
"Expiry": 0
},
"RateLimitOptions": {//限流相关配置
"ClientWhitelist": [],
"EnableRateLimiting": false,
"Period": null,
"PeriodTimespan": 0.0,
"Limit": 0
},
"AuthenticationOptions": {//认证相关选项
"AuthenticationProviderKey": null,
"AllowedScopes": []
},
"HttpHandlerOptions": {//HttpHandler相关的配置
"AllowAutoRedirect": false,//是否对下游重定向进行响应
"UseCookieContainer": false,//是否启动CookieContainer储存cookies
"UseTracing": false,
"UseProxy": true
},
"DownstreamHostAndPorts": [//下游端口及host
{
"Host": "localhost",
"Port": 1001
},
{
"Host": "localhost",
"Port": 1002
}
],
"UpstreamHost": null,//上游Host
"Key": null,
"DelegatingHandlers": [],
"Priority": 1,
"Timeout": 0,
"DangerousAcceptAnyServerCertificateValidator": false
}
当然上面的配置项我就不一一的进行介绍,因为很多配置相信大家根据意思都能知道个大概了。我只会对比较常用的配置做下介绍。而且在接下来的文章中对对每个节点进行单独的详细的介绍。在介绍之前呢先看Ocelot的几个特性。
Ocelot特性介绍
合并配置文件
这个特性允许用户创建多个配置文件来方便的对大型项目进行配置。试想一下,如果你的项目有几十个路由规则需要配置的话,那么在一个配置文件进行配置应该很痛苦吧,有了这个特性后,你就可以创建多个配置文件。Ocelot会自动合并他们。
在加载配置文件的时候 你可以通过下面的方式来调用AddOcelot()方法来替换直接加载某个配置的写法 如:AddJsonFile(“ocelot.json”)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
.AddOcelot()
.AddEnvironmentVariables();
})
在这种情况下,Ocelot会寻找所有匹配了 (?i)ocelot.([a-zA-Z0-9]*).json
的文件,然后合并他们。如何你要设置GlobalConfiguration 属性,那么你需要建立一个ocelot.global.json 的文件来进行全局的配置。
这里上一个例子吧!可以方便大家的理解。
新建一个ocelot.good.json文件,并加入下面的配置:
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 1001
},
{
"Host": "localhost",
"Port": 1002
}
],
"UpstreamPathTemplate": "/good/{everything}",
"UpstreamHttpMethod": [ "Get", "Post" ],
"LoadBalancerOptions": {
"Type": "RoundRobin"
}
}
]
}
然后再新建一个ocelot.order.json文件,并加入下面的配置:
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 1001
},
{
"Host": "localhost",
"Port": 1002
}
],
"UpstreamPathTemplate": "/order/{everything}",
"UpstreamHttpMethod": [ "Get", "Post" ],
"LoadBalancerOptions": {
"Type": "RoundRobin"
}
}
]
}
最后新建一个ocelot.all.json文件,并把上篇文章中的路由拷贝到里面:
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 1001
},
{
"Host": "localhost",
"Port": 1002
}
],
"UpstreamPathTemplate": "/{everything}",
"UpstreamHttpMethod": [ "Get", "Post" ],
"LoadBalancerOptions": {
"Type": "RoundRobin"
}
}
],
"GlobalConfiguration": {
}
}
然后修改下,Program.cs文件中的代码如下:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
.AddOcelot()
.AddEnvironmentVariables();
})
.UseUrls("http://localhost:1000")
.UseStartup<Startup>();
这里最重要的代码就是config.AddOcelot()
了。这段代码就会按照上面的规则查找所有符合条件的文件并合并路由。合并后的代码如下:
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{everything}",
"UpstreamPathTemplate": "/{everything}",
"UpstreamHttpMethod": [ "Get", "Post" ],
"AddHeadersToRequest": {},
"UpstreamHeaderTransform": {},
"DownstreamHeaderTransform": {},
"AddClaimsToRequest": {},
"RouteClaimsRequirement": {},
"AddQueriesToRequest": {},
"RequestIdKey": null,
"FileCacheOptions": {
"TtlSeconds": 0,
"Region": null
},
"ReRouteIsCaseSensitive": false,
"ServiceName": null,
"DownstreamScheme": "http",
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": 0,
"DurationOfBreak": 0,
"TimeoutValue": 0
},
"LoadBalancerOptions": {
"Type": "RoundRobin",
"Key": null,
"Expiry": 0
},
"RateLimitOptions": {
"ClientWhitelist": [],
"EnableRateLimiting": false,
"Period": null,
"PeriodTimespan": 0.0,
"Limit": 0
},
"AuthenticationOptions": {
"AuthenticationProviderKey": null,
"AllowedScopes": []
},
"HttpHandlerOptions": {
"AllowAutoRedirect": false,
"UseCookieContainer": false,
"UseTracing": false,
"UseProxy": true
},
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 1001
},
{
"Host": "localhost",
"Port": 1002
}
],
"UpstreamHost": null,
"Key": null,
"DelegatingHandlers": [],
"Priority": 1,
"Timeout": 0,
"DangerousAcceptAnyServerCertificateValidator": false
},
{
"DownstreamPathTemplate": "/api/{everything}",
"UpstreamPathTemplate": "/good/{everything}",
"UpstreamHttpMethod": [ "Get", "Post" ],
"AddHeadersToRequest": {},
"UpstreamHeaderTransform": {},
"DownstreamHeaderTransform": {},
"AddClaimsToRequest": {},
"RouteClaimsRequirement": {},
"AddQueriesToRequest": {},
"RequestIdKey": null,
"FileCacheOptions": {
"TtlSeconds": 0,
"Region": null
},
"ReRouteIsCaseSensitive": false,
"ServiceName": null,
"DownstreamScheme": "http",
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": 0,
"DurationOfBreak": 0,
"TimeoutValue": 0
},
"LoadBalancerOptions": {
"Type": "RoundRobin",
"Key": null,
"Expiry": 0
},
"RateLimitOptions": {
"ClientWhitelist": [],
"EnableRateLimiting": false,
"Period": null,
"PeriodTimespan": 0.0,
"Limit": 0
},
"AuthenticationOptions": {
"AuthenticationProviderKey": null,
"AllowedScopes": []
},
"HttpHandlerOptions": {
"AllowAutoRedirect": false,
"UseCookieContainer": false,
"UseTracing": false,
"UseProxy": true
},
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 1001
},
{
"Host": "localhost",
"Port": 1002
}
],
"UpstreamHost": null,
"Key": null,
"DelegatingHandlers": [],
"Priority": 1,
"Timeout": 0,
"DangerousAcceptAnyServerCertificateValidator": false
},
{
"DownstreamPathTemplate": "/api/{everything}",
"UpstreamPathTemplate": "/order/{everything}",
"UpstreamHttpMethod": [ "Get", "Post" ],
"AddHeadersToRequest": {},
"UpstreamHeaderTransform": {},
"DownstreamHeaderTransform": {},
"AddClaimsToRequest": {},
"RouteClaimsRequirement": {},
"AddQueriesToRequest": {},
"RequestIdKey": null,
"FileCacheOptions": {
"TtlSeconds": 0,
"Region": null
},
"ReRouteIsCaseSensitive": false,
"ServiceName": null,
"DownstreamScheme": "http",
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": 0,
"DurationOfBreak": 0,
"TimeoutValue": 0
},
"LoadBalancerOptions": {
"Type": "RoundRobin",
"Key": null,
"Expiry": 0
},
"RateLimitOptions": {
"ClientWhitelist": [],
"EnableRateLimiting": false,
"Period": null,
"PeriodTimespan": 0.0,
"Limit": 0
},
"AuthenticationOptions": {
"AuthenticationProviderKey": null,
"AllowedScopes": []
},
"HttpHandlerOptions": {
"AllowAutoRedirect": false,
"UseCookieContainer": false,
"UseTracing": false,
"UseProxy": true
},
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 1001
},
{
"Host": "localhost",
"Port": 1002
}
],
"UpstreamHost": null,
"Key": null,
"DelegatingHandlers": [],