【问题标题】:Express gateway gives 404快递网关给404
【发布时间】:2018-05-28 10:53:22
【问题描述】:

我是服务网址 获取请求http://myipaddress:5000/api/Tenant/tenants/{TenantID}

TenantID 将是动态的

我也有 http://myipaddress:5000/api/Tenant/tenants 的 POST

在此帖子中,请求有效负载在请求正文中传递。

我的网关配置 yml 文件如下

http:
  port: 8080
admin:
  port: 9876
  hostname: localhost
apiEndpoints:
  api:
    host: localhost
    paths: '/ip'
  tenant-api:
    host: localhost
    paths: '/api/Tenant/tenants/*'

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

当我尝试通过代理执行 GET 请求时得到 404。你也可以让我知道如何在我的 gatewayconfig.yml 中添加 POST api 端点

【问题讨论】:

    标签: node.js express-gateway


    【解决方案1】:

    第一个问题是您在代理策略中有多个操作

    - action:  # this one is always executing and goes to the httpbin
          serviceEndpoint: httpbin 
          changeOrigin: true
    - action:
          serviceEndpoint: tenant-svc 
          changeOrigin: true
    

    删除第一个动作,让所有调用都转到tenant-svc

    - action:
          serviceEndpoint: tenant-svc 
          changeOrigin: true
    

    此配置将接受所有方法 GET,POST '/api/Tenant/tenants/*' urls 上的任何内容

    要制作 Express-Gateway 进程 /api/Tenant/tenants url,您可以修改 api 端点,如:

    paths: ['/api/Tenant/tenants/*', '/api/Tenant/tenants' ] 
    

    https://www.express-gateway.io/docs/configuration/gateway.config.yml/apiEndpoints#markdown

    我认为不需要对 GET POST 进行特殊处理。 如果您需要 Gateway 仅过滤您可以添加的特定方法

    methods: 'POST,PUT' 
    

    到您的 api 端点配置

    所以最终配置看起来像

    http:
      port: 8080
    admin:
      port: 9876
      hostname: localhost
    apiEndpoints:
      tenant-api:
        host: localhost
        methods: 'GET,POST,PUT' 
        paths: ['/api/Tenant/tenants/*', '/api/Tenant/tenants' ] 
    
    serviceEndpoints:
      tenant-svc:
        url: 'http://localhost:5000'
    policies:
      - basic-auth
      - cors
      - expression
      - key-auth
      - log
      - oauth2
      - proxy
      - rate-limit
    pipelines:
      default:
        apiEndpoints:
          - tenant-api
        policies:
          - proxy:
              - action:
                  serviceEndpoint: tenant-svc 
                  changeOrigin: true
    

    或者您可以通过方法将多个 API 端点连接到同一个管道

      tenant-api-1:
        host: localhost
        methods: 'GET' 
        paths: '/api/Tenant/tenants/*'
    
      tenant-api-2:
        host: localhost
        methods: 'POST' 
        paths:  '/api/Tenant/tenants' 
    

    更新:多服务使用

    http:
      port: 8080
    admin:
      port: 9876
      hostname: localhost
    apiEndpoints:
      tenant-api:
        host: localhost
        methods: 'GET,POST,PUT' 
        paths: ['/api/Tenant/tenants/*', '/api/Tenant/tenants' ] 
      product-api:
        host: localhost
        paths: ['/api/products/*'] 
    serviceEndpoints:
      tenant-svc:
        url: 'http://localhost:5000'
      product-svc:
        url: 'http://localhost:6000'
    policies:
      - proxy
    pipelines:
      tenant:
        apiEndpoints:
          - tenant-api
        policies:
          - proxy:
              - action:
                  serviceEndpoint: tenant-svc 
                  changeOrigin: true
      products:
        apiEndpoints:
          - product-api
        policies:
          - proxy:
              - action:
                  serviceEndpoint: product-svc 
                  changeOrigin: true
    

    【讨论】:

    • 这是否意味着我只能指定一个动作。我想将此解决方案用于我的微服务应用程序。我有 70 多个微服务,其中每个服务至少有 3 个端点。我怎样才能有一个配置来容纳所有服务?
    • 通常您为不同和不同的 api 端点创建多个管道以匹配 url /tenants/* -> 转到“租户”管道 -> 管道具有到 tenant-src 端点 /products/* 的代理策略-> 转到“产品”管道 -> 管道具有到 products-src 端点的代理策略 ---- 可以执行多个操作。例如,如果您有条件,那么只有当条件与大多数用例匹配时才会执行操作,您只需要一个操作
    • 我已经用示例更新了答案。顺便说一句,当您想查看代理请求的确切位置时,使用环境变量“LOG_LEVEL=debug”运行 Express Gateway,例如“LOG_LEVEL=debug npm start”,如果触发了代理策略,它将输出带有目标 url 的消息
    • 这对我很有帮助。谢谢
    • 所以要将资源 URL 路由到适当的微服务,我必须声明一个 apiEndpoint、一个 serviceEndpoint 和一个管道,每个管道都有一个代理策略?
    猜你喜欢
    • 2012-10-27
    • 1970-01-01
    • 2017-10-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    相关资源
    最近更新 更多