在实际的应用当中,经常会遇到同一个操作要请求多个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
结果如下:

 
GetSex.png
 
GetAge.png

修改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,结果如下:

 
GetUserInfo_1.png

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,结果如下:

 
GetUserInfo_2.png

可以看到返回的数据中ID为空。
源码下载

完,下一篇介绍服务发现

 
 

1人点赞

 
Ocelot

 

.Netcore 2.0 Ocelot Api网关教程(3)- 路由聚合的更多相关文章

  1. .Netcore 2.0 Ocelot Api网关教程(2)- 路由

    .Netcore 2.0 Ocelot Api网关教程(1) 路由介绍 上一篇文章搭建了一个简单的Api网关,可以实现简单的Api路由,本文介绍一下路由,即配置文件中ReRoutes,ReRoutes ...

  2. .Netcore 2.0 Ocelot Api网关教程(6)- 配置管理

    本文介绍Ocelot中的配置管理,配置管理允许在Api网关运行时动态通过Http Api查看/修改当前配置.由于该功能权限很高,所以需要授权才能进行相关操作.有两种方式来认证,外部Identity S ...

  3. .Netcore 2.0 Ocelot Api网关教程(7)- 限流

    本文介绍Ocelot中的限流,限流允许Api网关控制一段时间内特定api的总访问次数.限流的使用非常简单,只需要添加配置即可. 1.添加限流 修改 configuration.json 配置文件,对  ...

  4. .Netcore 2.0 Ocelot Api网关教程(10)- Headers Transformation

    本文介绍Ocelot中的请求头传递(Headers Transformation),其可以改变上游request传递给下游/下游response传递给上游的header. 1.修改ValuesCont ...

  5. .Netcore 2.0 Ocelot Api网关教程(5)- 认证和授权

    本文介绍Ocelot中的认证和授权(通过IdentityServer4),本文只使用最简单的IdentityServer,不会对IdentityServer4进行过多讲解. 1.Identity Se ...

  6. .Netcore 2.0 Ocelot Api网关教程(1)- 入门

    Ocelot(Github)Ocelot官方文档(英文)本文不会介绍Api网关是什么以及Ocelot能干什么需要对Api网关及Ocelot有一定的理论了解 开始使用Ocelot搭建一个入门级Api网关 ...

  7. .Netcore 2.0 Ocelot Api网关教程(9)- QoS

    本文介绍Ocelot中的QoS(Quality of Service),其使用了Polly对超时等请求下游失败等情况进行熔断. 1.添加Nuget包 添加 Ocelot.Provider.Polly  ...

  8. .Netcore 2.0 Ocelot Api网关教程(4)- 服务发现

    本文介绍Ocelot中的服务发现(Service Discovery),Ocelot允许指定一个服务发现提供器,之后将从中寻找下游服务的host和port来进行请求路由.关于服务发现的详细介绍请点击. ...

  9. .Netcore 2.0 Ocelot Api网关教程(8)- 缓存

    Ocelot中使用 CacheManager 来支持缓存,官方文档中强烈建议使用该包作为缓存工具.以下介绍通过使用CacheManager来实现Ocelot缓存. 1.通过Nuget添加 Ocelot ...

随机推荐

  1. [zhuanzai]Bean对象注入失败 .NoSuchBeanDefinitionException: No qualifying bean of type..

    nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying b ...

  2. vmware添加新硬盘磁盘扫描脚本

    #! /bin/bash echo "- - -" > /sys/class/scsi_host/host0/scan echo "- - -" > ...

  3. webstorm 2016.3 注册方法

    用license server 的方式吧,activation code 的方式没有找到方法. license server 里写http://idea.iteblog.com/key.php 用li ...

  4. 02_pip区别: linux环境下python2,python3的

    1.pip与pip3理解 centos中,我的pip与pip3都是python2.7的,所以无法安装成功,总是安装成python2的 [root@IP ~]# pip -V pip /site-pac ...

  5. 分布式协调框架_Zookeeper

    Zookeeper 如今在分布式架构中应用十分广泛,它作为分布式协调框架在分布式架构中有着举足轻重的地位,本文是主要从以上几个方面对 Zookeeper 常用的知识进行总结. 一 从集中式到分布式架构 ...

  6. HDU 5936 Difference ( 2016 CCPC 杭州 D && 折半枚举 )

    题目链接 题意 : 给出一个 x 和 k 问有多少个 y 使得 x = f(y, k) - y .f(y, k) 为 y 中每个位的数的 k 次方之和.x ≥ 0 分析 : f(y, k) - y = ...

  7. 二维DFT

    学习DIP第4天 傅里叶变换数学原理会在后续完整介绍,目前只实现代码,观察下结果,公式在上一篇博客中已经描述 内容迁移至 http://www.face2ai.com/DIP-2-2-二维DFT/ h ...

  8. 8. 使用Zuul构建微服务网关

                    使用Zuul构建微服务网关 8.1. 为什么要使用微服务网关 8.2. Zuul简介 8.3. 编写Zuul微服务网关 8.4. Zuul的路由端点 8.5. Zuul ...

  9. .net 数据导出

    安装npoi,下面是具体的C#代码: public static XSSFWorkbook BuildWorkbook(DataTable dt) { var book = new XSSFWorkb ...

  10. 巨丑vue

    <template> <div> <div class="demo-type" align="right" style=" ...