0

好吧,我需要创建一个端点,可以使用 express-gateway 创建一个用户,其中有 2 个端口正在运行。

  1. 网关 http 服务器监听 :::8181
  2. 管理员 http 服务器正在监听 127.0.0.1:9876

我可以创建一个用户,将我的信息发送到:

http://127.0.0.1:9876/users

我不能使用这个我的端点,因为我的前端有其他配置,所以在我的前端将我的创建用户信息发送到:

http://localhost:8181/api/user/create

现在我需要将我的信息发送到此http://localhost:8181/api/user/create并将网关中的内部重定向到此http://127.0.0.1:9876/users,我尝试了一些方法,但只是有错误的网关或找不到。我称这个端点为用户,所以这是脚本。

http:
  port: 8181
admin:
  port: 9876
  host: localhost
apiEndpoints:
  events:
    host: localhost
    paths: ["/api/events*", "/swagger*"]
    methods: ["GET", "PATCH"]
  users:
    host: localhost
    paths: "/api/user/create*"
    url: "http://localhost:9876"
    methods: ["POST", "OPTIONS"]
  eventsCreate:
    host: localhost
    paths: "/api/events*"
    methods: ["POST", "PUT", "OPTIONS"]
  auth:
    host: localhost
    paths: "/api/auth*"
    methods: ["POST", "GET", "OPTIONS"]
serviceEndpoints:
  auth:
    url: "http://localhost:59868"
  events:
    url: "http://localhost:5000"
  users:
    url: "http://localhost:9876"
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
  - jwt
  - request-transformer
pipelines:
  authPipeline:
    apiEndpoints:
      - auth
    policies:
      - cors:
      - log:
          action:
            message: "auth ${req.method}"
      - proxy:
          action:
            serviceEndpoint: auth
            changeOrigin: true
  eventsPipeline:
    apiEndpoints:
      - events
    policies:
      - cors:
      - log:
          action:
            message: "events ${req.method}"
      - proxy:
          action:
            serviceEndpoint: events
            changeOrigin: true
  usersPipeline:
    apiEndpoints:
      - users
    policies:
      - cors:
      - log:
          action:
            message: "users ${req.method}"
      - proxy:
          action:
            serviceEndpoint: users
            changeOrigin: true
  userPipeline:
    apiEndpoints:
      - events
    policies:
      - cors:
      - log:
          action:
            message: "events ${req.method}"
      - proxy:
          action:
            serviceEndpoint: events
            changeOrigin: true
  eventsCreatePipeline:
    apiEndpoints:
      - eventsCreate
    policies:
      - cors:
      - log:
          action:
            message: "events ${req.method}"
      - jwt:
          action:
            secretOrPublicKey: "MORTADELAIsMyPassion321"
            checkCredentialExistence: false
      - proxy:
          action:
            serviceEndpoint: events
            changeOrigin: true
4

1 回答 1

0

您正在尝试将传入 URL 映射http://localhost:8181/api/user/create到 Express Gateway 管理 URL http://localhost:9876/users,但您的代理策略仅更改 URL 的主机名和端口组件,而不是路径。

在 Proxy 文档的路径管理部分进行了描述。要更改路径,您需要调整现有users服务端点或创建一个新端点,并向代理中间件配置添加一些说明:

例如,添加一个名为的新 ServiceEndpoint userCreate

serviceEndpoints:
  auth:
    url: "http://localhost:59868"
  userCreate:
    url: "http://localhost:9876/users"
  users:
    url: "http://localhost:9876"

然后引用新的服务端点并在代理配置中设置 stripPath:

      - proxy:
         action:
            serviceEndpoint: userCreate
            changeOrigin: true
            stripPath: true
于 2020-11-07T11:57:18.157 回答