[译]Ocelot - Routing
Ocelot主要的功能就是将http请求转发到对应的下游服务上去。
Ocelot将一个请求路由到另外一个路由的动作叫做ReRoute
。为了能让Ocelot能正常工作,需要在配置中设置ReRoute
。
{
"ReRoutes": [
]
}
配置ReRoute
需要添加一个ReRoutes
json 数组。
{
"DownstreamPathTemplate": "/api/posts/{postId}",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 80,
}
],
"UpstreamPathTemplate": "/posts/{postId}",
"UpstreamHttpMethod": [ "Put", "Delete" ]
}
DownstreamPathTemplate
, DownstreamScheme
和DownstreamHostAndPorts
定义请求需要转发到哪个URL上去。
DownstreamHostAndPorts
是一个集合,它定义了要转发到的下游服务的host和port。通常这个集合只需要一个条目就可以了,但是如果你想要搞负载均衡的话,可以在这指定多个条目。
UpstreamPathTemplate
定义了ocelot用哪个发到ocelot的请求处理DownstreamPathTemplate
。Ocelot根据UpstreamHttpMethod
来针对同一个URL不同请求方式分别进行对应的处理。如果没指定UpstreamHttpMethod
的话,那就是针对所有的HTTP请求方式。
可以为Template中的变量添加placeholder(以{something}的形式)。placeholder变量必须同时出现在DownstreamPathTemplate
和UpstreamPathTemplate
中。
可以通过下面做一个通用的默认ReRoute:
{
"DownstreamPathTemplate": "/api/{everything}",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 80,
}
],
"UpstreamPathTemplate": "/{everything}",
"UpstreamHttpMethod": [ "Get", "Post" ]
}
上面的代码会将所有的path+query组合起来发到下流服务的/api路径下。
ReRouting配置大小写不敏感。
可以通过下面的配置设置大小写敏感:
"ReRouteIsCaseSensitive": true
Catch All
如果你的配置如下,所有的请求将直接发送,上下游的url地址会完全一致。 {url}名字可以随便改,不是一个专用的名字。
{
"DownstreamPathTemplate": "/{url}",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 80,
}
],
"UpstreamPathTemplate": "/{url}",
"UpstreamHttpMethod": [ "Get" ]
}
catch all的优先级最低。如果你有下面的一个ReRoute,那么会优先匹配这个ReRoute,而不是Catch all:
{
"DownstreamPathTemplate": "/",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "10.0.10.1",
"Port": 80,
}
],
"UpstreamPathTemplate": "/",
"UpstreamHttpMethod": [ "Get" ]
}
Upstream Host
使用UpstreamHost设置基于upstream host的ReRoute。通过客户端请求的host来决定使用哪个ReRoute。
通过下面的配置使用Upstream host:
{
"DownstreamPathTemplate": "/",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "10.0.10.1",
"Port": 80,
}
],
"UpstreamPathTemplate": "/",
"UpstreamHttpMethod": [ "Get" ],
"UpstreamHost": "somedomain.com"
}
上面的ReRoute只有在host header的值为somedomain.com
的时候才会匹配到。
Priority
可以通过Priority
定义ReRoute的匹配的优先级别。
{
"Priority": 0
}
0是最低的优先级别。Ocelot会为 /{catchAll} ReRoute 使用 0这个优先级别。
{
"UpstreamPathTemplate": "/goods/{catchAll}"
"Priority": 0
}
{
"UpstreamPathTemplate": "/goods/delete"
"Priority": 1
}
在上面的例子中,如果你通过/goods/delete
请求Ocelot,将匹配到/goods/delete
ReRoute。
Query Strings
你可以在DownstreamPathTemplate
中使用querystring:
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/subscriptions/{subscriptionId}/updates?unitId={unitId}",
"UpstreamPathTemplate": "/api/units/{subscriptionId}/{unitId}/updates",
"UpstreamHttpMethod": [
"Get"
],
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 50110
}
]
}
],
"GlobalConfiguration": {
"UseServiceDiscovery": false
}
}
在上面的例子中,ocelot会将upstream path template中的{unitId}的值,添加到downstream的querystring中去。
同样也可以在UpstreamPathTemplate
中使用querystring:
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/units/{subscriptionId}/{unitId}/updates",
"UpstreamPathTemplate": "/api/subscriptions/{subscriptionId}/updates?unitId={unitId}",
"UpstreamHttpMethod": [
"Get"
],
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 50110
}
]
}
],
"GlobalConfiguration": {
"UseServiceDiscovery": false
}
}
上面的例子中Ocelot会将上游模板querystring中的{unitid}作为下游的url path中的一部分。
[译]Ocelot - Routing的更多相关文章
- [译]Ocelot - Service Discovery
原文 你可以指定一个service discovery provider,ocelot将使用它来找下游的host和port. Consul 下面的配置要放在GlobalConfiguration中.如 ...
- [译]Ocelot - Big Picture
原文 目录 Big Picture Getting Started Configuration Routing Request Aggregation Service Discovery Authen ...
- [译]Ocelot - Quality of Service
原文 可以针对每个ReRoute设置对下游服务的熔断器circuit breaker.这部分是通过Polly实现的. 将下面的配置添加到一个ReRoute下面去. "QoSOptions&q ...
- [译]Ocelot - Caching
原文 Ocelot支持基本的缓存,目前Ocelot的缓存是通过CacheManager project实现的. 下面的示例展示了如何启用缓存: s.AddOcelot() .AddCacheManag ...
- [译]Ocelot - Rate Limiting
原文 Ocelot支持对上游做访问限流,这样就可以保证下游不要负载太大了. 如果要启用访问限流,需要做如下配置: "RateLimitOptions": { "Clien ...
- [译]Ocelot - Request Aggregation
原文 Aggregate ReRoutes用来组合多个ReRoutes,将它们的响应结果映射到一个响应中返回给客户端. 为了使用Aggregate ReRoutes,你必须像下面的ocelot.jso ...
- [译]Ocelot - Getting Started
原文 Ocelot专为.NET Core而设计. .NET Core 2.1 安装 首先需要创建一个netstandard2.0项目,然后再通过nuget安装. Install-Package Oce ...
- [译]Ocelot - Configuration
原文 这里有一个配置的样例.配置主要有两个部分.一个是ReRoutes数组,另一个是GlobalConfiguration.ReRoute告诉Ocelot怎么处理上游的请求.Global config ...
- [译]Ocelot - Delegating Handlers
原文 可以为HttpClient添加delegating handlers. Usage 为了添加delegating handler需要做两件事. 首先如下一样创建一个类. public class ...
随机推荐
- 黏包现象之TCP
老师的博客:http://www.cnblogs.com/Eva-J/articles/8244551.html#_label5 server #_*_coding:gbk*_ from socket ...
- 新建swap分区的规划、挂载和自动挂载示例
注:来自Linux系统管理_磁盘分区和格式化的扩展 Linux系统管理_磁盘分区和格式化:http://murongqingqqq.blog.51cto.com/2902694/1361918 思路: ...
- 详解Linux双网卡绑定之bond0
1.什么是bond? 网卡bond是通过多张网卡绑定为一个逻辑网卡,实现本地网卡的冗余,带宽扩容和负载均衡,在生产场景中是一种常用的技术.Kernels 2.4.12及以后的版本均供bonding模块 ...
- js修改父子json格式成树状结构格式
js修改父子json成树状结构 var json = [ { "id" : "01", "pId":"" } , { & ...
- Docker启动的问题解决笔记
一.错误信息1:解决VM 与 Device/Credential Guard 不兼容 错误原因: 1.出现此问题的原因是Device Guard或Credential Guard与Workstati ...
- webpack4配置详解之常用插件分享
前言 继上一次webpack的基础配置分享之后,本次将分享一些工作中项目常用的配置插件.也会包含一些自己了解过觉得不错的插件,如有分析不到位的,欢迎纠错,嗯,这些东西文档都有,大佬可绕过. Wepac ...
- enex 转 md 格式的几种方式(免费版/氪金版)
因为最近有读者投稿,用的是印象笔记,文件格式为 .enex ,一般发文章都用 markdown 格式,这叫我好生苦恼,于是乎,Google 搜了一下,找到了如下解决办法. 氪金版: 我只找到了一款比较 ...
- JS 面向对象 ~ 继承的7种方式
前言: 继承 是 OO 语言中的一个最为人津津乐道的概念.许多 OO 语言都支持两种继承方式:接口继承 和 实现继承.接口继承只继承方法签名,而实现继承则继承实际的方法.如前所述,由于函数没有签名,在 ...
- 打开Player时出现时间格式的错误提示
安装完Player后如果更改了Windows的系统时间和日期显示格式,再次打开Player后会出现时间日期格式错误的提醒,需要按照要求更改Windows系统设置,才能正常运行Player. 此错误提示 ...
- js05-DOM对象二
一.节点操作 创建节点:var ele_a = document.createElement('a');添加节点:ele_parent.appendChild(ele_img);删除节点:ele_pa ...