【问题标题】:Microservices with express gateway : routes return 404 error具有快速网关的微服务:路由返回 404 错误
【发布时间】:2019-02-14 19:08:13
【问题描述】:

我的微服务在端口3000 中运行,我正在尝试通过express-js gateway 代理请求

但是我在访问微服务中的路由时遇到了问题

微服务路线

app.use('/sample',(req,res)=>{
   res.json({
     message:'Run with microservice'
   })
})

我可以访问这条路线http://localhost:3000/sample

这是我的gateway.config.yml

http:
  port: 3004
admin:
  port: 9876
  hostname: localhost
apiEndpoints:
  test:
    host: localhost
    paths: '/test'
serviceEndpoints:
  test:
    url: 'http://localhost:3002/'  
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines: 
  test:
    apiEndpoints:
      - test
    policies:
      - proxy:
          - action:
              serviceEndpoint: test
              changeOrigin: true              

当我尝试通过http://localhost:3004/test/sample 访问我的微服务路由时,返回cannot get route error

【问题讨论】:

    标签: node.js express microservices express-gateway


    【解决方案1】:

    一般来说,请务必查看我们的Path Management Piece。这将帮助您了解转发到您的服务的 PATH 是什么。

    具体谈谈你的问题——如果你想转发一个可以“继续”超出规范的路径,你需要将路径定义从/test更改为/test*。这指示网关路径不完整,可能会提供其他内容。

    另外,在服务方面,我会将app.use 更改为app.get,这样您就只会听您感兴趣的动词。

    干杯!

    【讨论】:

    • 我已将路径更改为paths: '/test*',但仍然出现 404 错误
    • 服务的转发路径确实是/test/sample——如果你想让它只是代理到/sample,你需要在代理策略选项中启用stripPath选项。看看我上面给你的链接。
    • 在哪里可以添加代理策略参数??
    • 我建议您查看我们的代理政策页面,这样您就可以看到正确放置所有选项的位置。
    • @Vincenzo 你有没有使用 nodejs 的微服务示例手册?
    【解决方案2】:

    我会选择 Janus - https://hellofresh.gitbooks.io/janus/ 进行 API 网关管理,因为它专门针对这一点。看看,让我知道你的想法。

    【讨论】:

    • 问题是关于 express-gateway 我正在为我当前的问题寻求解决方案而不是替代
    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 2021-02-21
    • 2016-07-22
    • 1970-01-01
    • 2020-04-20
    相关资源
    最近更新 更多