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