组件概述
腾讯云 SCF 云函数组件通过使用 Serverless Cloud Framework ,基于云上 Serverless 服务(云函数及触发器等),支持同时部署多个云函数,该组件支持丰富的配置扩展,提供了目前最易用、低成本并且弹性伸缩的多个云函数的开发、配置及部署能力。
操作步骤
创建
方式1:参考 快速创建应用模板,选择 SCF 项目模板进行创建。
方式2:直接使用
scf init命令创建。例如,快速创建 Node.js 示例:scf init multi-scf-nodejs
说明:
目前 multi-scf 组件支持的 helloworld 模板有:multi-scf-golang、multi-scf-php、multi-scf-python,只需要将命令中的 multi-scf-nodejs 更换为前面列出的模板名称,即可快速初始化相应语言模板。
部署
执行
cd multi-scf-nodejs进入项目目录,执行scf deploy命令,将会弹出二维码,直接扫码授权进行部署。查看
执行
scf info命令,查看您部署的项目信息。移除
执行
scf remove命令,移除您已经部署的项目。注意:
使用 remove 命令后,将删除已部署的应用,同时删除对应的函数,且无法恢复,请谨慎使用此命令。
配置详情
全部配置
# serverless.yml#应用组织信息app: '' # 应用名称。留空则默认取当前组件的实例名称为app名称。stage: '' # 环境名称。默认值是 dev。建议使用${env.STAGE}变量定义环境名称#组件信息component: multi-scfname: multi-scfdemo#组件参数配置inputs:# 需要多个函数同时生效的配置参数放到这里src:src: ./ # 指定当前需要上传的包含工作流配置文件的目录exclude:- .envregion: ap-shanghai # 云函数所在区域namespace: defaulttype: event # 函数类型,默认为 event(事件类型),web(web类型)memorySize: 128 # 内存大小,单位MBtimeout: 20 # 函数执行超时时间,单位秒initTimeout: 3 # 初始化超时时间,单位秒publicAccess: true # 是否开启公网访问installDependency: false # 是否在线安装依赖vpcConfig: # 私有网络配置vpcId: vpc-xxx # 私有网络的IdsubnetId: subnet-xxx # 子网IDcfs: # cfs配置- cfsId: cfs-xxx # cfs IDmountInsId: cfs-xxx # mout IDlocalMountDir: /mnt/ # 本地挂载路径remoteMountDir: / # 远程挂载路径deadLetter: # 死信队列配置type: deadLetterTypename: deadLetterNamefilterType: deadLetterFilterTypelayers: #layer配置- name: scfLayer # layer名称version: 1 # 版本cls: # 函数日志logsetId: 3010c83a-f46d-45a8-94ba-d1567e8ca2f2topicId: 9bedc538-3f03-4cc4-9eb3-376533d6db12eip: true # 是否开启固定IP,true: 开启, false: 不开启,默认不开启asyncRunEnable: true # 是否启用异步执行(长时间运行)traceEnable: true # 是否状态追踪msgTTL: 21600 # 消息保留时间,单位秒retryNum: 2 # 重试次数dnsCache: true # 是否开启dns缓存,true: 开启, false: 不开启,默认不开启overClockConfig: # 请求缓冲队列timeOut: 15 # 最大缓冲时间,单位秒,范围 1-15秒publish: false # 部署的同时是否发布新版本,true:发布;false:不发布aliasName: $DEFAULT #别名及流量灰度配置,需发布或使用的函数别名aliasDescription: 'cli create alisa description' #别名描述,非必填aliasFunctionVersion: $LATEST # 别名下配置的版本1additionalVersionWeights: #别名下配置的版本2及权重,自动为版本1分配剩余权重- version: 1weight: 0.4publishDescription: 'cli publish version' # 版本描述environments: # 环境变量- key: NODE_ENVvalue: developmenttags: # 标签配置- key: slstestvalue: slstestfunctions:index:# 仅仅需要某个函数生效的配置参数放到这里handler: index.indextype: event # 函数类型,默认为 event(事件类型),web(web类型)runtime: Nodejs24.11instanceConcurrencyConfig: # 单实例多并发配置,仅web函数支持开启enable: true #是否开启dynamicEnabled: false #是否开启智能动态并发,true: 动态并发,false:表示静态并发maxConcurrency: 1 #最大并发数,dynamicEnabled是false时,可进行设置protocolType: 'WS' #当前支持 WebSockets 协议,值为 WS,只有 type:web 时此配置生效。protocolParams:wsParams:idleTimeOut: 60 #空闲超时时间, 单位秒,默认15s。可配置范围1-1800s# image: # 镜像配置# registryId: tcr-xxx # 容器镜像服务名称,企业版必须# imageType: personal # 镜像类型:personal - 个人,enterprise - 企业版,public - 镜像模板# imageUrl: ccr.ccs.tencentyun.com/sls-scf/nodejs_test:latest@sha256:xxx# command: node index.js # 容器启动命令# args: test # 容器启动参数# containerImageAccelerate: true # 是否开启镜像加速# imagePort: 9000 # -1表示 job镜像, 9000 表示webServer镜像。不传或为空时,默认取9000hello:# 仅仅需要某个函数生效的配置参数放到这里handler: index.hellotype: event # 函数类型,默认为 event(事件类型),web(web类型)memorySize: 256timeout: 10runtime: Nodejs22.21triggers: # 触发器- type: clb # clb触发器function: indexparameters:loadBalanceId: stringdomain: stringprotocol: stringport: stringurl: stringweight: string- type: timer # 定时触发器function: indexparameters:name: timer1qualifier: $DEFAULT # 别名配置cronExpression: '*/5 * * * * * *' # 每5秒触发一次enable: trueargument: argument # 额外的参数- type: http #函数URLfunction: indexparameters:netConfig:enableIntranet: trueenableExtranet: truequalifier: $DEFAULTauthType: NONE- type: mqtt # mqtt触发器function: index # 函数名parameters:enable: true # 是否开启mqtt触发器qualifier: $DEFAULT # 函数别名/版本name: test-xx # 触发器名称timeout: 60 # 超时时间MaxMsgNum: 40 # 最大消息数RetryNum: 1 # 重试次数InstanceId: "mqtt-xxx" # 实例idtopicFilters: '#' # 订阅主题过滤器, 支持多个用逗号分隔配置多个过滤器,如:#,+userName: "xxxx" # 用户名password: "xxxx" # 密码encodeType: "Base64StdEncoding" # 编码类型,传空字符串,则不开启编码userProperties:"$where": "where $Qos = 1 AND K1 = v1 OR K2 = v2"- type: cos #cos触发器function: indexparameters:qualifier: $DEFAULT # 别名配置bucket: cli-appid.cos.ap-beijing.myqcloud.comfilter:prefix: filterdir/suffix: .jpgevents: 'cos:ObjectCreated:*'enable: true- type: cmq # CMQ Topic 触发器function: indexparameters:qualifier: $DEFAULT # 别名配置name: test-topic-queueenable: truefilterType: 1 # 消息过滤类型,1为标签类型,2为路由匹配类型filterKey: # 当 filterType 为1时表示消息过滤标签,当 filterType 为2时表示 Binding Key- key1- key2- type: ckafka # ckafka触发器function: indexparameters:name: ckafka-xxx-xxxqualifier: $DEFAULTinstanceId: ckafka-xxxtopic: testxxxmaxMsgNum: 999retry: 10000offset: latesttimeout: 60consumerGroupName: ckafkaxxx #消费组名称,不传时将自动创建新消费组easConfig:userName: ckafka-xxxxpassword: "******"enable: true- type: cls # cls 触发器function: indexparameters:qualifier: '$DEFAULT' # 别名配置topicId: 'xxx-228b-42f5-aab5-7f740cc2fb11' # 日志主题 IDmaxWait: 60 # 最长等待时间,单位秒enable: true- type: mpsfunction: indexparameters:qualifier: $DEFAULT # 别名配置type: EditMediaTask # 事件类型enable: true- type: apacheKafka # 自定义触发器function: indexparameters:name: xxx-apache-kafka-xxx #触发器名称enable: truetopicName: testxxx #要订阅的主题bootstrapServers:- x.xxx.92.106:9006 #需连接消费的自建 Apache Kafka 实例地址,支持IP+端口或域名+端口offset: latestmaxMsgNum: 1timeout: 1consumerGroupName: testxx #消费组名称retry: 10easConfig: #配置Apache Kafka 实例允许消费的账号密码信息及安全协议、身份验证机制username: xxxpassword: xxxsecurityProtocol: SASL_PLAINTEXTmechanism: PLAIN