前言
實事求是地說,Serverless Framework 的 Components 真的好用,原先使用 SCF CLI 和 VSCode 插件部署騰訊云函式盡管也方便,但也只能部署云函式,
假如我有靜態資源,想配置 CDN,想系結域名,或者其他更多的操作......可能都離不開控制臺,但是 Serverless Framework 的 Components 幾乎可以讓我暫時告別控制臺,對這樣的一個工具,我真的 respect!
然而就在我嘗試使用 Components 做稍微大一點的專案,遇到了兩個不算問題的問題,但也著實讓人抓狂,
- Component 沒有全域變數;
- Component 不能單獨部署;
再進行下文閱讀前,可以先了解這些背景知識:
- Serverless Component 是什么,我怎樣使用它?
- 如何開發自己的第一個 Serverless Component
全域變陣列件
如果只有一個組件需要部署,例如下面這個 Yaml,那么全域變數存在的意義的確不大,
hello_world:
component: "@serverless/tencent-website"
inputs:
code:
src: ./public
index: index.html
error: index.html
region: ap-shanghai
bucketName: hello_world
但是實際生產中,一個 Yaml 中會寫很多的部分,
例如我的 Blog 的 Yaml:https://github.com/anycodes/ServerlessBlog/blob/master/serverless.yaml,這里面共有十幾個函式,如果沒有全域變數的話,那可能真的是噩夢,
比方有 10 個函式,這些函式都要部署在 ap-guangzhou,部署完成之后,我又要把它們部署到 ap-shanghai 區,如果沒有全域變數,就要修改十幾個函式的配置,即使批量替換修改,也可能出現問題,所以,此時若有全域變數的組件,就顯得尤為重要,
為此,我貢獻了這樣一個組件:serverless-global,通過這個組件,我們可以設定一些全域變數,在程式中使用:
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}
通過 serverless-global,我們可以定義一些全域的公共引數,并且通過變數的方法參考這些引數,例如 ${Conf.region} 就是參考 Conf-inputs 中定義的 region 變數,期待 Serverless 團隊在未來能支持全域變數的功能,
單獨部署組件
還是 Serverless Blog 這個例子,里面有多個模塊,包括十幾個函式、API 網關以及 Website 等,初次部署真的爽歪歪+美滋滋:一鍵部署就是爽!
但是,當我修改其中的某個函式,僅僅修改了一個配置資訊,我再執行 sls --debug 部署的時候,它竟然又為我重新部署了一次!部署一次約 10min,可我僅僅修改了一行代碼,雖說不是什么大問題,但體驗也不如人意:為什么 Component 沒有指定部署某個資源的功能?
我猜想:如果可通過某個引數,來控制我要部署那個資源,該有多好?
例如:我用命令 sls --debug -n website 可以只部署 website,而不是全部資源再跑一次部署,那多方便啊!于是我思前想后,通過簡單的幾行代碼,實作了一套非常簡單的 Component:

是的,我就是在官方 Component 上層,嵌套了一個 tempComponent,使用方法很簡單,例如,有這么一個 website 的部分:
test1:
component: "@serverless/tencent-website"
inputs:
code:
src: ./public
index: index.html
error: index.html
region: ap-shanghai
bucketName: test1
把里面 component 的名字改一下,改成@gosls:
test1:
component: "@gosls/tencent-website"
inputs:
code:
src: ./public
index: index.html
error: index.html
region: ap-shanghai
bucketName: test1
這樣就變成了支持部署單個組件的 component 了,并且所有騰訊云的組件都可以通過修改前面的前綴進行變化,如果不想用了,可以隨時修改回 @serverless,下面的 inputs 的內容和格式,和官方的一模一樣,直接轉發給對應的 @serverless/tencent-website,例如:
# 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
執行 sls --debug:
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
可以看到完成了三個的部署,當我使用引數,執行部署 test2 的時候:
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
可以看到,通過 -n 引數,只部署了 test2,其他的組件沒有發生任何變化,
目前這個功能支持絕大部分 Tencent 官方提供的組件(https://github.com/gosls ):
@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
快來開始動手吧~
傳送門:
- GitHub: github.com/serverless
- 官網:serverless.com
歡迎訪問:Serverless 中文網,您可以在 最佳實踐 里體驗更多關于 Serverless 應用的開發!
推薦閱讀:《Serverless 架構:從原理、設計到專案實戰》
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/33956.html
標籤:其他
上一篇:5. Docker Compose講解+網站部署實戰
下一篇:新手求cocos怎么下載
