[译]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中.如 ...
随机推荐
- Python简单的多线程demo:常用写法
简单多线程实现:启动50个线程,并计算执行时间. import threading import time def run(n): time.sleep(3) print("task:&qu ...
- threejs学习笔记(二)
THREE.WebGLRenderer THREE.Scene THREE.OrthographicCamera正交相机 THREE.PerspectiveCamera透视相机 renderer.se ...
- Linux-基础学习(四)-部署图书管理系统项目
部署图书管理项目需要以下软件 项目文件(django项目文件夹) 数据库文件(django项目对应的数据库文件) centos7(linux本体) nginx(反向代理以及静态文件收集) uWSGI( ...
- 基于winserver的Apollo配置中心分布式&集群部署实践(正确部署姿势)
基于winserver的Apollo配置中心分布式&集群部署实践(正确部署姿势) 前言 前几天对Apollo配置中心的demo进行一个部署试用,现公司已决定使用,这两天进行分布式部署的时候 ...
- python之pickle
#!/usr/bin/python # -*- coding: UTF- -*- ''' ''' import pickle # pickle 只能Python识别 不适用于别的语言 li = [, ...
- BZOJ3711 Druzyny 最大值分治、线段树
传送门 被暴力包菜了,然而还不会卡-- 有一个很暴力的DP:设\(f_i\)表示给\(1\)到\(i\)分好组最多可以分多少组,转移枚举最后一个组.接下来考虑优化这个暴力. 考虑:对于每一个位置\(i ...
- 横线和文字一排,文字居中显示vertical-align: middle;
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- 【北航软件工程】Alpha阶段前端页面编写及服务器部署
前端页面编写 虽然之前对html语法有过一些了解,但是完全没有编写前端页面的经验,和我合作的czy大概也是这么个情况.在Alpha阶段的前端页面编写过程中,我们是摸着石头过河,html是个入门很快专精 ...
- ondaHTTPError: HTTP 000 CONNECTION FAILED for url
可能是网络问题,换网络. 可能是获取库的IP无法链接到,换库的IP,如添加清华镜像IP等.
- YCD 软件更新方法
备份Messenger的数据库和Player的Chainmail数据 Upgrade process in Cnario is quite simple, uninstall old version ...