原文

Aggregate ReRoutes用来组合多个ReRoutes,将它们的响应结果映射到一个响应中返回给客户端。

为了使用Aggregate ReRoutes,你必须像下面的ocelot.json中做些配置。 在下面的例子中,有两个ReRoutes,且它们都有一个Key属性,我们将使用ReRoute里面的key在Aggregate中组合ReRoute。Aggregate 和 ReRoutes的UpstreamPathTemplate不能重复。Aggregate可以使用ReRoute中出了RequestIdKey之外的所有配置。

Advanced register your own Aggregators

ocelot.json添加Aggregator属性:

{
"ReRoutes": [
{
"DownstreamPathTemplate": "/",
"UpstreamPathTemplate": "/laura",
"UpstreamHttpMethod": [
"Get"
],
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 51881
}
],
"Key": "Laura"
},
{
"DownstreamPathTemplate": "/",
"UpstreamPathTemplate": "/tom",
"UpstreamHttpMethod": [
"Get"
],
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 51882
}
],
"Key": "Tom"
}
],
"Aggregates": [
{
"ReRouteKeys": [
"Tom",
"Laura"
],
"UpstreamPathTemplate": "/",
"Aggregator": "FakeDefinedAggregator"
}
]
}

添加了一个名为FakeDefinedAggregator的Aggregator。

还要将这个FakeDefinedAggregator添加到OcelotBuilder中:

services
.AddOcelot()
.AddSingletonDefinedAggregator<FakeDefinedAggregator>();

因为FakeDefinedAggregator注册到了DI容器中,因此可以向下面一样添加依赖到它里面去:

services.AddSingleton<FooDependency>();

services
.AddOcelot()
.AddSingletonDefinedAggregator<FooAggregator>();

上面的例子中,FooAggregator可以依赖于FooDependency, 它通过DI容器resolved。

另外,还可以将Aggregator注册为transient生命周期:

services
.AddOcelot()
.AddTransientDefinedAggregator<FakeDefinedAggregator>();

自定义的Aggregator必须实现IDefinedAggregator接口:

public interface IDefinedAggregator
{
Task<DownstreamResponse> Aggregate(List<DownstreamResponse> responses);
}

通过这个特性,我们可以做许多事情,因为DownstreamResponse包含了Content, Headers 和 Status Code。如果这个Aggregator其中的一个ReRoute请求时发生了异常,那么将得不到这个ReRoute的DownstreamResponse。如果抛出了异常,那么会有日志记录。

Basic expecting JSON from Downstream Services

{
"ReRoutes": [
{
"DownstreamPathTemplate": "/",
"UpstreamPathTemplate": "/laura",
"UpstreamHttpMethod": [
"Get"
],
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 51881
}
],
"Key": "Laura"
},
{
"DownstreamPathTemplate": "/",
"UpstreamPathTemplate": "/tom",
"UpstreamHttpMethod": [
"Get"
],
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 51882
}
],
"Key": "Tom"
}
],
"Aggregates": [
{
"ReRouteKeys": [
"Tom",
"Laura"
],
"UpstreamPathTemplate": "/"
}
]
}

如果 ReRoute /tom 返回 {“Age”: 19}, /laura 返回 {“Age”: 25}, 那么Aggregator返回:

{"Tom":{"Age": 19},"Laura":{"Age": 25}}

ReRoute key 作为了Aggregator返回字典的key, 响应做为了返回字典的值。

所有downstream services的响应头都会被丢弃掉。

Ocelot返回的content type是 application/json

如果downstream services 返回了一个 404,那么aggregator不会为这个downstream service返回任何东西。aggregator不会受此影响返回404, 即使这个aggregator的所有ReRoute都返回了404,它也不会返回404。

Aggregator 只支持 GET 请求方式。

[译]Ocelot - Request Aggregation的更多相关文章

  1. [译]Ocelot - Request Id / Correlation Id

    原文 Ocelot可以通过header的形式发送一个requestid.ocelot会将这个requestid转发到下游服务. 如果在日志配置中设置了IncludeScopes为true,那么requ ...

  2. [译]Ocelot - Big Picture

    原文 目录 Big Picture Getting Started Configuration Routing Request Aggregation Service Discovery Authen ...

  3. [译]Ocelot - Delegating Handlers

    原文 可以为HttpClient添加delegating handlers. Usage 为了添加delegating handler需要做两件事. 首先如下一样创建一个类. public class ...

  4. [译]Ocelot - Logging

    原文 Ocelot使用标准的日志接口ILoggerFactory和ILogger<T>.它们封装在IOcelotLogger 和 IOcelotLoggerFactory中,因为ocelo ...

  5. [译]Ocelot - Headers Transformation

    原文 Add to Request 为上游请求添加请求头,只需如下一样将下面的配置添加到一个ReRoute里: "UpstreamHeaderTransform": { " ...

  6. [译]Ocelot - Quality of Service

    原文 可以针对每个ReRoute设置对下游服务的熔断器circuit breaker.这部分是通过Polly实现的. 将下面的配置添加到一个ReRoute下面去. "QoSOptions&q ...

  7. [译]Ocelot - Caching

    原文 Ocelot支持基本的缓存,目前Ocelot的缓存是通过CacheManager project实现的. 下面的示例展示了如何启用缓存: s.AddOcelot() .AddCacheManag ...

  8. [译]Ocelot - Rate Limiting

    原文 Ocelot支持对上游做访问限流,这样就可以保证下游不要负载太大了. 如果要启用访问限流,需要做如下配置: "RateLimitOptions": { "Clien ...

  9. [译]Ocelot - Service Discovery

    原文 你可以指定一个service discovery provider,ocelot将使用它来找下游的host和port. Consul 下面的配置要放在GlobalConfiguration中.如 ...

随机推荐

  1. ASP.NET MVC5学习系列——身份验证、授权

    一.什么是身份验证和授权 人们有时对用户身份验证和用户授权之间的区别感到疑惑.用户身份验证是指通过某种形式的登录机制(包括用户名/密码.OpenID.OAuth等说明身份的项)来核实用户的身份.授权验 ...

  2. Codeforces Global Round 2 Solution

    这场题目设置有点问题啊,难度:Div.2 A->Div.2 B->Div.2 D->Div.2 C->Div.2 D->Div.1 D-> Div.1 E-> ...

  3. .Net Core应用框架Util介绍(一)

    距离上次发文,已经过去了三年半,这几年技术更新节奏异常迅猛,.Net进入了跨平台时代,前端也被革命性的颠覆. 回顾 2015年,正当我还沉迷于JQuery + EasyUi的封装时,突然意识到技术已经 ...

  4. Oracle通过Navicat导入表数据与机构,数据无法直接查询,需要加双引号的问题

    使用navicat 导入表到ORACLE时,总是会遇到虽然表格完整导入到数据库,但是往往查不出来数据,网上提供的解决办法是把查询的列 加上 双引号,或者表名加上双引号,但这解决办法却减慢了编写sql ...

  5. Codeforces Round #498 (Div. 3)--E. Military Problem

    题意问,这个点的然后求子树的第i个节点. 这道题是个非常明显的DFS序: 我们只需要记录DFS的入DFS的时间,以及出DFS的时间,也就是DFS序, 然后判断第i个子树是否在这个节点的时间段之间. 最 ...

  6. Python之使用转义序列 \n 和 \t 跟 expandtabs 函数输出表格

    示例: text = "username\temail\tpassword\nashdfh\tfiodfh@q.com\ty567\nsdfiuh\tadfhisoj@163.com\t42 ...

  7. Linux下修改MySQL数据表中字段属性

    一.修改某个表的字段类型及指定为空或非空 alter table 表名称 change 字段名称 字段名称 字段类型 [是否允许非空]; alter table 表名称 modify 字段名称 字段类 ...

  8. Minieye杯第十五届华中科技大学程序设计邀请赛网络赛D Grid(简单构造)

    链接:https://ac.nowcoder.com/acm/contest/560/D来源:牛客网 题目描述 Give you a rectangular gird which is h cells ...

  9. python语法与c++不同点

    代码结构由':'号和缩进 来标示. 函数: def 定义 不定参数: *args:   tarple可逐个传, 整体传, *拆包传 **kwargs dict可逐个传, 整体传, ** 拆包传, 有2 ...

  10. setData优化过程

    https://blog.csdn.net/rolan1993/article/details/88106343 在做一个小球跟随手指移动的效果时候,由于在touchmove事件中频繁调用setDat ...