ocelot网关启用动态路由
为了简化ocelot网关路由配置,可以考虑使用动态路由模式。下面是相关的英文介绍:
翻译过来大概是说如果配合服务发现(例如consul),ocelot可以启用动态路由功能。例如:
访问http://api.hello.com/user/register这个gateway网址时,自动查询consul中是否有user服务,如果有(假设user服务的host为user.hello.com),则动态转发为http://user.hello.com/register。
要启用这个功能,只需要2个配置:
1、Routes、Reroutes只能是空节点,不能包含任何配置
2、在全局配置中设定DownstreamScheme,例如http
3、可以在全局配置中设定其他设定,例如RateLimitOptions、QoSOptions等
大概像下面:
{
"Routes": [],
"Aggregates": [],
"GlobalConfiguration": {
"RequestIdKey": null,
"ServiceDiscoveryProvider": {
"Host": "localhost",
"Port": 8500,
"Type": "Consul",
"Token": null,
"ConfigurationKey": null
},
"RateLimitOptions": {
"ClientIdHeader": "ClientId",
"QuotaExceededMessage": null,
"RateLimitCounterPrefix": "ocelot",
"DisableRateLimitHeaders": false,
"HttpStatusCode": 429
},
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": 0,
"DurationOfBreak": 0,
"TimeoutValue": 0
},
"BaseUrl": null,
"LoadBalancerOptions": {
"Type": "LeastConnection",
"Key": null,
"Expiry": 0
},
"DownstreamScheme": "http",
"HttpHandlerOptions": {
"AllowAutoRedirect": false,
"UseCookieContainer": false,
"UseTracing": false
}
}
}