带有 API 网关的 AWS ECS 中基于路径的微服务路由

Posted

技术标签:

【中文标题】带有 API 网关的 AWS ECS 中基于路径的微服务路由【英文标题】:Path based routing for Microservices in AWS ECS with API Gateway 【发布时间】:2021-04-01 17:29:42 【问题描述】:

我们首次在 AWS ECS 中部署节点微服务,每个服务都在监听动态端口。

以下是启动节点应用程序时暴露的微服务的API。

动物微服务

[Nest] 29   - 12/22/2020, 8:12:30 AM   [RouterExplorer] Mapped /animal/:id, GET route +2ms
[Nest] 29   - 12/22/2020, 8:12:30 AM   [RouterExplorer] Mapped /animal/getAll, GET route +1ms
[Nest] 29   - 12/22/2020, 8:12:30 AM   [RouterExplorer] Mapped /animal, POST route +1ms
[Nest] 29   - 12/22/2020, 8:12:30 AM   [RouterExplorer] Mapped /animal/:id, PUT route +1ms
[Nest] 29   - 12/22/2020, 8:12:30 AM   [RouterExplorer] Mapped /animal/:id, DELETE route +1ms
[Nest] 29   - 12/22/2020, 8:12:30 AM   [RoutesResolver] dogPlanController /dog: +0ms
[Nest] 29   - 12/22/2020, 8:12:30 AM   [RouterExplorer] Mapped /dog/:id, GET route +1ms
[Nest] 29   - 12/22/2020, 8:12:30 AM   [RouterExplorer] Mapped /dog, POST route +1ms
[Nest] 29   - 12/22/2020, 8:12:30 AM   [RouterExplorer] Mapped /dog/:id, DELETE route +1ms

鸟微服务

[Nest] 29   - 12/22/2020, 8:12:30 AM   [RouterExplorer] Mapped /bird/:id, GET route +2ms
[Nest] 29   - 12/22/2020, 8:12:30 AM   [RouterExplorer] Mapped /bird/getAll, GET route +1ms
[Nest] 29   - 12/22/2020, 8:12:30 AM   [RouterExplorer] Mapped /bird, POST route +1ms
[Nest] 29   - 12/22/2020, 8:12:30 AM   [RouterExplorer] Mapped /bird/:id, PUT route +1ms
[Nest] 29   - 12/22/2020, 8:12:30 AM   [RouterExplorer] Mapped /bird/:id, DELETE route +1ms
[Nest] 29   - 12/22/2020, 8:12:30 AM   [RoutesResolver] eaglePlanController /eagle: +0ms
[Nest] 29   - 12/22/2020, 8:12:30 AM   [RouterExplorer] Mapped /eagle/:id, GET route +1ms
[Nest] 29   - 12/22/2020, 8:12:30 AM   [RouterExplorer] Mapped /eagle, POST route +1ms
[Nest] 29   - 12/22/2020, 8:12:30 AM   [RouterExplorer] Mapped /eagle/:id, DELETE route +1ms

用户微服务

[Nest] 28   - 12/21/2020, 11:29:32 PM   [RouterExplorer] Mapped /healthcheck, GET route +0ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RoutesResolver] SsoController /sso: +0ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RouterExplorer] Mapped /sso, POST route +1ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RoutesResolver] AuthController /auth: +0ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RouterExplorer] Mapped /auth/signup, POST route +1ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RouterExplorer] Mapped /auth/sendotp, POST route +1ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RouterExplorer] Mapped /auth/verifyOtp, POST route +0ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RouterExplorer] Mapped /auth/login, POST route +1ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RouterExplorer] Mapped /auth/duplicateUserName, GET route +0ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RouterExplorer] Mapped /auth/resetPasswordOtp, POST route +1ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RouterExplorer] Mapped /auth/resetPassword, POST route +1ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RoutesResolver] UserController /user: +0ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RouterExplorer] Mapped /user/questionnaire/:id, POST route +1ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RouterExplorer] Mapped /user/questionnaire/:id, GET route +0ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RouterExplorer] Mapped /user/:id, GET route +1ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RouterExplorer] Mapped /user, GET route +0ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RouterExplorer] Mapped /user/userList, POST route +1ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RouterExplorer] Mapped /user/connectWearable, POST route +0ms
[Nest] 28   - 12/21/2020, 11:29:32 PM   [RouterExplorer] Mapped /user/getArticles, POST route +1ms

后端微服务

[Nest] 29   - 12/22/2020, 12:02:28 AM   [RoutesResolver] AuthController /auth: +1ms
[Nest] 29   - 12/22/2020, 12:02:28 AM   [RouterExplorer] Mapped /auth/signup, POST route +0ms
[Nest] 29   - 12/22/2020, 12:02:28 AM   [RouterExplorer] Mapped /auth/login, POST route +1ms
[Nest] 29   - 12/22/2020, 12:02:28 AM   [RoutesResolver] animalController /animal: +0ms
[Nest] 29   - 12/22/2020, 12:02:28 AM   [RouterExplorer] Mapped /animal/color, GET route +1ms
[Nest] 29   - 12/22/2020, 12:02:28 AM   [RouterExplorer] Mapped /animal/color, POST route +0ms
[Nest] 29   - 12/22/2020, 12:02:28 AM   [RouterExplorer] Mapped /animal, GET route +1ms
[Nest] 29   - 12/22/2020, 12:02:28 AM   [RouterExplorer] Mapped /animal, POST route +1ms
[Nest] 29   - 12/22/2020, 12:02:28 AM   [RouterExplorer] Mapped /animal/:id, DELETE route +0ms
..
..
..
..

开发者期待以下路由。

注意:仔细观察他们请求的路由。

对于动物微服务,

example.com/animal  -->>  animal-microservice-url/animal

example.com/animal/dog  -->>  animal-microservice-url/dog


对于 USER 微服务,

example.com/user  -->> user-microservice-url/user

example.com/user/auth  -->> user-microservice-url/auth

example.com/user/sso  -->> user-microservice-url/sso

有人可以帮我在 AWS 环境中实现这一点吗?

我尝试了基于 ALB 路径的路由,但由于 ALB 没有去除 alburl 中的后缀,因此该方案没有成效。

我也尝试过使用 ALB 的 API Gateway。我设置了一些基于 ALB 路径的路由,以便它可以接受来自 apigateway 的请求并将它们转发到目标组。

但是,我在 APIGateway 中设置 `wildcard, *, in ALB path based routing' 以将所有后缀路由到一个 url 时遇到了麻烦。

示例:

apigatewayurl.com/animal    -->>    alburl.com/animal

apigatewayurl.com/animal/dog    -->>    alburl.com/dog

以上两个工作正常。

但以下不是。

apigatewayurl.com/animal/:id    -->>    alburl.com/animal/:id

apigatewayurl.com/animal/getAll -->>    alburl.com/animal/getAll

apigatewayurl.com/animal/dog/:id    -->>    alburl.com/dog/:id

如何创建一个 HTTP API 路由,以便它从 apigatewayurl 访问 alburl 中的所有后缀?

比如,当我输入apigatewayurl.com/animal/dog/* -->> alburl.com/dog/*

我尝试尽可能地提出问题。

请随时询问配置。

如果这可以通过 AWS 中的任何其他服务完成,请在此处建议我。

【问题讨论】:

【参考方案1】:

我认为这是不可能的。因为这里 ALB 只是检查路径,它只是验证给定的路径是否等于那里提到的路径,它不能接受值.. 你在 URL 中发送值。

我的建议是 - 在请求正文中发送 id 或数据值,而不是在请求 URL 中发送。并在您的 API 中捕捉这些价值

【讨论】:

是的。我们已经尝试过 API Gateway,但我们认为它行不通。开发人员使用全局前缀更改了后端代码。

以上是关于带有 API 网关的 AWS ECS 中基于路径的微服务路由的主要内容,如果未能解决你的问题,请参考以下文章

用于微服务的私有子网 ecs 连接上负载均衡器的 AWS API 网关

尝试通过 Cloudformation 创建 AWS API 网关时出错

AWS ECS:VPC 终端节点和 NAT 网关

如何在 Node.js 中使用带有 API 网关的 AWS Lambda 发送二进制响应? [复制]

带有 Cognito 的 AWS Lambda API 网关 - 如何使用 IdentityId 访问和更新 UserPool 属性?

带有 SAM 的 API 网关未正确更新