立即点击-> 选择您最常用的编程语言,免费体验 Serverless Demo
Python 是一种热门的编程语言,Serverless 是近年来迅速兴起的一个技术概念,基于Serverless架构能构建出多种应用场景,适用于各行各业。
前三期直播内容,我们详细阐述了什么是Serverless Component,Component 在实战中的应用。如果现有的 Component 无法满足诉求,我们应该如何制作一个自己的Component呢?这样多个云资源编排的Serverless场景化的解决方案是如何运行的,开发者应该怎样按照自己的需求定制化开发?本次将分享如何开发一个 Serverless Express 组件。
文章整理自 Serverless Framework 技术专家陈涛在腾讯云大学的分享。
本次分享内容:
1、Serverless Component 运行机制 2、一个完整的Component 开发步骤 3、Serverless Component 组件开发 4、Serverless Component 运行,调试与发布 5、开发一个 Serverless Express 组件实战
一、Serverless Component 运行机制
首先介绍一下什么是Serverless Component,下图是简单拿了一个全栈的应用,比方说我们全栈应用分为两部分,第一部分是前台,静态资源的展示,包括托管,我们简单称为DashBoard。
那么第二部分就是Web API的一个组件,我们认识的API其实也是封装了一个express,express下面又有几个组件,Cloud Function和API Gateway,还有COS静态存储等一系列的东西。
第二部分看一下Component的本质到底是什么,这可能对大部分人来说是一个新名词。Serverless Component其实就是一个NPM包,对它的引用也特别简单,我们只要在yml文件里引入我们的一些NPM包的名称,Serverless框架就会帮助我们把NPM install到我们所需要的本地一个路径。
二、一个完整的Component 开发步骤
看一下整体的一个component,它的开发步骤。总共有4步:
三、Serverless Component 组件开发
那么我们来完整看一下组件开发的一些主体。这些参数的话,可以直接去拉我们的仓库,默认仓库里面有一些英文的注释,大家也可以去看。
首先有一个标准的Component的主体是由三部分的函数组成的,但Component是没有CI复杂,因为CI有部署创建回滚, Component只有两个操作, 第一个是部署。第二个是remove,删除移除的一些逻辑。就是Component的一个主体的需求。
目前的话,我们Component的组件,其实就是分为这三个函数,我们其实只要把逻辑写到这三个函数里面,那么就已经完成了我们80%甚至90%的工作,这块的话,我们一定要引入一个Serverless的核心代码,我们的一个包,然后这块的话是一定要注意这个包,假如你要调试的话也必须得安装到本地。
四、Serverless Component 运行,调试与发布
调试,其实我之前也是有踩到一个坑,因为我第一次使用的时候我是把整个GitHub克隆到本地的,所以这块的话,其实他就是引用我们本地的一个东西,所以在我们组件进行运行、调试发布的时候,我们就可以去直接定位到跟路径,然后去做这个事情。
这里的逻辑,我希望大家可以明白就是我们依然也可以去调用本地来做一些调试发布的工作。那这就是我们后面整体的一个Component组件的运行原理,包括一些调试方法,还有整体的发布步骤。
其次就是有哪些人在用我们Serverless的架构,那么下图也列举出了一些我们的客户,他们都是在用我们的Serverless架构的形态。
那么最关键的问题就是你现在有什么。我们其实现有的一些Component的支持还是比较多的,比方说Express、website、django等。
五、开发一个 Serverless Express 组件
1. 安装
$ npm install -g serverless
$ touch serverless.yml
npm init # 创建后持续回车
npm i --save express # 安装express
app.js
文件,并在其中创建您的 Express App:const express = require('express')
const app = express()
app.get('/', function(req, res) {
res.send('Hello Express')
})
// don't forget to export!
module.exports = app
# serverless.yml
express:
component: '@serverless/tencent-express'
inputs:
region: ap-shanghai
sls
命令进行部署,并可以添加--debug
参数查看部署过程中的信息$ sls --debug
DEBUG ─ Resolving the template's static variables.
DEBUG ─ Collecting components from the template.
DEBUG ─ Downloading any NPM components found in the template.
DEBUG ─ Analyzing the template's components dependencies.
DEBUG ─ Creating the template's components graph.
DEBUG ─ Syncing template state.
DEBUG ─ Executing the template's components graph.
DEBUG ─ Compressing function ExpressComponent_7xRrrd file to /Users/dfounderliu/Desktop/temp/code/.serverless/ExpressComponent_7xRrrd.zip.
DEBUG ─ Compressed function ExpressComponent_7xRrrd file successful
DEBUG ─ Uploading service package to cos[sls-cloudfunction-ap-shanghai-code]. sls-cloudfunction-default-ExpressComponent_7xRrrd-1572512568.zip
DEBUG ─ Uploaded package successful /Users/dfounderliu/Desktop/temp/code/.serverless/ExpressComponent_7xRrrd.zip
DEBUG ─ Creating function ExpressComponent_7xRrrd
DEBUG ─ Created function ExpressComponent_7xRrrd successful
DEBUG ─ Starting API-Gateway deployment with name express.TencentApiGateway in the ap-shanghai region
DEBUG ─ Using last time deploy service id service-n0vs2ohb
DEBUG ─ Updating service with serviceId service-n0vs2ohb.
DEBUG ─ Endpoint ANY / already exists with id api-9z60urs4.
DEBUG ─ Updating api with api id api-9z60urs4.
DEBUG ─ Service with id api-9z60urs4 updated.
DEBUG ─ Deploying service with id service-n0vs2ohb.
DEBUG ─ Deployment successful for the api named express.TencentApiGateway in the ap-shanghai region.
express:
region: ap-shanghai
functionName: ExpressComponent_7xRrrd
apiGatewayServiceId: service-n0vs2ohb
url: http://service-n0vs2ohb-1300415943.ap-shanghai.apigateway.myqcloud.com/release/
36s › express › done
部署完毕后,可以在浏览器中访问返回的链接,看到对应的express返回值。
实践相关源码:
https://docs.qq.com/doc/DVURnWFp3SFNGd3N
Serverless Framework 免费试用计划
Serverless Framework 免费试用名额已开放,我们诚邀您来试用和体验最便捷的 Serverless 开发和部署方式。包括服务中使用到云函数 SCF、API 网关、对象存储 COS 等产品,均在试用期内提供免费资源,并伴有专业的技术支持,帮助您的业务快速、便捷实现 Serverless !
Serverless Framework 落地 Serverless 架构的全云端开发闭环体验,覆盖编码、运维、调试、部署等开发全生命周期。使用 Serverless Framework 即可在几秒钟内将业务部署至云端。
详情可查阅:https://cloud.tencent.com/document/product/1154/38792
本文分享自 ServerlessCloudNative 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有