共計 4600 個字符,預計需要花費 12 分鐘才能閱讀完成。
如何解析 Serverless 組件開發中的全局變量組件和單獨部署組件,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
前言
實事求是地說,Serverless Framework 的 Components 真的好用,原先使用 SCF CLI 和 VSCode 插件部署騰訊云函數盡管也方便,但也只能部署云函數。
假如我有靜態資源,想配置 CDN,想綁定域名,或者其他更多的操作 …… 可能都離不開控制臺。但是 Serverless Framework 的 Components 幾乎可以讓我暫時告別控制臺。對這樣的一個工具,我真的 respect!
然而就在我嘗試使用 Components 做稍微大一點的項目,遇到了兩個不算問題的問題,但也著實讓人抓狂。
Component 沒有全局變量;
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.ymltest1: component: @gosls/tencent-website inputs: code: src: ./public index: index.html error: index.html region: ap-shanghai bucketName: test1
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
關于如何解析 Serverless 組件開發中的全局變量組件和單獨部署組件問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注丸趣 TV 行業資訊頻道了解更多相關知識。