ServerlessComponent:全局变量组件和单独部署方法

数据结
• 阅读 1512

前言

说句发自肺腑的话,Serverless Framework的Components真的好用,原先使用SCFCLI和VSCode部署SCF函数的时候很方便,但是他也只能部署云函数,我如果有静态资源,如果想配置CDN,如果想绑定域名,如果......可能都离不开控制台,但是Serverless Framework的Components真的是可以让我暂时告别控制台了。对这样的一个工具,我也是充满崇敬。
在使用Components做了几个比较有趣的,稍微大一点的项目,我发现了两个不是问题的问题,却也让人抓狂的事情。

  1. Component没有全局变量;
  2. 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/S... 这里面一共有十几个函数,如果没有全局变量的话,那可能真的是噩梦。例如说,我有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 Framework团队会做这个功能,但是短期内他们还不支持的时候,可以先用这个方案作为替代,也很期待Serverless团队早日支持。

单独部署组件

这几天,我在做一个Serverless Blog,里面有多个模块,包括十几个函数、API网关以及Website等,初次部署真的爽歪歪+美滋滋:一键部署就是爽!
但是,当我对其中某个函数进行修改之后,我仅仅修改了一个配置信息,我再执行sls --debug的时候,他竟然又为我重新部署了一次!部署一次要十分钟左右,我修改一行代码,修改一个配置就要等十分钟?虽然说不是致命问题,但是确实让人抓狂:为什么Component没有指定部署某个资源的功能?
我就在幻想着:如果我可通过某个参数,来控制我要部署那个资源,该有多好?例如:我用:
sls --debug -n website
就可以简单轻松的只部署website,而不是全部资源都重新部署一次,那么我想这应该是超级方便的一件事情。所以我思前想后,通过简单的几行代码,实现了一套非常简单的Component:

ServerlessComponent:全局变量组件和单独部署方法

是的,没有弄错,我就是在官方的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

ServerlessComponent:全局变量组件和单独部署方法

点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
4年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Jacquelyn38 Jacquelyn38
4年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Peter20 Peter20
4年前
mysql中like用法
like的通配符有两种%(百分号):代表零个、一个或者多个字符。\(下划线):代表一个数字或者字符。1\.name以"李"开头wherenamelike'李%'2\.name中包含"云",“云”可以在任何位置wherenamelike'%云%'3\.第二个和第三个字符是0的值wheresalarylike'\00%'4\
Wesley13 Wesley13
4年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Stella981 Stella981
4年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Easter79 Easter79
4年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
4年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
4年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n