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技術,搭建一個基於人工智能的小測小程序,用戶登錄之後可以上傳照片,上傳完成之後,系統會根據照片內容,自動生成關鍵詞和照片的描述,然後用戶就可以通過文字來搜索到照片,這多麼炫酷和方便哇。


分享到:


相關文章: