分类目录

链接

2019 年 6 月
 12
3456789
10111213141516
17181920212223
24252627282930

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > 大数据, 微服务 > 正文
API网关ocelot特性之聚合
大数据, 微服务 暂无评论 阅读(250)

请求聚合

即将多个API请求结果合并为一个返回。要实现请求聚合我们需要给其它参与的路由起一个Key。

{
    <span class="hljs-attr">"</span><span class="hljs-attr">ReRoutes": [
        {
            "DownstreamPathTemplate": <span class="hljs-string">"/",
            "UpstreamPathTemplate": "/laura",
            "UpstreamHttpMethod": [
                "Get"
            ],
            "DownstreamScheme": "http",
            "DownstreamHostAndPorts": [
                {
                    "Host": "localhost",
                    "Port": <span class="hljs-number">51881
                }
            ],
            "Key": "Laura"
        },
        {
            "DownstreamPathTemplate": "/",
            "UpstreamPathTemplate": "/tom",
            "UpstreamHttpMethod": [
                "Get"
            ],
            "DownstreamScheme": "http",
            "DownstreamHostAndPorts": [
                {
                    "Host": "localhost",
                    "Port": 51882
                }
            ],
            "Key": "Tom"
        }
    ],
    "Aggregates": [
        {
            "ReRouteKeys": [
                "Tom",
                "Laura"
            ],
            "UpstreamPathTemplate": "/"
        }
    ]
}</span></span></span>

当我们请求/的时候,会将/tom和/laura两个结果合并到一个response返回

{<span class="hljs-attr">"</span><span class="hljs-attr">Tom":{"Age": <span class="hljs-number">19},"Laura":{"Age": 25}}</span></span>

需要注意的是:

  • 聚合服务目前只支持返回json
  • 目前只支持Get方式请求下游服务
  • 任何下游的response header并会被丢弃
  • 如果下游服务返回404,聚合服务只是这个key的value为空,它不会返回404

有一些其它的功能会在将来实现

  • 下游服务很慢的处理
  • 做一些像 GraphQL的处理对下游服务返回结果进行处理
  • 404的处理

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:API网关ocelot特性之聚合 | Bruce's Blog

发表评论

留言无头像?