原文

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, DownstreamSchemeDownstreamHostAndPorts定义请求需要转发到哪个URL上去。

DownstreamHostAndPorts是一个集合,它定义了要转发到的下游服务的host和port。通常这个集合只需要一个条目就可以了,但是如果你想要搞负载均衡的话,可以在这指定多个条目。

UpstreamPathTemplate定义了ocelot用哪个发到ocelot的请求处理DownstreamPathTemplate。Ocelot根据UpstreamHttpMethod来针对同一个URL不同请求方式分别进行对应的处理。如果没指定UpstreamHttpMethod的话,那就是针对所有的HTTP请求方式。

可以为Template中的变量添加placeholder(以{something}的形式)。placeholder变量必须同时出现在DownstreamPathTemplateUpstreamPathTemplate中。

可以通过下面做一个通用的默认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的更多相关文章

  1. [译]Ocelot - Service Discovery

    原文 你可以指定一个service discovery provider,ocelot将使用它来找下游的host和port. Consul 下面的配置要放在GlobalConfiguration中.如 ...

  2. [译]Ocelot - Big Picture

    原文 目录 Big Picture Getting Started Configuration Routing Request Aggregation Service Discovery Authen ...

  3. [译]Ocelot - Quality of Service

    原文 可以针对每个ReRoute设置对下游服务的熔断器circuit breaker.这部分是通过Polly实现的. 将下面的配置添加到一个ReRoute下面去. "QoSOptions&q ...

  4. [译]Ocelot - Caching

    原文 Ocelot支持基本的缓存,目前Ocelot的缓存是通过CacheManager project实现的. 下面的示例展示了如何启用缓存: s.AddOcelot() .AddCacheManag ...

  5. [译]Ocelot - Rate Limiting

    原文 Ocelot支持对上游做访问限流,这样就可以保证下游不要负载太大了. 如果要启用访问限流,需要做如下配置: "RateLimitOptions": { "Clien ...

  6. [译]Ocelot - Request Aggregation

    原文 Aggregate ReRoutes用来组合多个ReRoutes,将它们的响应结果映射到一个响应中返回给客户端. 为了使用Aggregate ReRoutes,你必须像下面的ocelot.jso ...

  7. [译]Ocelot - Getting Started

    原文 Ocelot专为.NET Core而设计. .NET Core 2.1 安装 首先需要创建一个netstandard2.0项目,然后再通过nuget安装. Install-Package Oce ...

  8. [译]Ocelot - Configuration

    原文 这里有一个配置的样例.配置主要有两个部分.一个是ReRoutes数组,另一个是GlobalConfiguration.ReRoute告诉Ocelot怎么处理上游的请求.Global config ...

  9. [译]Ocelot - Delegating Handlers

    原文 可以为HttpClient添加delegating handlers. Usage 为了添加delegating handler需要做两件事. 首先如下一样创建一个类. public class ...

随机推荐

  1. Mysql数据中Packet for query is too large错误的解决方法

    有时,程序在连接mysql执行操作数据库时,会出现如下类似错误信息: Packet for query is too large (4230 > 1024). You can change th ...

  2. 安装Gradle(Windows & Linux)

    Gradle 是以 Groovy 语言为基础,面向Java应用为主.基于DSL(领域特定语言)语法的自动化构建工具.在github上,gradle项目很多,有的是gradel跟maven构建一块儿使用 ...

  3. poj 3090 Visible Lattice Points(离线打表)

    这是好久之前做过的题,算是在考察欧拉函数的定义吧. 先把欧拉函数讲好:其实欧拉函数还是有很多解读的.emmm,最基础同时最重要的算是,¢(n)表示范围(1, n-1)中与n互质的数的个数 好了,我把规 ...

  4. Bubble Babble Binary Data Encoding的简介以及bubblepy的安装使用方法

    Bubble Babble Binary Data Encoding是由Antti Huima创建的一种编码方法,可以把二进制信息表示为由交替的元音和辅音组成的伪词(pseudo-words),主要用 ...

  5. SpringBoot 数据篇之使用JDBC

    SpringBootTutorial :: Data :: Jdbc 简介 API execute update query 实战 配置数据源 完整示例 引申和引用 简介 Spring Data 包含 ...

  6. tiff图片拆分

    本程序下载地址: tiff格式的图片可以由多张图片合成, 也可以拆分为多张图片.不管是合成或者拆分,都借助了第三方开源库Cximage,对于这个图像库的使用,我们没有必要去一行一行的去看它的代码实现, ...

  7. 写个.net开发者的Linux迁移指南

    前言 为什么要迁移到Linux 首先我个人还是有点软件洁癖,以前是穷酸学生的时候也是用盗版的用户,后来在知乎被洗脑终于有了点版权意识.然后便有了能用开源软件的就用开源,实在不能就选社区版或者免费版.于 ...

  8. STL中的set使用方法详细!!!!

    1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...

  9. 系统IO

    系统IO:Linux系统提供给应用程序操作文件的接口 Everything is a file  ,in  Unix 在Unix/Linux下,万物皆文件 打开文件函数原型: #include< ...

  10. Firefox 中出现的 “Network Protocol Error”怎么办

    Mozilla Firefox 多年来一直是我的默认 Web 浏览器,我每天用它来进行日常网络活动,例如访问邮件,浏览喜欢的网站等.今天,我在使用 Firefox 时遇到了一个奇怪的错误.我试图在 R ...