.Netcore 2.0 Ocelot Api网关教程(3)- 路由聚合
在实际的应用当中,经常会遇到同一个操作要请求多个api来执行。这里先假设一个应用场景:通过姓名获取一个人的个人信息(性别、年龄),而获取每种个人信息都要调用不同的api,难道要依次调用吗?在Ocelot中为我们提供了很好的解决方法。
路由聚合
继续使用前边的文章建立的项目,在WebApiA项目中添加一个新的WebApi控制器命名为UserController,代码如下:
[Produces("application/json")]
[Route("api/[controller]/[action]")]
public class UserController : Controller
{
[HttpGet]
public string GetSex(string name)
{
if (name == "Jonathan")
return "Man";
return null;
}
[HttpGet]
public int? GetAge(string name)
{
if (name == "Jonathan")
return 24;
return null;
}
}
启动WebApiA,然后使用Postman分别访问
http://localhost:5001/api/User/GetSex?name=Jonathan
http://localhost:5001/api/User/GetAge?name=Jonathan
结果如下:


修改configuration.json文件,向ReRoutes节点中添加如下配置:
{
"DownstreamPathTemplate": "/api/User/GetSex",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5001
}],
"UpstreamPathTemplate": "/Sex",
"UpstreamHttpMethod": [ "Get" ],
"Key": "Sex"
},
{
"DownstreamPathTemplate": "/api/User/GetAge",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5001
}],
"UpstreamPathTemplate": "/Age",
"UpstreamHttpMethod": [ "Get" ],
"Key": "Age"
}
在与ReRoutes同级的位置添加如下配置:
"Aggregates": [
{
"ReRouteKeys": [
"Sex",
"Age"
],
"UpstreamPathTemplate": "/GetUserInfo"
}]
请求聚合执行的操作为:当请求GetUserInfo时,自动到Reoutes中查找ReRouteKeys下Key值相同的路由,并全部请求,然后将请求结果拼成Json格式返回。
Postman访问http://localhost:5000/GetUserInfo,结果如下:

Ocelot请求聚合现在只支持get方法,点击https://github.com/ThreeMammals/Ocelot/blob/master/src/Ocelot/Configuration/File/FileAggregateReRoute.cs查看源码及注释说明。
请求聚合的页面404
请求聚合下不会对404的页面返回任何结果,我们现在在configuration.json文件中的ReRoutes节点下添加如下内容:
{
"DownstreamPathTemplate": "/api/User/GetID",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5001
}],
"UpstreamPathTemplate": "/ID",
"UpstreamHttpMethod": [ "Get" ],
"Key": "ID"
}
向Aggregates节点下的ReRoutesKeys节点下添加一个ID
注意:在WebApiA项目中是没有/api/User/GetID方法的,所以会返回404
然后我们启动项目访问http://localhost:5000/api/GetUserInfo,结果如下:

可以看到返回的数据中ID为空。
源码下载
完,下一篇介绍服务发现
1人点赞
.Netcore 2.0 Ocelot Api网关教程(3)- 路由聚合的更多相关文章
- .Netcore 2.0 Ocelot Api网关教程(2)- 路由
.Netcore 2.0 Ocelot Api网关教程(1) 路由介绍 上一篇文章搭建了一个简单的Api网关,可以实现简单的Api路由,本文介绍一下路由,即配置文件中ReRoutes,ReRoutes ...
- .Netcore 2.0 Ocelot Api网关教程(6)- 配置管理
本文介绍Ocelot中的配置管理,配置管理允许在Api网关运行时动态通过Http Api查看/修改当前配置.由于该功能权限很高,所以需要授权才能进行相关操作.有两种方式来认证,外部Identity S ...
- .Netcore 2.0 Ocelot Api网关教程(7)- 限流
本文介绍Ocelot中的限流,限流允许Api网关控制一段时间内特定api的总访问次数.限流的使用非常简单,只需要添加配置即可. 1.添加限流 修改 configuration.json 配置文件,对 ...
- .Netcore 2.0 Ocelot Api网关教程(10)- Headers Transformation
本文介绍Ocelot中的请求头传递(Headers Transformation),其可以改变上游request传递给下游/下游response传递给上游的header. 1.修改ValuesCont ...
- .Netcore 2.0 Ocelot Api网关教程(5)- 认证和授权
本文介绍Ocelot中的认证和授权(通过IdentityServer4),本文只使用最简单的IdentityServer,不会对IdentityServer4进行过多讲解. 1.Identity Se ...
- .Netcore 2.0 Ocelot Api网关教程(1)- 入门
Ocelot(Github)Ocelot官方文档(英文)本文不会介绍Api网关是什么以及Ocelot能干什么需要对Api网关及Ocelot有一定的理论了解 开始使用Ocelot搭建一个入门级Api网关 ...
- .Netcore 2.0 Ocelot Api网关教程(9)- QoS
本文介绍Ocelot中的QoS(Quality of Service),其使用了Polly对超时等请求下游失败等情况进行熔断. 1.添加Nuget包 添加 Ocelot.Provider.Polly ...
- .Netcore 2.0 Ocelot Api网关教程(4)- 服务发现
本文介绍Ocelot中的服务发现(Service Discovery),Ocelot允许指定一个服务发现提供器,之后将从中寻找下游服务的host和port来进行请求路由.关于服务发现的详细介绍请点击. ...
- .Netcore 2.0 Ocelot Api网关教程(8)- 缓存
Ocelot中使用 CacheManager 来支持缓存,官方文档中强烈建议使用该包作为缓存工具.以下介绍通过使用CacheManager来实现Ocelot缓存. 1.通过Nuget添加 Ocelot ...
随机推荐
- cmake学习笔记之add_library、target_link_libraries和link_directories
cmake是Linux(这里默认是Ubuntu系统)下常使用的编译C++的工具,而使用cmake就需要先在CmakeLists.txt文件中对编译规则进行.这里介绍常用的三种指令add_library ...
- udp广播,单播,多播
一.单播 (1)简介 两个节点之间的通信,一个发送者一个接收者 (2)特点 1.服务器及时响应客户机的请求. 2.服务器针对每个客户不通的请求发送不通的数据,容易实现个性化服务. 3.允许在Inter ...
- BZOJ 4802: 欧拉函数 (Pollard-Rho)
开始一直T,原来是没有srand- CODE #include<bits/stdc++.h> using namespace std; typedef long long LL; vect ...
- 【ArcMap】
1.加载图层(1)内容列表中右键添加数据(2)目录列表中拖拽(3)导航中的添加数据 2.编辑要素(1)选中编辑器点击开始编辑(2)在编辑要素中选中要编辑的要素 选择构造工具 执行编辑操作(3)停止编辑 ...
- CentOS6与7区别整理
(1)桌面系统 [CentOS6] GNOME 2.x [CentOS7] GNOME 3.x(GNOME Shell) (2)文件系统 [CentOS6] ext4 [CentOS7] xfs (3 ...
- Codeforces Round #346 (Div. 2) C题
C. Tanya and Toys In Berland recently a new collection of toys went on sale. This collection consist ...
- php遍历一个文件下的所有文件和子文件夹下的文件
function AllFile($dir){ if($dh = opendir($dir)){ while (($file = readdir($dh)) !== false){ if($file ...
- 001_C/C++笔试题_考察C/C++语言基础概念
(一)文章来自:C/C++笔试题-主要考察C/C++语言基础概念.算法及编程,附参考答案 (二)基础概念 2. 头文件中的ifndef/define/endif的作用? 答:防止该头文件被重复引用. ...
- Remove Mapping
http://social.msdn.microsoft.com/forums/vstudio/en-US/ffa3aa15-1d61-4464-ac4a-7a812d073a67/remove-ma ...
- linux系统编程--守护进程,会话,进程组,终端
终端: 在UNIX系统中,用户通过终端登录系统后得到一个Shell进程,这个终端成为Shell进程的控制终端(Controlling Terminal), 进程中,控制终端是保存在PCB中的信息,而f ...