本文介绍如何将Egg.js应用部署到函数计算。与传统的部署方法相比,您可以跳过购买机器等步骤,将传统的Egg.js应用一键部署至远端直接用于生产,并且拥有弹性伸缩、按量付费和免运维等特性。

前提条件

您已完成以下操作:
注意

本文介绍的如何使用Funcraft迁移传统框架的相关内容,后期将不再维护,建议您使用Serverless Devs迁移相关框架到函数计算。关于如何将函数计算的相关资源从Funcraft迁移到Serverless Devs进行管理的详细操作,请参见从Funcraft迁移到Serverless Devs

关于如何使用Serverless Devs迁移Web框架的详细操作,请参见使用Serverless Devs迁移Web框架

由此带来的不便,敬请谅解!

背景信息

Egg.js为企业级框架和应用而生,为开发人员提供了Web开发的核心功能和一套具有高扩展性的插件机制。开发人员按照统一的约定开发应用,从而降低开发和维护成本。更多Egg的内容,请参见Egg.js

操作步骤

示例一:以部署一个HelloWorld应用为例介绍具体的操作步骤。

  1. 在目标目录中,执行以下命令创建一个Egg.js项目。详细信息,请参见快速初始化
    npm init egg --type=simple
    说明 若已有Egg.js项目则跳过该步骤。
    输出示例:
    Need to install the following packages:
      create-egg
    Ok to proceed? (y) y
    npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
    npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
    npm WARN deprecated socks@1.1.10: If using 2.x branch, please upgrade to at least 2.1.6 to avoid a serious bug with socket data flow and an import issue introduced in 2.1.0
    [egg-init] use registry: https://registry.npm.taobao.org
    [egg-init] target dir is /test/egg-example
    [egg-init] fetching npm info of egg-init-config
    ? Please select a boilerplate type simple - Simple egg app boilerplate
    [egg-init] use boilerplate: simple(egg-boilerplate-simple)
    [egg-init] fetching npm info of egg-boilerplate-simple
    [egg-init] downloading https://registry.npmmirror.com/egg-boilerplate-simple/download/egg-boilerplate-simple-3.3.1.tgz
    [egg-init] extract to /tmp/egg-init-boilerplate
    [egg-init] collecting boilerplate config...
    ? project name egg-example
    ? project description
    ? project author
    ? cookie security keys 1632377928307_4634
    [egg-init] write to /test/egg-example/.autod.conf.js
    [egg-init] write to /test/egg-example/.eslintignore
    [egg-init] write to /test/egg-example/.eslintrc
    [egg-init] write to /test/egg-example/README.md
    [egg-init] write to /test/egg-example/.gitignore
    [egg-init] write to /test/egg-example/package.json
    [egg-init] write to /test/egg-example/app/router.js
    [egg-init] write to /test/egg-example/config/config.default.js
    [egg-init] write to /test/egg-example/config/plugin.js
    [egg-init] write to /test/egg-example/app/controller/home.js
    [egg-init] write to /test/egg-example/test/app/controller/home.test.js
    [egg-init] usage:
          - cd /test/egg-example
          - npm install
          - npm start / npm run dev / npm test
  2. 执行以下命令,安装依赖。
    npm install
    输出示例:
    npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
    npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
    npm WARN deprecated debug@3.2.6: Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (https://github.com/visionmedia/debug/issues/797)
    npm WARN deprecated babel-eslint@8.2.6: babel-eslint is now @babel/eslint-parser. This package will no longer receive updates.
    npm WARN deprecated mkdirp@0.5.4: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
    npm WARN deprecated mkdirp@0.5.1: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
    npm WARN deprecated core-js@2.6.12: core-js@<3.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Please, upgrade your dependencies to the actual version of core-js.
    
    added 1244 packages in 34s
  3. 执行以下命令运行本地项目。
    npm run dev
  4. 执行fun deploy -y命令将项目部署至函数计算。
    fun deploy -y
    返回结果如下。
    using template: template.yml
    using region: cn-qingdao
    using accountId: ***********3743
    using accessKeyId: ***********Ptgk
    using timeout: 60
    
    Collecting your services information, in order to caculate devlopment changes...
    
    Resources Changes(Beta version! Only FC resources changes will be displayed):
    ... ... ...
           trigger httpTrigger deploy success
       function egg-example deploy success
    service egg-example deploy success
    
    Detect 'DomainName:Auto' of custom domain 'Domain'
    Fun will reuse the temporary domain 17090425-19861144305****.test.functioncompute.com, expired at 2020-04-27 10:27:05, limited by 1000 per day.
    
    Waiting for custom domain Domain to be deployed...
    custom domain Domain deploy success

    部署成功后,您可以在日志中看到函数计算为您生成的临时域名,通过这个临时域名您可直接访问刚部署的应用。

    说明 临时域名仅用作演示以及开发,具有时效性。如需用作生产,请绑定已经在阿里云备案的域名。详细信息,请参见绑定自定义域名

示例二:以部署一个开源的Egg.js Web应用为例介绍具体的操作步骤。

应用示例预览效果请参见预览效果

  1. 执行以下命令把示例克隆到本地。
    git clone https://github.com/OrangeXC/mtime
  2. 执行以下命令进入目录,并安装依赖。
    1. 执行以下命令,进入mtime目录。
      cd mtime
    2. 执行以下命令,安装依赖。
      npm install
  3. 在本地打开config/config.default.js文件,将MySQL的usernamepassword配置为正确的值。
    在本地启动应用时,需要使用config/config.default.js文件中的数据库配置。
    说明 在生产环境中启动应用时,Egg.js默认优先使用config/config.prod.js中的数据库配置。因此您需要将生产环境中的数据库信息配置到该文件中。配置完成后,您可以在本地使用npm run startnpm run stop命令以生产的方式启停应用来验证配置是否正确。
  4. 执行以下命令运行本地项目。
    npm run dev
  5. 执行以下命令修改config/config.prod.js文件,获取Egg.js缓存与日志目录的读取权限。
    config.rundir = '/tmp/run',
    config.logger = {
        dir: '/tmp/log',
    }
  6. 执行fun deploy -y命令将项目部署至函数计算。
    fun deploy -y
    返回结果如下。
    using template: template.yml
    using region: cn-qingdao
    using accountId: ***********3743
    using accessKeyId: ***********Ptgk
    using timeout: 60
    
    Collecting your services information, in order to caculate devlopment changes...
    
    Resources Changes(Beta version! Only FC resources changes will be displayed):
    ... ... ...
            trigger httpTrigger deploy success
        function egg-example deploy success
    service egg-example deploy success
    
    Detect 'DomainName:Auto' of custom domain 'Domain'
    Fun will reuse the temporary domain 17090425-19861144305****.test.functioncompute.com, expired at 2020-04-27 10:27:05, limited by 1000 per day.
    
    Waiting for custom domain Domain to be deployed...
    custom domain Domain deploy success

    部署成功后,您可以在日志中看到函数计算为您生成的临时域名,通过这个临时域名您可直接访问刚部署的应用。

    说明 临时域名仅用作演示以及开发,具有时效性。如需用作生产,请绑定已经在阿里云备案的域名。详细信息,请参见绑定自定义域名