2

我是 GET REQUEST 的服务 urlhttp://myipaddress:5000/api/Tenant/tenants/{TenantID}

TenantID 是动态的

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

在这篇文章中,请求有效负载在请求正文中传递。

我的网关配置 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 端点

4

1 回答 1

3

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

- 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 进行特殊处理。如果您需要网关仅过滤您可以添加的特定方法

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
于 2017-12-15T07:52:50.927 回答