[译]Ocelot - Request Aggregation
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的更多相关文章
- [译]Ocelot - Request Id / Correlation Id
原文 Ocelot可以通过header的形式发送一个requestid.ocelot会将这个requestid转发到下游服务. 如果在日志配置中设置了IncludeScopes为true,那么requ ...
- [译]Ocelot - Big Picture
原文 目录 Big Picture Getting Started Configuration Routing Request Aggregation Service Discovery Authen ...
- [译]Ocelot - Delegating Handlers
原文 可以为HttpClient添加delegating handlers. Usage 为了添加delegating handler需要做两件事. 首先如下一样创建一个类. public class ...
- [译]Ocelot - Logging
原文 Ocelot使用标准的日志接口ILoggerFactory和ILogger<T>.它们封装在IOcelotLogger 和 IOcelotLoggerFactory中,因为ocelo ...
- [译]Ocelot - Headers Transformation
原文 Add to Request 为上游请求添加请求头,只需如下一样将下面的配置添加到一个ReRoute里: "UpstreamHeaderTransform": { " ...
- [译]Ocelot - Quality of Service
原文 可以针对每个ReRoute设置对下游服务的熔断器circuit breaker.这部分是通过Polly实现的. 将下面的配置添加到一个ReRoute下面去. "QoSOptions&q ...
- [译]Ocelot - Caching
原文 Ocelot支持基本的缓存,目前Ocelot的缓存是通过CacheManager project实现的. 下面的示例展示了如何启用缓存: s.AddOcelot() .AddCacheManag ...
- [译]Ocelot - Rate Limiting
原文 Ocelot支持对上游做访问限流,这样就可以保证下游不要负载太大了. 如果要启用访问限流,需要做如下配置: "RateLimitOptions": { "Clien ...
- [译]Ocelot - Service Discovery
原文 你可以指定一个service discovery provider,ocelot将使用它来找下游的host和port. Consul 下面的配置要放在GlobalConfiguration中.如 ...
随机推荐
- 打印进度条>>>>
i+1: 当前的数量 300: 总数量 import sys print("下载中...") def process(curr, count): cursor_count = c ...
- python 结巴分词学习
结巴分词(自然语言处理之中文分词器) jieba分词算法使用了基于前缀词典实现高效的词图扫描,生成句子中汉字所有可能生成词情况所构成的有向无环图(DAG), 再采用了动态规划查找最大概率路径,找出基于 ...
- 【English】20190416
anti-money laundering反洗钱[ˈænti][ˈlɔːndərɪŋ] misconduct不当行为[ˌmɪsˈkɑːndʌkt] Currently, she is focus ...
- Java之匿名内部类详解
前言 本文讲解Java中最后一种内部类,叫做匿名内部类.顾名思义,所谓的匿名内部类就是一个没有显式的名字的内部类,在实际开发中,此种内部类用的是非常多的. 匿名内部类 本质:匿名内部类会隐式的继承一个 ...
- Jenkins插件安装
默认的Jenkins平台继有的功能是有限的,插件为Jenkins提供了各种扩展功能,有了插件,Jenkins就会变得更加强大,这里推荐两个不错的插件: 点击菜单栏中“系统管理”--“插件管理”--“A ...
- web 本地存储 (localStorage、sessionStorage)
web 本地存储 (localStorage.sessionStorage,cookie) localStorage(长期储存):即使关闭浏览器数据也不会删除,除非使用localStorage.cle ...
- jQuery 图片查看插件 Magnify 开发简介(仿 Windows 照片查看器)
前言 因为一些特殊的业务需求,经过一个多月的蛰伏及思考,我开发了这款 jQuery 图片查看器插件 Magnify,它实现了 Windows 照片查看器的所有功能,比如模态窗的拖拽.调整大小.最大化, ...
- windows service承载的web api宿主搭建(Microsoft.Owin+service)
今天突然想起改良一下以前搭建的“windows service承载的web api”服务,以前也是直接引用的类库,没有使用nuget包,时隔几年应该很旧版本了吧.所以本次把需要nuget获取的包记录一 ...
- python之yagmail模块--小白博客
yagmail 实现发邮件 yagmail 可以简单的来实现自动发邮件功能. 安装 pip install yagmail 简单例子 import yagmail #链接邮箱服务器 yag = yag ...
- 小程序蓝牙BLE——自动连接设备(手环)
了解小程序蓝牙API: /** *蓝牙API: * 1.初始化蓝牙(判断蓝牙是否可用):openBluetoothAdapter * 2.获取蓝牙设备状态(蓝牙是否打开):getBluetoothAd ...