在实际的应用当中,经常会遇到同一个操作要请求多个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. MyBatis-09-Lombok

    9.Lombok Project Lombok is a java library that automatically plugs into your editor and build tools, ...

  2. 内网监控zabbix

    告警 告警方式:linkedsee 类型:使用脚本linkedsee.sh [root@zabbix-server ~]# cat linkedsee.sh #! /bin/bash SERVICE_ ...

  3. [Functional Programming] Reader with Async ADT

    ReaderT is a Monad Transformer that wraps a given Monad with a Reader. This allows the interface of ...

  4. 小米oj 帮小学生排队(排序+插入)

     帮小学生排队 序号:#18难度:有挑战时间限制:1000ms内存限制:10M 描述 用一个数组表示一群正在排队的小学生,每个小学生用一对整数 H, K 来表示:H 表示这个小学生的身高,K 表示这个 ...

  5. Intel Code Challenge Final Round D. Dense Subsequence 二分思想

    D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. hdu 1023 hdu 1131

    How Many Trees? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  7. Codevs 1574 广义斐波那契数列(矩阵乘法)

    1574 广义斐波那契数列 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 广义的斐波那契数列是指形如an=p*an-1+q* ...

  8. 简单NLT

    目录 操作文本 需求 代码实现 操作文本 I have a dream that my four little children will one day live in a nation where ...

  9. json常用的注解

    json注解: 1.@JsonIgnoreProperties: 此注解是类注解,作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响. 写法将此标签加在model ...

  10. Fiddler主界面图标简单说明

    Fiddler主界面图标简单说明: 名称 含义 # 抓取HTTP Request的顺序,从1开始,以此递增 Result HTTP状态码 Protocol 请求使用的协议,如HTTP/HTTPS/FT ...