Ocelot容许您指定聚合多个普通ReRoutes的Aggregate ReRoutes(聚合路由),并将其响应映射到一个对象中。通常用于当您有一个客户端向服务器发出多个请求,而这些请求能够合并成一个的时候。此功能容许您经过Ocelot实现前端类型结构的后端。css
此功能是问题 79的一部分,而且做为问题 298的一部分进行了进一步改进。html
为了设置它,你必须在ocelot.json中作以下的事情。 这里咱们已经指定了两个普通的ReRoutes,每个都有一个Key属性。 而后,咱们使用ReRouteKeys列表中的键指定组成两个ReRoutes的聚合,而后设置UpstreamPathTemplate,它的工做方式与普通的ReRoute类似。 很明显,您不能在ReRoutes和Aggregates之间复制UpstreamPathTemplates。 除RequestIdKey以外,您可使用普通ReRoute全部的选项(在下面的陷阱中进行了解释)。前端
高级应用-注册你本身的聚合器
Ocelot只是基本的请求聚合,而后咱们添加了一个更高级的方法,让用户从下游服务中获取响应,而后将它们聚合到响应对象中。java
ocelot.json的设置与基本聚合方法几乎相同,只需额外添加一个Aggregator属性,以下所示。git
{
"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的聚合器。当Ocelot尝试聚合这个ReRoute的时候,会去查看这个聚合器。github
为了使这个聚合器可用,咱们必须像下面这样把FakeDefinedAggregator添加到OcelotBuilder。json
services
.AddOcelot()
.AddSingletonDefinedAggregator<FakeDefinedAggregator>();
如今,当Ocelot尝试聚合上述ReRoute时,它会在容器中找到FakeDefinedAggregator并使用它来聚合ReRoute。 因为FakeDefinedAggregator是在容器中注册,所以您能够将它须要的任何依赖项都添加到容器中,以下所示。后端
services.AddSingleton<FooDependency>();
services
.AddOcelot()
.AddSingletonDefinedAggregator<FooAggregator>();
在这个例子中FooAggregator依赖FooDependency,将会被容器解析。服务器
除此以外,Ocelot还容许您添加以下所示的瞬态聚合器。(参考.net core依赖注入,译者注)markdown
services
.AddOcelot()
.AddTransientDefinedAggregator<FakeDefinedAggregator>();
为了实现一个聚合器,你必须实现这个接口。
public interface IDefinedAggregator {
Task<DownstreamResponse> Aggregate(List<DownstreamResponse> responses);
}
使用此功能,您几乎能够作任何您想作的事情,由于DownstreamResponse包含内容,头和状态代码。 若是须要,咱们能够添加额外的东西,只需在GitHub上提出这个问题。请注意,若是在向聚合中的ReRoute发出请求时HttpClient抛出异常,那么您将不会得到其DownstreamResponse,但您会得到其余请求成功的DownstreamResponse。 若是某个请求抛出异常,则会被记录。
基本演示
{
"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": "/"
}
]
}
你也能够设置Aggregate的UpstreamHost和ReRouteIsCaseSensitive,和其余ReRoutes的做用是同样的。
如何路由/tom返回 {“Age”: 19},路由/laura返回{“Age”: 25},那么聚合以后的相应就以下所示。
{"Tom":{"Age": 19},"Laura":{"Age": 25}}
目前的聚合功能很是简单。 Ocelot只是从你的下游服务得到响应,并将其复制到json字典中,如上所示。将ReRoute键做为字典的关键字,下游服务的响应体做为值。你能够看到这个对象就是没有任何缩进空格的JSON。
来自下游服务相应的全部头部都会丢失。
Ocelot将老是将聚合请求的内容类型返回application/json。
若是下游服务返回404,那么聚合将为该下游服务返回空内容。 即便全部下游都返回404,它也不会是聚合响应为404。
疑难杂症 / 更多信息
您不能将ReRoutes与特定的RequestIdKeys一块儿使用,由于这将使跟踪很是的复杂。
聚合仅支持GET HTTP动词。
原做者:小水 原文连接:https://www.cnblogs.com/loogn/p/9007768.html