在实际的应用当中,经常会遇到同一个操作要请求多个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. ThreadPoolExecutor源码分析二

      接上文,这里继续分析源码 private static final int COUNT_BITS = Integer.SIZE - 3; private static final int CAPA ...

  2. Jenkins构建从github上克隆时,报Host key verification failed.

    首先在本地通过CMD执行git clone xxxxx时,可以成功的通过免密(SSH_KEY)克隆下来代码,但是通过Jenkins克隆时,就报如下信息: Cloning into 'GitHub'.. ...

  3. File "/usr/bin/pip", line 11, in <module> sys.exit(__main__._main()) AttributeError: 'module' object has no attribute '_main'

    多个版本pip共存导致 直接使用pip2进行安装即可 如:pip2 install requests

  4. unity vulkan snapdragon profiler

    your device does not match the hardware requirements of this application 遇到上面那个warning 跟了下 是vulkan c ...

  5. Kafka中的消息是否会丢失和重复消费(转)

    在之前的基础上,基本搞清楚了Kafka的机制及如何运用.这里思考一下:Kafka中的消息会不会丢失或重复消费呢?为什么呢? 要确定Kafka的消息是否丢失或重复,从两个方面分析入手:消息发送和消息消费 ...

  6. python--ctypes模块:调用C函数

    Python 的 ctypes 要使用 C 函数,需要先将 C 编译成动态链接库的形式,即 Windows 下的 .dll 文件,或者 Linux 下的 .so 文件 Windows 系统下的 C 标 ...

  7. binlog2sql闪回工具的使用

    binlog2sql闪回工具的使用 一.下载安装依赖的python yum install openssl-devel bzip2-devel expat-devel gdbm-devel readl ...

  8. 配置NFS共享

    NFS(网络文件系统)-------> linux与linux平台 服务器端: 1.安装软件nfs-utils(服务:nfs-server) 2.创建共享目录:mkdir /nfs_dir 3. ...

  9. 【csp模拟赛5】购物(shopping.cpp)--常规

    多项式,因为每次的x相同,所以把a和b相加就行了,然后找对称轴,找离对称轴最近的整数点,然而我却写了个暴力,没看x #include <iostream> #include <cst ...

  10. 数据结构实验之链表一:顺序建立链表(SDUT 2116)

    Problem Description 输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据. Input 第一行输入整数的个数N: 第二行依次输入每个整数. Output ...