简体   繁体   English

如何使用快速网关将多个响应与单个网关端点相结合

[英]How to combined multiple responses with single gateway endpoint using express gateway

I'm trying to create an endpoint at gateway that will call multiple service calls and combine them in one response.我正在尝试在网关创建一个端点,它将调用多个服务调用并将它们组合在一个响应中。 Is that possible with express-gateway?使用 express-gateway 可以吗?

This is my gateway.config.yml.这是我的 gateway.config.yml。

http:
  port: 8080
admin:
  port: 9876
  host: localhost
apiEndpoints:
  api:
    host: localhost
    paths: '/ip'
  uuid:
    host: localhost
    paths: '/uuid'
  agent: 
    host: localhost
    paths: '/user-agent'

serviceEndpoints:
  httpbin:
    url: 'https://httpbin.org'
  uuid:
    url: 'https://httpbin.org'
  agent:
    url: 'https://httpbin.org'    
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines:
  default:
    apiEndpoints:
      - api
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
      - key-auth:
      - proxy:
          - action:
              serviceEndpoint: httpbin 
              changeOrigin: true
  default-1:
    apiEndpoints:
      - uuid
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
      - key-auth:
      - proxy:
          - action:
              serviceEndpoint: uuid 
              changeOrigin: true            
  default-2:
    apiEndpoints:
      - agent
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
      - key-auth:
      - proxy:
          - action:
              serviceEndpoint: agent 
              changeOrigin: true

Basically, I want to combine all of declared serviceEndpoints to one path.基本上,我想将所有声明的 serviceEndpoints 组合到一个路径中。 Let say I trigger /ip , it will call 'api,uuid,agent' serviceEndpoints and combine them all to one response.假设我触发 /ip ,它将调用 'api,uuid,agent' serviceEndpoints 并将它们全部组合为一个响应。 Is that possible?那可能吗?

Express Gateway does not really support such scenario unfortunately.不幸的是,Express Gateway 并不真正支持这种情况。 You're going to have to write your own plugin — but it is not going to be so easy.您将不得不编写自己的插件——但这不会那么容易。

There are multiple approaches suggested on this regard, which you can weigh based on your use-case, tech-stack, team & tech-skills and other variables.在这方面建议了多种方法,您可以根据您的用例、技术堆栈、团队和技术技能以及其他变量进行权衡。

  1. Write an aggregation service that calls other underlying service编写一个聚合服务,调用其他底层服务
  2. Let the Gateway perform the service aggregation, data formatting etc... The important aspect here is to ensure that you don't break the domain boundaries.让网关执行服务聚合、数据格式化等......这里的重要方面是确保您不会打破域边界。 Its highly likely/tempting to inject some domain logic at the gateway along with aggregation, so be very careful about it.它极有可能/很想在网关处注入一些域逻辑以及聚合,所以要非常小心。
  3. Have a look at GraphQL libraries out there, which is good to expose on top of your standard REST apis and you can define the schema and define the resolvers.看看那里的 GraphQL 库,它很好地暴露在您的标准 REST api 之上,您可以定义架构并定义解析器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM