Easily build Eggless web application based on Serverless

gabriel-garcia-marengo-ht9aLtovtSo-unsplash.jpg

First introduce some of the more important concepts that appear in this article:

Function calculation (Function Compute): Function calculation is an event-driven service. Through function calculation, users do not need to manage the operation of the server and other operations, just write code and upload. Function calculation prepares computing resources and runs user code in a flexible manner, and users only need to pay based on the resources consumed by the actual code operation. More information function calculation reference .
Fun: Fun is a tool for supporting serverless application deployment, which can help you conveniently manage function computing, API gateway, log service and other resources. It assists you in development, build, and deployment operations through a resource configuration file (template.yml). More Fun document reference .

Note: The technique described in this article requires Fun version greater than or equal to 3.6.9.

What is Egg.js?

The official description of Egg.js is:

Egg.js is born for enterprise-level frameworks and applications. We hope that Egg.js will spawn more upper-level frameworks to help development teams and developers reduce development and maintenance costs.
Egg pursues "convention is better than configuration", and conducts application development according to a unified set of conventions. This method can reduce the learning cost of developers within the team. Developers are no longer "nails" and can flow.
Egg's plugin mechanism is highly extensible, and a plugin only does one thing. Egg aggregates these plugins through the framework and customizes the configuration according to their business scenarios, so that the development cost of the application becomes very low.

Egg features:

  • Provide the ability to customize the upper framework based on Egg
  • Highly extensible plugin mechanism
  • Built-in multi-process management
  • Based on Koa development, excellent performance
  • Stable framework and high test coverage
  • Progressive development

Sample effect preview

After the deployment of the application examples involved in this article, click the preview effect:

Egg.js application example: http://mtime.functioncompute.com/hot

Environmental preparation

First install Fun to the machine according to the method described in Fun's installation documentation .

PS: The method described in this article does not require the installation of Docker, just install Fun. The easiest way is to directly download the executable binary file.

After the installation is complete, you can execute fun --version to check whether Fun is successfully installed.

First example: Quickly initialize and deploy an Egg.js sample application

Follow the steps described in the official quick start document in order as follows.

Initialize an egg.js example:

mkdir egg-example && cd egg-example
npm init egg --type=simple
npm i

Run the sample locally to test:

npm run dev

Then you can visit http://127.0.0.1:7001 in the browser to view the effect.

After the local development is completed, you can directly use the following command for one-click deployment:

fun deploy -y 

Second example: Quickly migrate an existing Egg.js application

Here we take an open source Egg.js web application as an example: https://github.com/OrangeXC/mtime

First we need to clone the application:

git clone https://github.com/OrangeXC/mtime

Go to the mtime directory and use npm to install dependencies:

npm install

Start the application locally:

npm run dev

Local start an application, use config/config.default.jsthis configuration, you need to configure the configuration of mysql username, password and other properties to the correct values before the application starts up.

After the startup is complete, open the URL http://127.0.0.1:7001 through the browser to preview the effect.

When the local test is completed, we have to consider how to deploy it online. Deployed to the line, Egg.js default precedence config/config.prod.jsconfigured, we can configure online database to this file, so that you can achieve the purpose of local development and online deployment using a different database.

Of course, you can also verify at the local configuration is correct, direct use npm run startcan be applied to produce a run up, use npm run stopcan be applied to stop.

Finally, we also need to modify under Egg.js cache and log directory, we add the following to config/config.prod.jsthe:

  config.rundir = '/tmp/run',
  config.logger = {
    dir: '/tmp/log',
  }

The above configuration means that the Egg.js cache and log are written to the writable directory of the function calculation (log output to the console is also possible).

Finally, we use Fun one-click deployment:

fun deploy -y

After the deployment is complete, you can see the temporary domain name 13500180-1986114430573743.test.functioncompute.com in the terminal output . Open the temporary domain name through the browser to see the same effect as the local runtime.

to sum up

This article mainly introduces how to deploy Egg.js application to function calculation. Compared with the traditional deployment method, not only is it not more complicated, but it also omits the steps of buying machines, installing and configuring Node, Npm. It can be achieved. After the traditional Egg.js application is developed locally, it can be deployed to the remote site for direct production with one click, and has the characteristics of elastic scaling, pay-as-you-go, and free operation and maintenance.

If you encounter problems during the migration of your application, please join our dingding group 11721331 for feedback.

" Alibaba Cloud Native focuses on microservices, serverless, containers, Service Mesh and other technical fields, focusing on cloud native popular technology trends, cloud native large-scale landing practices, and being the technology circle that understands cloud native developers best.

Guess you like

Origin www.cnblogs.com/alisystemsoftware/p/12712331.html