實事求是地說,Serverless Framework 的 Components 真的好用,原先使用 SCF CLI 和 VSCode 插件部署騰訊雲函數儘管也方便,但也只能部署雲函數。html
假如我有靜態資源,想配置 CDN,想綁定域名,或者其餘更多的操做......可能都離不開控制檯。可是 Serverless Framework 的 Components 幾乎可讓我暫時告別控制檯。對這樣的一個工具,我真的 respect!mysql
然而就在我嘗試使用 Components 作稍微大一點的項目,遇到了兩個不算問題的問題,但也着實讓人抓狂。laravel
再進行下文閱讀前,能夠先了解這些背景知識:git
若是隻有一個組件須要部署,例以下面這個 Yaml,那麼全局變量存在的意義的確不大。github
hello_world: component: "@serverless/tencent-website" inputs: code: src: ./public index: index.html error: index.html region: ap-shanghai bucketName: hello_world
可是實際生產中,一個 Yaml 中會寫不少的部分。web
例如個人 Blog 的 Yaml:https://github.com/anycodes/ServerlessBlog/blob/master/serverless.yaml。這裏面共有十幾個函數,若是沒有全局變量的話,那可能真的是噩夢。sql
比方有 10 個函數,這些函數都要部署在 ap-guangzhou。部署完成以後,我又要把它們部署到 ap-shanghai 區,若是沒有全局變量,就要修改十幾個函數的配置。即便批量替換修改,也可能出現問題。因此,此時如有全局變量的組件,就顯得尤其重要。express
爲此,我貢獻了這樣一個組件:serverless-global。經過這個組件,咱們能夠設置一些全局變量,在程序中使用:flask
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 團隊在將來能支持全局變量的功能。api
仍是 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 應用的開發!