为了账号安全,请及时绑定邮箱和手机立即绑定

aws lambda 是否只能公开一个 spring boot api

aws lambda 是否只能公开一个 spring boot api

qq_花开花谢_0 2021-09-03 14:33:18
我已经使用 Spring Boot 开发了 4 个 api。现在我正在尝试为无服务器启用 aws lambda。是否可以使用单个 lambda 公开 4 个 api。
查看完整描述

1 回答

?
慕侠2389804

TA贡献1719条经验 获得超6个赞

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

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

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

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

//img1.sycdn.imooc.com//6131c1ea000197fb12580710.jpg

查看以下有关创建 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 lambdavsMicro Lambda在这里:https : //hackernoon.com/aws-lambda-should-you-have-few-monolithic-functions-or-many-single-purposed-functions-8c3872d4338f


查看完整回答
反对 回复 2021-09-03
  • 1 回答
  • 0 关注
  • 126 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信