{
"ReRoutes":[
{
"DownstreamPathTemplate": "/api/post/{postId}", // 下游请求的API
"DownstreamScheme": "https", //请求协议,目前应该是支持http和https
"DownstreamHostAndPorts": [
{
"Host": "localhost", // 下游API的IP
"Port": 80, //下游API的PORT
}
],
"UpstreamPathTemplate": "/post/{postId}", // 上游请求的API,即客户端请求到API Gateway的请求
"UpstreamHttpMethod": [ "Get"] , // 允许请求的方法组
"Priority": 1 //对多个产生冲突的路由设置优化级
"RouteClaimsRequirement": { //标记该路由是否需要认证
"UserType": "registered" //TOKEN,示例,K/V形式,授权声明,授权token中会包含一些claim,如填写则会判断是否和token中的一致,不一致则不准访问
},
-----------负载均衡---------------
"LoadBalancer": "LeastConnection",
--------------限流------------------
"RateLimitOptions": {
"ClientWhitelist": [], //ClientWihteList 白名单
"EnableRateLimiting": true, //EnableRateLimiting 是否启用限流
"Period": "1s", //Period 统计时间段:1s, 5m, 1h, 1d
"PeriodTimespan": 1, //PeroidTimeSpan 多少秒之后客户端可以重试
"Limit": 1 //Limit 在统计时间段内允许的最大请求数量
}
--------------------服务质量与熔断--------------------
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking":3, //ExceptionsAllowedBeforeBreaking 允许多少个异常请求
"DurationOfBreak":5, //DurationOfBreak 熔断的时间,单位为秒/
"TimeoutValue":5000 //TimeoutValue 如果下游请求的处理时间超过多少则自如将请求设置为超时
}
--------------缓存--------------
"FileCacheOptions": { "TtlSeconds": 15, "Region": "somename" } //Region是对缓存进行的一个分区,我们可以调用Ocelot的 administration API来移除某个区下面的缓存 。
-----------------认证--------------------- //如果我们需要对下游API进行认证以及鉴权服务的
"AuthenticationOptions": {
"AuthenticationProviderKey": "TestKey", // 认证时的key
"AllowedScopes": [] //这里的Scopes将从当前 token 中的 claims中来获取,我们的鉴权服务将依靠于它来实现 。当前路由的下游API需要某个权限时,我们需要在这里声明 。和oAuth2中的 scope意义一致。
}
----------------鉴权-------------------
"RouteClaimsRequirement": { //当前请求上下文的token中所带的claims如果没有 name=”UserType” 并且 value=”registered” 的话将无法访问下游服务。
"UserType": "registered"
}
-------------Claims转化-----------------
-----Claims to Claims
"AddClaimsToRequest": {
"UserType": "Claims[sub] > value[0] > |",
"UserId": "Claims[sub] > value[1] > |"
}
-------Claims to Headers
"AddHeadersToRequest": {
"CustomerId": "Claims[sub] > value[1] > |"
}
-----Claims to Query String
"AddQueriesToRequest": {
"LocationId": "Claims[LocationId] > value",
}
"UseServiceDiscovery": false // 是否使用配置服务发现
}
}
]
"GlobalConfiguration": { --全局的配置
"BaseUrl": "http://localhost:5002" --网关服务器启动时的地址
"RequestIdKey": "Oc_RequestId", --
"ServiceDiscoveryProvider": { --服务发现与检查的服务器地址
"Provider": "Consul", --Consul 服务发现框架
"Host": "localhost",
"Port": 8500,
"ConfigurationKey": "Oceolot_A" --当使用 Consul服务的时候,动态的获取配置,如果未配置key,则使用默认的配置,如果有,则使用指定的 consul配置文件
}
}
在 GlobalConfiguration下我们还可以进行以下配置
"RateLimitOptions": {
"DisableRateLimitHeaders": false,
"QuotaExceededMessage": "Customize Tips!",
"HttpStatusCode": 999,
"ClientIdHeader" : "Test"
}
Http头 X-Rate-Limit 和 Retry-After 是否禁用
QuotaExceedMessage 当请求过载被截断时返回的消息
HttpStatusCode 当请求过载被截断时返回的http status
ClientIdHeader 用来识别客户端的请求头,默认是 ClientId
// ConfigurationKey:
Well,should we add a node(something named like ConfigurationKey)
for the ServiceDiscoveryProvider node in ocelot.json
,besides the Provider,Host and Port?
And when fetching configuration from Consul,
ConsulFileConfigurationRepository may use the ConfigurationKey first,
then the default key(InternalConfiguration) if ConfigurationKey is empty.
LoadBalancer将决定负载均衡的算法
LeastConnection – 将请求发往最空闲的那个服务器
RoundRobin – 轮流发送
NoLoadBalance – 总是发往第一个请求或者是服务发现