【问题标题】:Is aws lambda can expose only one spring boot apiaws lambda是否只能公开一个spring boot api
【发布时间】:2019-02-11 23:06:00
【问题描述】:

我使用 spring boot 开发了 4 个 api。现在我正在尝试为无服务器启用 aws lambda。是否可以使用单个 lambda 公开 4 个 api。

【问题讨论】:

    标签: java amazon-web-services spring-boot aws-lambda microservices


    【解决方案1】:

    是否可以使用单个 lambda 公开 4 个 api。

    AWS lambda 是 FaaS - 用作服务,每个 Lambda 一个函数。

    但是,您可以使用包装器/代理函数作为入口点来实现预期功能,并根据需要将请求路由到上游方法/函数

    这里有描述aws api gateway & lambda: multiple endpoint/functions vs single endpoint

    查看以下有关创建 API 网关 => Lambda 代理集成的文档: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html

    以下只是对aws api gateway & lambda: multiple endpoint/functions vs single endpoint 此处给出的内容的重新措辞解释。

    AWS 例子有很好的解释;如下所示的 Lambda 请求:

    POST /testStage/hello/world?name=me HTTP/1.1
    Host: gy415nuibc.execute-api.us-east-1.amazonaws.com
    Content-Type: application/json
    headerName: headerValue
    
    {
        "a": 1
    }
    

    最终会将以下事件数据发送到您的 AWS Lambda 函数:

    {
      "message": "Hello me!",
      "input": {
        "resource": "/{proxy+}",
        "path": "/hello/world",
        "httpMethod": "POST",
        "headers": {
          "Accept": "*/*",
          "Accept-Encoding": "gzip, deflate",
          "cache-control": "no-cache",
          "CloudFront-Forwarded-Proto": "https",
          "CloudFront-Is-Desktop-Viewer": "true",
          "CloudFront-Is-Mobile-Viewer": "false",
          "CloudFront-Is-SmartTV-Viewer": "false",
          "CloudFront-Is-Tablet-Viewer": "false",
          "CloudFront-Viewer-Country": "US",
          "Content-Type": "application/json",
          "headerName": "headerValue",
          "Host": "gy415nuibc.execute-api.us-east-1.amazonaws.com",
          "Postman-Token": "9f583ef0-ed83-4a38-aef3-eb9ce3f7a57f",
          "User-Agent": "PostmanRuntime/2.4.5",
          "Via": "1.1 d98420743a69852491bbdea73f7680bd.cloudfront.net (CloudFront)",
          "X-Amz-Cf-Id": "pn-PWIJc6thYnZm5P0NMgOUglL1DYtl0gdeJky8tqsg8iS_sgsKD1A==",
          "X-Forwarded-For": "54.240.196.186, 54.182.214.83",
          "X-Forwarded-Port": "443",
          "X-Forwarded-Proto": "https"
        },
        "queryStringParameters": {
          "name": "me"
        },
        "pathParameters": {
          "proxy": "hello/world"
        },
        "stageVariables": {
          "stageVariableName": "stageVariableValue"
        },
        "requestContext": {
          "accountId": "12345678912",
          "resourceId": "roq9wj",
          "stage": "testStage",
          "requestId": "deef4878-7910-11e6-8f14-25afc3e9ae33",
          "identity": {
            "cognitoIdentityPoolId": null,
            "accountId": null,
            "cognitoIdentityId": null,
            "caller": null,
            "apiKey": null,
            "sourceIp": "192.168.196.186",
            "cognitoAuthenticationType": null,
            "cognitoAuthenticationProvider": null,
            "userArn": null,
            "userAgent": "PostmanRuntime/2.4.5",
            "user": null
          },
          "resourcePath": "/{proxy+}",
          "httpMethod": "POST",
          "apiId": "gy415nuibc"
        },
        "body": "{\r\n\t\"a\": 1\r\n}",
        "isBase64Encoded": false
      }
    }
    

    现在您可以访问所有标头、url 参数、正文等。因此您可以使用它在包装器/代理 Lambda 函数中以不同方式处理请求,并根据您的路由需求路由到上游函数。

    现在很多人都在使用这种方法,而不是为每个方法和一个 api 网关资源创建 lambda 函数。

    这种方法有利有弊

    • 部署:如果每个 lambda 函数都是离散的,那么您可以独立部署它们,这可能会降低代码更改的风险(微服务策略)。相反,您可能会发现需要单独部署功能会增加复杂性和负担。
    • 自我描述:API Gateway 的界面让您可以非常直观地查看 RESTful 端点的布局——名词和动词一目了然。实施您自己的路由可能会牺牲这种可见性。
    • Lambda 大小和限制:如果您代理所有 - 那么您最终需要选择一个实例大小、超时等,以适应您的所有 RESTful 端点。如果您创建离散函数,那么您可以更仔细地选择最能满足特定调用需求的内存占用、超时、死信行为等。

    更多关于Monolithic lambdaMicro Lambda的信息:https://hackernoon.com/aws-lambda-should-you-have-few-monolithic-functions-or-many-single-purposed-functions-8c3872d4338f

    【讨论】:

      猜你喜欢
      • 2019-01-03
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 2019-02-28
      • 2020-03-05
      • 1970-01-01
      • 2017-01-31
      • 1970-01-01
      相关资源
      最近更新 更多