[译]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 ...
随机推荐
- shell中的EOF用法
重定位运算符 >> 是追加内容> 是覆盖原有内容 1.EOF Shell中通常将EOF与 << 结合使用,表示后续的输入作为子命令或子Shell的输入,直到遇到EOF为止 ...
- Python 隔离环境 virtualenv
1) 安装 $ sudo pip3 install virtualenv 2) 创建并进入工程目录,例如 myproject $ mkdir myproject $ cd myproject 3) 在 ...
- Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures
参考博客: https://www.cnblogs.com/xiao987334176/p/9046028.html 线程简述 什么是线程?线程是cpu调度的最小单位进程是资源分配的最小单位 进程和线 ...
- PWC6345: There is an error in invoking javac. A full JDK (not just JRE) is required
今天在使用jetty运行一个项目的时候报这个错误,仔细看了下,应该是eclipse配置的jdk或者jre出错. 看了下环境变量,发现有些配置没有配置完全. 我个人的解决方法: 在path中,添加%JA ...
- OpenStack-Keystone(2)
一. Keystone 概述 管理用户及其权限 维护OpenStack Services的Endpoint Authentication(认证)和 Authorization(授权) 1.验证用户 验 ...
- 从源码看springboot默认的资源文件和配置文件所在位置
首先,使用的springboot版本是2.X,在这里写一点学习springboot的记录 springboot需要配置的不多,但也并不是完全不需要配置就可以顺畅使用,这里看一下它默认的配置 首先,看一 ...
- Lodop简短问答客户反馈篇 及排查步骤 及注册相关
A.http下打印图片正常,https下打印图片是××.(有的客户端可以,有的不可以)重置ie浏览器试试.客户反馈:(和ie浏览器的设置有关)intenet选项--高级里,我调整为和能打印出图片的电脑 ...
- Qt测算程序运行时间
#include <QDebug> #include <QTime> #include <sys/time.h> #include <windows.h> ...
- 通过SQL脚本来查询SQLServer 中主外键关系
在SQLServer中主外键是什么,以及主外键如何创建,在这里就不说了,不懂的可以点击这里,这篇文章也是博客园的博友写的,我觉得总结的很好: 此篇文章主要介绍通过SQL脚本来查看Sqlserver中主 ...
- POJChallengeRound2 Tree 【数学期望】
题目分析: 我们令$G(x)$表示前$x$个点的平均深度,$F(x)$表示第$x$个点的期望深度. 有$F(x) = G(x-1)+1$,$G(x) = G(x-1)+\frac{1}{x}$ 所以答 ...