原文

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. springcloud ribbon 客户端负载均衡用法

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is ...

  2. SP11470 TTM - To the moon

    嘟嘟嘟 主席树+区间修改. 以为是水题,写着写着发现区间修改标记下传会出问题,然后想了想发现以前做的只是单点修改. 那怎么办咧? 然后题解交了我标记永久化这个神奇的东西. 特别好理解,就是修改的时候直 ...

  3. Html 改变原有标签属性

    内容简要: 当标签内内容 达到某以条件的时候改变当前标签属性 例如原标签为<tr> 当tr内的值符合某一条件时把<tr>变成<a>标签 例:当订单状体编程已支付的时 ...

  4. LCA-RMQ+欧拉序

    还是那一道洛谷的板子题来说吧 传送门 其实好几天之前就写了 结果dr实在是太弱了 没有那么多的精力 于是就一直咕咕咕了 哎 今天终于补上来了 LCA概念传送门 RMQ传送门 这个算法是基于RMQ和欧拉 ...

  5. Nvidia和Google的AI芯片战火蔓延至边缘端

    AI 的热潮还在持续,AI 的战火自然也在升级.英伟达作为这一波 AI 浪潮中最受关注的公司之一,在很大程度上影响着 AI 的战局.上周在美国举行的 GTC 2019 上,黄仁勋大篇幅介绍了英伟达在 ...

  6. InheritedWidget

    下面这个示例是InheritedWidgt的一个简单用法: class CounterProvider extends InheritedWidget{//数据之前必须加上final,下面这三个数据都 ...

  7. Vue-移动端项目真机测试

    一.查看ip地址 在控制台输入 ifconfig 查看ip地址 二.修改webpack-dev-server配置项 webpack-dev-server 默认不支持ip地址访问,需要修改配置项 三.测 ...

  8. Python变量的本质与intern机制

    变量的存储 a = 'abc' 理解:①先在内存中生成一个字符串‘abc’ ②可以把比变量名a看做一个便利贴,然后将a贴到‘abc’中     ③注意顺序,是生成‘abc’,然后再创建a指向‘abc’ ...

  9. 【学习总结】GirlsInAI ML-diary day-13-Try/Except 异常处理

    [学习总结]GirlsInAI ML-diary 总 原博github链接-day13 认识异常处理 要点小结: try和except是同个等级,注意对齐和缩进 可以把try和except直接理解成另 ...

  10. HP 1010、 1020、 1022 、M1005激光打印机内部无卡纸,但机器仍提示卡纸?

    HP 1010.1018.1020.1022.M1005激光打印机,硒鼓原装编号:Q2612A  1800页 ( A4纸,5%覆盖率).是办公桌面小型打印机中主流产品,故障率极小. 现有一台HP 10 ...