【问题标题】:AWS single ApiGateway with multiple services具有多种服务的 AWS 单一 ApiGateway
【发布时间】:2021-05-20 13:55:42
【问题描述】:

我正在为我的电子商务应用程序创建一个设计。它将拥有由 AWS Lambdas 支持的多项服务。 Orderservice、InventoryService、PaymentService、LoggingService、dashboardService 是其中的一些服务。我不能给出确切的服务数量,但肯定会超过 15 个。根据这个链接microservices ,一个好的微服务架构应该有一个网关可以路由到相应的服务。我的 AWS ApiGateway 与 order Lambda 函数的代码如下所示。
我的问题是每个 Orderservice、inventoryservice、paymentservice 等都可以有多个获取、发布、删除、放置的路线。它们中的大多数将具有嵌套资源。在这种情况下,我应该在同一个 SAM 模板中包含所有这些服务的 api 路由和 lambda 函数。如果是,那它不是一个整体模板吗?如果我需要更改任何服务,我必须部署整个模板并打破微服务原则。理想情况下,我想独立部署所有这些服务并共享 ApiGateway。这可能吗 ?。如果没有,我是否应该在不同的 SAM 模板中为每个服务创建单独的 ApiGateway,以便可以单独部署它们。这将导致在所有网关中重复身份验证、授权和监控,这听起来又不像是要走的路。
请提出正确的方法。

AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Description: ApiGateway for the Ecommerce 

Resources:
  EcomApi:
    Type: AWS::Serverless::Api
    Properties:
      DefinitionBody:
        swagger: "2.0"
        info:
          title: Ecommerce Api
        schemes:
          - "https"
        x-amazon-apigateway-request-validator: Validate body and params
        paths:
          /order:
            get:
              summary: Get the orders
              consumes:
                - "application/json"
              produces:
                - "application/json"
              responses:
                "200":
                  description: "200 response"
                  schema:
                    $ref: "#/definitions/Empty"
                  headers:
                    Access-Control-Allow-Origin:
                      type: "string"
              security:
                - Auth: []
              x-amazon-apigateway-integration:
                type: "AWS_PROXY"
                httpMethod: "POST"
                uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetOrders.Arn}/invocations
                responses:
                  default:
                    statusCode: "200"
                    responseParameters:
                      method.response.header.Access-Control-Allow-Origin: "'*'"
                passthroughBehavior: "when_no_match"
                contentHandling: "CONVERT_TO_TEXT"


  GetOrders:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: .
      Handler: src/handlers/getOrders.handler
      Role:
        Fn::GetAtt:
          - TestRole
          - Arn
      Events:
        List:
          Type: Api
          Properties:
            Path: /orders
            Method: get
            RestApiId: !Ref EcomApi

【问题讨论】:

    标签: amazon-web-services aws-lambda microservices amazon-cloudformation aws-api-gateway


    【解决方案1】:

    正如您所提到的 - 创建一个单独的 API 网关并与您的 API 共享它。 According to AWS,建议有一个单独的 API 网关来服务多个 Lambda 函数。

    我承认我不是 AWS SAM 专家,但使用 serverless-framework 真的很容易。

    【讨论】:

    • 您共享的链接显示了单个网关,该网关基于 /products、/order 和 /buy 等资源路由到多个服务。有趣的是它看起来很简单。但是,如果有 15 + 这样的资源怎么办。每个资源都有自己的 10 多种方法。是否可以将所有这些 api 部署在单个 cloudformation 中。
    • 这真的取决于你的应用程序。如果您的应用程序与您描述的一样大,那么您应该打破每个服务的 CI/CD。无论哪种方式,独立部署每个服务(只要没有依赖项)都是更好的方法。我认为您的主要障碍是将 CI/CD 设置为使用“基于路径”,并且并非所有 CI/CD 服务都支持它。这是一个如何将单个 API 网关与多个服务一起使用的示例 - github.com/unfor19/serverless-template (serverless-framework)
    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 2021-04-02
    • 2020-11-08
    • 2021-05-14
    • 2019-06-25
    相关资源
    最近更新 更多