ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Serverless 组件开发尝试:全局变量组件和单独安顿组件

2020-03-25 20:02:06  阅读:271  来源: 互联网

标签:Serverless website serverless 安顿 index ap tencent 组件 全局变量


 

 

前语

兢兢业业地说,Serverless Framework 的 Components 真的好用,原先运用 SCF CLI 和 VSCode 插件安顿腾讯云函数虽然也便当,但也只能安顿云函数。

假定我有静态资源,想配备 CDN,想绑定域名,或许其他更多的操作......或许都离不开操控台。但是 Serverless Framework 的 Components 几乎能够让我暂时辞别操控台。对这样的一个工具,我真的 respect!

但是就在我尝试运用 Components 做稍微大一点的项目,遇到了两个不算问题的问题,但也着实让人抓狂。

  1. Component 没有全局变量;
  2. 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 官方供给的组件:

@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,website,serverless,安顿,index,ap,tencent,组件,全局变量
来源: https://www.cnblogs.com/1654kjl/p/12569010.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有