原文

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. linux上修改mysql登陆密码

    1. 修改MySQL的登录设置: # vi /etc/my.cnf     2. 在[mysqld]的段中加上一句:skip-grant-tables  例如: [mysqld] port       ...

  2. ✔ OI Diary ★

    一 | 2019-3-28 1.整晨,云之考矣,暴后皆不会,邃无感而写斯普雷尔,然则午后知暴可六十哉. 然则斯普雷毙,虽特判之矣,然则暴只判二十哉,呜呼! ​2.午间归宿,视白购书一本,目触,感之甚集 ...

  3. ubuntu下安装飞鸽传书

    1.从官网下载Linux版本飞鸽传书(http://www.ipmsg.org.cn/) 2.解压后执行 ./QIpmsg 若报错 libstdc++.so.6: version `CXXABI_AR ...

  4. 监控glusterfs

    监控集群状态 [4ajr@elk1 scripts]$ cat glusterfs_peer_status.sh #!/bin/bash peer_status=`sudo gluster peer ...

  5. 使用exec和sp_executesql动态执行SQL语句(转载)

    当需要根据外部输入的参数来决定要执行的SQL语句时,常常需要动态来构造SQL查询语句,个人觉得用得比较多的地方就是分页存储过程和执行搜索查询的SQL语句.一个比较通用的分页存储过程,可能需要传入表名, ...

  6. C#自定义应用程序上下文对象+IOC自己实现依赖注入

    以前的好多代码都丢失了,加上最近时间空一些,于是想起整理一下以前的个人半拉子项目,试试让它们重生.自从养成了架构师视觉 搭建框架之后,越来 越看不上以前搭的框架了.先撸个上下文对象加上实现依赖注入.由 ...

  7. Java 开发笔记2

    Java获取参数名称 https://blog.csdn.net/z69183787/article/details/81117525 DefaultParameterNameDiscoverer() ...

  8. Django组件 之 分页器(paginator)

    --------------------------------------------------------------------------------路虽远,行则将至.  事虽难,做则必成. ...

  9. Pandas基本操作

    pandas:数据分析 pandas是一个强大的Python数据分析的工具包. pandas是基于NumPy构建的. pandas的主要功能 具备对其功能的数据结构DataFrame.Series 集 ...

  10. NTT板子

    不说别的. 这份NTT跑得比FFT快,不知道为什么. 以下代码针对\(10^5\)的数据范围. #include<cstdio> #include<vector> #inclu ...