首页 > 其他分享 >AWS Serverless Lambda Function架构设计的几种方式

AWS Serverless Lambda Function架构设计的几种方式

时间:2023-05-05 13:36:20浏览次数:46  
标签:Serverless Function serverless service 架构设计 res app AWS yml

如何构建大型Serverless应用

从路由方式上,Lambda大致可以分为三种架构方式
1:单体应用式
这种方式和传统REST API很相似,以nodejs为例,在service内部使用express框架作路由,如下图所示

代码示例如下

const express = require('express')
const app = express()
app.listen(80, () => {
	console.log('express server running at http://127.0.0.1')
})
app.get('/user',(req,res) => {
  //向客户端发送json对象
  res.send({name:'zs', age:20, gender:'男'})
})
app.post('/user',(req,res) => {
  //向客户端发送文本内容
  res.send('请求成功')
})

这种方式优点在于和传统编程类似,学习成本、以后的代码迁移成本低。
缺点也很明显,服务会很重,cold start时间久,也不利于代码重用。

2:API Gateway路由方式
路由通过API Gateway完成,每个handler都是单一的处理方法。每个handler都有Serverless.yml做lambda的配置。

代码结构如下

my-app/
  service-a/
    src/
      ...
    serverless.yml
  service-b/
    src/
      ...
    serverless.yml

app下有多个service,每个service都有自己的handler和serverless.yml,配置有自己的api gateway,通过Serverless框架的compose方法进行发布。

3:半独立方式
AWS Best Practice并没有这种方式,纯属自己实践中总结
该方式也会在serverless.yml中为每个function配置handler,但不会都配置api gateway。 只为需要通过internet暴露的service配置api gateway。通过其中一个Lambda作为BFF,调度其他Lambda,使用AWS SDK,代码示例如下

    public static void invokeFunction(LambdaClient awsLambda, String functionName) {

         InvokeResponse res = null ;
        try {
            //Need a SdkBytes instance for the payload
            String json = "{\"Hello \":\"Paris\"}";
            SdkBytes payload = SdkBytes.fromUtf8String(json) ;

            //Setup an InvokeRequest
            InvokeRequest request = InvokeRequest.builder()
                    .functionName(functionName)
                    .payload(payload)
                    .build();

            res = awsLambda.invoke(request);
            String value = res.payload().asUtf8String() ;
            System.out.println(value);

        } catch(LambdaException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
    }

通过查看SDK源码,可以看到LambdaClient的基类是SmithyClient, 再查看其文档,AWS Query Protcol也是基于Http/Https,并无其他。
The AWS Query protocol uses HTTP and serializes HTTP requests using query string parameters and responses using XML.

参考:
https://aws.amazon.com/blogs/compute/best-practices-for-organizing-larger-serverless-applications/
https://www.serverless.com/framework/docs/guides/compose
https://www.serverless.com/blog/serverless-framework-compose-multi-service-deployments
https://github.com/aws/aws-sdk-js-v3/blob/main/clients/client-lambda/src/LambdaClient.ts
https://smithy.io/2.0/aws/protocols/aws-query-protocol.html#:~:text=The AWS Query protocol uses HTTP and serializes,using query string parameters and responses using XML.

标签:Serverless,Function,serverless,service,架构设计,res,app,AWS,yml
From: https://www.cnblogs.com/Andy1982/p/17373675.html

相关文章

  • 23-2期中测试获奖用户名单及参考答案:通达系统架构设计
    你好,我是李智慧。今天我们来公布一下期中测试的获奖用户名单和对应的答案。我们期中测试的要求是写一个同城快送业务的系统架构设计文档,这个测试主要考察的目标包括:使用UML进行系统建模的能力,用文档表达设计思路的能力,完整思考一个系统整体架构的能力,以及识别设计落地关键技术问......
  • Built-in Functions
    以为内置函数有很多呢 https://docs.python.org/3/library/functions.html ......
  • AI 作画火了,如何用 Serverless 函数计算部署 Stable Diffusion?
    作者:寒斜立即体验基于函数计算部署StableDiffusion:https://developer.aliyun.com/topic/aigcAIGC领域目前大火,除了Chatgpt,在文生图领域StableDiffusion大放异彩,深刻的地影响着绘画、视频制作等相关领域。利用这项技术,普通人也可以制作出令人惊叹的艺术作品。今天我们将......
  • 生成函数GeneratingFunction
    生成函数GeneratingFunction极限\(\forall\rightarrow\)对于\(\exists\rightarrow\)存在极限:\(\forall\epsilon,\existsN,N>n,|a_n-A|<\epsilon\)就是说,对于所有(任意小的非负整数)\(\epsilon\)存在\(N\),使得\(a_n\)与A的差值小于\(\epsilon\)我们就把\(A\)叫做此序列......
  • TypeError: this.libOptions.parse is not a function
    安装完node.js运行项目后,报错:TypeError:this.libOptions.parseisnotafunctionatESLint8Plugin.<anonymous>(C:\ProgramFiles\JetBrains\GoLand2022.1.4\plugins\JavaScriptLanguage\languageService\eslint\bin\eslint8-plugin.js:139:64)atstep......
  • Router Function
    https://blog.csdn.net/m0_67778103/article/details/123384914 https://blog.csdn.net/zhang33565417/article/details/122012459?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168266912316800215051889%2522%252C%2522scm%2522%253A%252220140713.130102334......
  • CSS animation (animation-timing-function) 测试脚本
    代码:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=de......
  • 浅谈复杂业务系统的架构设计
    作者:京东科技 皮亮1.什么是复杂系统我们经常提到复杂系统,那么到底什么是复杂系统。我们看下维基的定义:复杂系统(英语:complexsystem),又称复合系统,是指由许多可能相互作用的组成成分所组成的系统。强调了两点:由点组成点之间有各种关联两点的规模和复杂性直接决定了系统的......
  • 写代码犹如写文章: “大师级程序员把系统当故事来讲,而不是当做程序来写” | 如何架构
    “大师级程序员把系统当故事来讲,而不是当做程序来写”写代码犹如写文章好的代码应该如好文章一样表达思想,被人读懂。中心思想:突出明确程序是开发者用编程语言写成的一本书,首先应该是记录开发者对业务需求分析、系统分析,最终用软件实现所思所想的知识的记录与传承。然后再是完成程......
  • Day 27 27.1 JS进阶-Function对象之prototype对象
    JS-Function对象之prorotype原型对象案例1://创建方式1varfoo=newFunction("console.log('helloworld!')")foo()//创建方式2functionPerson(){console.log("helloperson")}varp1=Person()console.log(p1)varp2=newPers......