Mercer-Lee的空间

vuePress-theme-reco Mercer-Lee的空间    2018 - 2024
Mercer-Lee的空间 Mercer-Lee的空间

Choose mode

  • dark
  • auto
  • light
TimeLine
分类
  • 数据结构和算法
  • 后端
  • 运维
  • 前端
  • 工具
  • 语言
标签
我的GitHub (opens new window)
author-avatar

Mercer-Lee的空间

27

文章

29

标签

TimeLine
分类
  • 数据结构和算法
  • 后端
  • 运维
  • 前端
  • 工具
  • 语言
标签
我的GitHub (opens new window)
  • 一次舒服的Serverless之旅

    • Serverless 介绍
      • Serverless 部署和发布
        • 结语

        一次舒服的Serverless之旅

        vuePress-theme-reco Mercer-Lee的空间    2018 - 2024

        一次舒服的Serverless之旅


        Mercer-Lee的空间 2020-03-23 Egg JS Node

        最近自己在业余搞一个项目,由于用户量初始不会很大,而且由于项目的自身原因,后端的接口请求不会很频繁,但是又不想去买一个小水管带宽的服务器委屈自己,最后还是想到了最近有点火的 Serverless,抱着试试的心态去阿里云和腾讯云看了下。发现非常有意思,腾讯云和一家专门搞 Serverless 的公司合作,所以像 koa 和 egg 这样的框架,腾讯云也支持配置上线。

        # Serverless 介绍

        说到这个 serverless,我还是稍微介绍下吧,在维基百科上给出的解释就是无服务计算,也叫功能及服务,大白话就是不需要去关心运维的那些东西,你只要写好业务型代码,直接提交就能有一个服务,不需要去关心机器运维那些。这对于那些初创和对运维不熟悉的创业者以及开发人员是极其友好的。Serverless是Faas(函数运行平台)和BaaS(后端服务组件)组成,它是由事件驱动触发的,讲到这里,函数、事件驱动触发这些东西一出来,没错,node太适合serverless了,无论是node的事件循环机制还是js自带的函数即亲儿子的特性,都让他对serverless有一种天然的般配。额。。。不吹了,后面讲下国内比较大的serverless平台,因为我使用的是腾讯云,所以我下面也是主要讲解腾讯云的 serverless,即云函数。这里给出腾讯云的云函数文档 (opens new window),里面讲的非常清楚,价格也很清晰。我觉得云函数对于一个初始项目来说是很合适的,毕竟 99%的项目一开始都是小项目,大部分的互联网项目一开始都是 MVP(最小化可行产品),serverless 的出现我觉得是非常符合的。接下来我就要讲一下 serverless 的部署和应用。

        # Serverless 部署和发布

        因为我这边是用的 egg,所以就拿 egg 来做例子,其实其他框架也是差不多的,包括 Python 或者其他语言的框架。大家也可以在GitHub仓库上看腾讯给出的文档 (opens new window),首先我们在自己的 egg 项目安装 serverless:

        npm install -g serverless
        

        然后在根目录下增加一个 serverless.yml 文件,然后在这个文件下添加一下内容:

        MyComponent:
          component: "@serverless/tencent-egg"
          inputs:
            region: ap-guangzhou
            functionName: projectName
            runtime: Nodejs8.9
            code: ./
            functionConf:
              timeout: 10
              memorySize: 128
              environment:
                variables:
                  TEST: vale
              vpcConfig:
                subnetId: ""
                vpcId: ""
            apigatewayConf:
              protocols:
                - https
              environment: release
        

        然后我们在 egg 的 config 文件中增加配置,一般我们的 config 是有区分本地和 prod 环境的,所以我们直接在在 config.prod.js 修改:

        // 默认配置和云函数配置
        module.exports = () => {
          const config = (exports = {
            env: "prod", // 推荐云函数的 egg 运行环境变量修改为 prod
            rundir: "/tmp",
            logger: {
              dir: "/tmp",
            },
          });
          return {
            ...config,
          };
        };
        

        然后我们写好了之后直接在终端:

        sls --debug
        

        就可以了,一般第一次会让你扫码确认,当然你可以把你的腾讯云账号的SecretId和SecretKey保存到项目的.env文件。上传成功之后的终端一般是这样的:

        $ 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 ─ Creating the template's components graph.
          DEBUG ─ Syncing template state.
          DEBUG ─ Executing the template's components graph.
          DEBUG ─ Compressing function egg-function file to /Users/tina/Desktop/live/egg-proj/.serverless/egg-function.zip.
          DEBUG ─ Compressed function egg-function file successful
          DEBUG ─ Uploading service package to cos[sls-cloudfunction-ap-beijing-code]. sls-cloudfunction-default-egg-function-1581335565.zip
          DEBUG ─ Uploaded package successful /Users/tina/Desktop/live/egg-proj/.serverless/egg-function.zip
          DEBUG ─ Creating function egg-function
          DEBUG ─ Updating code... 
          DEBUG ─ Updating configure... 
          DEBUG ─ Created function egg-function successful
          DEBUG ─ Setting tags for function egg-function
          DEBUG ─ Creating trigger for function egg-function
          DEBUG ─ Deployed function egg-function successful
          DEBUG ─ Starting API-Gateway deployment with name MyComponent.TencentApiGateway in the ap-beijing region
          DEBUG ─ Service with ID service-n5m5e8x3 created.
          DEBUG ─ API with id api-cmkhknda created.
          DEBUG ─ Deploying service with id service-n5m5e8x3.
          DEBUG ─ Deployment successful for the api named MyComponent.TencentApiGateway in the ap-beijing region.
        
          MyComponent: 
            region:              ap-beijing
            functionName:        egg-function
            apiGatewayServiceId: service-n5m5e8x3
            url:                 https://service-n5m5e8x3-1251971143.bj.apigw.tencentcs.com/release/  这个就是腾讯给的你的项目API地址
        
          32s › MyComponent › done
        

        上面可以看到腾讯云会给你一个url,这个就是腾讯给的你的项目API地址,可以直接拿来用,我们在他的腾讯云函数控制台 (opens new window)可以看到详细的函数情况。

        控制台

        # 结语

        这次的serverless之旅就这样结束了,总的来说是非常舒服,文档齐全,安装部署简单,关键是完全匹配到了我的需求,我觉得serverless非常适合于初创项目和一些想要快速验证市场和想法的项目,低成本快速试错将是未来互联网开发的主旋律。node本身可以同时用于前端和后端的开发加上serverless更是让一些对服务器和运维不熟悉的开发人员或者创业者以及有想法的人更快速地去验证市场和想法。