02.28 「实践与踩坑」我为Serverless Framework增加了这样的Component

说句发自肺腑的话,Serverless Framework的Components真的好用,原先使用SCFCLI和VSCode部署SCF函数的时候很方便,但是他也只能部署云函数,我如果有静态资源,如果想配置CDN,如果想绑定域名,如果......可能都离不开控制台,但是Serverless Framework的Components真的是可以让我暂时告别控制台了。对这样的一个工具,我也是充满崇敬。

在使用Components做了几个比较有趣的,稍微大一点的项目,我发现了两个不是问题的问题,却也让人抓狂的事情。

1: Component没有全局变量;

2: Component不能单独部署;


全局变量组件

首先说没有全局变量这件事情,如果说我们只有一个组件需要部署,例如,我的Yaml这样:

<code>hello_world:  component: "@serverless/tencent-website"  inputs:    code:      src: ./public      index: index.html      error: index.html    region: ap-shanghai    bucketName: hello_world/<code>

那么我觉得,全局变量存在的意义不大,但是实际生产中,我们一个Yaml中会写很多很多的部分,例如我的Blog的Yaml:https://github.com/anycodes/ServerlessBlog/blob/master/serverless.yaml,这里面一共有十几个函数,如果没有全局变量的话,那可能真的是噩梦。例如说,我有10个函数,这些函数都要部署在ap-guangzhou,部署完成之后,我又要把他们部署到ap-shanghai区,如果没有全局变量,我岂不是要修改十几个函数的配置,就算是批量替换修改,也可能出现问题。所以,此时此刻,有一个全局变量的组件,就显得尤为重要,为此,我贡献了这样一个组件:serverless-global

通过这个组件,我们可以设置一些全局变量,在程序中使用:

<code>Conf:  component: "serverless-global"  inputs:    region: ap-shanghai    mysql_host: gz-cdb-mytest.sql.tencentcdb.com    mysql_user: mytest    mysql_password: mytest    mysql_port: 62580    mysql_db: mytest​Album_Login:  component: "@serverless/tencent-scf"  inputs:    name: Album_Login    codeUri: ./album/login    handler: index.main_handler    runtime: Python3.6    region: ${Conf.region}    environment:      variables:        mysql_host: ${Conf.mysql_host}        mysql_port: ${Conf.mysql_port}        mysql_user: ${Conf.mysql_user}        mysql_password: ${Conf.mysql_password}        mysql_db: ${Conf.mysql_db}/<code>

通过serverless-global,我们可以定义一些全局的公共的参数,并且在下面通过变量的方法引用这些参数,例如${Conf.region}就是饮用Conf-inputs中定义的region变量。

这个公共组件,我相信在不久的将来Serverless Framework团队会做这个功能,但是短期内他们还不支持的时候,可以先用这个方案作为替代,也很期待Serverless团队早日支持。


单独部署组件

这几天,我在做一个Serverless Blog,里面有多个模块,包括十几个函数、API网关以及Website等,初次部署真的爽歪歪+美滋滋:一键部署就是爽!

但是,当我对其中某个函数进行修改之后,我仅仅修改了一个配置信息,我再执行sls --debug的时候,他竟然又为我重新部署了一次!部署一次要十分钟左右,我修改一行代码,修改一个配置就要等十分钟?虽然说不是致命问题,但是确实让人抓狂:为什么Component没有指定部署某个资源的功能?

我就在幻想着:如果我可通过某个参数,来控制我要部署那个资源,该有多好?例如:我用:

sls --debug -n website

就可以简单轻松的只部署website,而不是全部资源都重新部署一次,那么我想这应该是超级方便的一件事情。所以我思前想后,通过简单的几行代码,实现了一套非常简单的Component:

「实践与踩坑」我为Serverless Framework增加了这样的Component

是的,没有弄错,我就是在官方的Component上层,嵌套了一个tempComponent,使用方法很简单,例如,我有一个website的部分:

<code>test1:  component: "@serverless/tencent-website"  inputs:    code:      src: ./public      index: index.html      error: index.html    region: ap-shanghai    bucketName: test1/<code>

只需要把这里面的component的名字改一下,改成@gosls:

<code>test1:  component: "@gosls/tencent-website"  inputs:    code:      src: ./public      index: index.html      error: index.html    region: ap-shanghai    bucketName: test1/<code>

这样就非常简单轻松加愉快的,变成了支持部署单个组件的component了,并且所有的腾讯云的组件都可以通过修改前面的前缀进行变化,如果不想用了,可以随时修改会@serverless,下面的inputs的内容和格式,和官方的一模一样,直接转发给对应的@serverless/tencent-website.

举一个例子:

<code># serverless.yml​test1:  component: "@gosls/tencent-website"  inputs:    code:      src: ./public      index: index.html      error: index.html    region: ap-shanghai    bucketName: test1​​test2:  component: "@gosls/tencent-website"  inputs:    code:      src: ./public      index: index.html      error: index.html    region: ap-shanghai    bucketName: test2​​test3:  component: "@gosls/tencent-website"  inputs:    code:      src: ./public      index: index.html      error: index.html    region: ap-shanghai    bucketName: test3/<code>

我执行sls --debug:

<code>DFOUNDERLIU-MB0:website_test dfounderliu$ 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 ─ Website deployed successfully to URL: http://test2-1256773370.cos-website.ap-shanghai.myqcloud.com.  DEBUG ─ Website deployed successfully to URL: http://test3-1256773370.cos-website.ap-shanghai.myqcloud.com.​  test1:     url: http://test1-1256773370.cos-website.ap-shanghai.myqcloud.com    env:   test2:     url: http://test2-1256773370.cos-website.ap-shanghai.myqcloud.com    env:   test3:     url: http://test3-1256773370.cos-website.ap-shanghai.myqcloud.com    env: ​  19s › test1 › done/<code>

可以看到他完成了三个的部署,当我使用参数,执行部署test2的时候:

<code>DFOUNDERLIU-MB0:website_test dfounderliu$ sls --debug -n test2​  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 ─ Uploading directory /Users/dfounderliu/Desktop/ServerlessComponents/test/website_test/public to bucket test2-1256773370  DEBUG ─ Website deployed successfully to URL: http://test2-1256773370.cos-website.ap-shanghai.myqcloud.com.​  test1:   test2:     url: http://test2-1256773370.cos-website.ap-shanghai.myqcloud.com    env:   test3: ​  6s › test3 › done/<code>

可以看到,通过-n参数,只部署了test2,其他的组件没有发生任何变化。

目前这个功能支持绝大部分Tencent官方提供的组件(https://github.com/gosls):

<code>@serverless/tencent-apigateway@serverless/tencent-cam-policy@serverless/tencent-cam-role@serverless/tencent-cdn@serverless/tencent-cos@serverless/tencent-egg@serverless/tencent-express@serverless/tencent-flask@serverless/tencent-koa@serverless/tencent-laravel@serverless/tencent-scf@serverless/tencent-website/<code>




Serverless实践列表:

  • ServerlessBlogSystem:https://github.com/anycodes/ServerlessBlog该系统是通过Serverless的原生开发,和Flask框架部署到Serverless结合,做了前台页面和后台管理页面,用户可以通过简单的配置数据库信息,网站标题,描述等信息,快速部署一个自己的博客在Serverless架构上。都2020年了,你的博客在Serverless上,才是最炫酷的!
  • 基于人工智能的相册小程序:https://github.com/anycodes/AI_Album由于我个人比较喜欢拍照,又经常找不到之前拍过的照片,我就在想能不能通过Serverless技术,搭建一个基于人工智能的小测小程序,用户登录之后可以上传照片,上传完成之后,系统会根据照片内容,自动生成关键词和照片的描述,然后用户就可以通过文字来搜索到照片,这多么炫酷和方便哇。


分享到:


相關文章: