Author: Huo Daxia in the small courtyard

Hello everyone, I'm Daxia Huo. In this article, we learn to deploy popular web frameworks, such as Spring Boot, Express, and Web IDE, so that you can master the serverless function computing architecture and technology, and appreciate the benefits of elastic concurrency and high availability. Next, I will share step by step from three major chapters : introduction to practice, demonstration of practice, and detailed analysis .

This article is selected from the "Serverless Function Calculation Call for Papers", and the background reply " Function " for details.

foreword

This article will take you to learn:

  • Master the concepts and architecture of Serverless
  • Master the operations and processes of Alibaba Cloud function computing products
  • Deploy popular web frameworks to Alibaba Cloud function computing platform
  • Using the Serverless Devs Client Local Deployment Method
  • Make your own Web IDE application

Learning Matters:

1. Difficulty: Getting Started

2. Prerequisite: Create your own Alibaba Cloud account

3. Duration: 60 minutes

4. Enter Alibaba Cloud Lab:

https://developer.aliyun.com/adc/scenario/fa580ae836224ba6947fc5ec327e4ba1

 title=

 title=

Serverless related concepts & practices

Statement Note: The Alibaba Cloud Function Compute FC used in this experiment, the object storage OSS is a traffic product, and the domain name service is a pre-order product. There will be traffic charges and purchase charges. Please use it carefully according to your own situation. If there is any cost, please bear it by yourself. Of course, it can also be used purely for reading and learning.

Serverless Evolution

 title=

Serverless, what exactly is Function Compute?

Serverless, also known as serverless. Serverless emphasizes an architectural idea and service model, so that developers do not need to care about infrastructure (servers, etc.), but focus on application business logic. Serverless is also the next generation computing engine. For example, Alibaba Cloud Platform Serverless products mainly include Alibaba Cloud Function Compute FC, Serverless Application Engine SAE, and Serverless Workflow. So you can understand that Serverless is an architectural idea.

I think the core of serverless is function computing, which is an event-driven fully managed serverless computing service. You don't need to manage infrastructure such as servers, you just need to write code and upload it, and function computing will prepare computing resources for you, and provide elastic, Run your code in a reliable way.

Usually people say cloud function also refers to serverless.

 title=

At present, the popular application architectures on the Internet are mainly from monolithic architectures, to popular microservices, and now the rapidly developing serverless architectures. You can see some characteristics of the three architectures. The single architecture has high coupling, elasticity, and poor scalability; microservices have improved a lot and are more flexible, but the cost of operation and maintenance learning is also high.

Through the event-driven model, Serverless has the characteristics of elasticity and high availability; including pay-as-you-go; free operation and maintenance are its strengths. I believe that with the continuous development of technology, Serverless must be a very popular technology architecture in the future.

 title=

From this official introduction of Alibaba Cloud, we can see the different forms of Serverless for different scenarios. With the emergence of standard templates and paid customized FaaS templates in FaaS, I believe that they will gradually develop. Maybe it will eventually be the direct AI intelligent generation function through low-code configuration, let's wait and see.

Serverless Function Compute Advantages

 title=

Free operation and maintenance: no need to manage the server host or server process.

Elastic scaling: Automatic scaling and automatic configuration according to the load. The scaling range is zero to infinity.

Pay-As-You-Go: Actual costs are determined based on usage.

High Availability: Implicit high availability.

In addition, cloud functions cannot save state, but this can be improved through the cooperation of other computing services, such as using Redis or local cookies to save user state information.

 title=

One of the core advantages of Serverless is its rich elastic policies and flexible invocation policies. We can see from the figure that it can flexibly configure basic indicators CPU, memory, upper and lower limits of instances, etc.

 title=

At the same time, Alibaba Cloud Function Compute FC can combine most products and services of cloud computing to form a very flexible, low-coupling binding, which can be used as needed. Called by events, http, triggers in various forms. This official flowchart clearly illustrates the entire process of function computing.

 title=

Serverless application scenarios

Serverless architectures can be used in most industries. For example, games, online education, e-commerce, SaaS products, small programs, etc., form strong support through core function computing and other serverless products.

From here we can learn more about the application details of function computation. Interested can save.

 title=

 title=

Now let's talk about a simple Web site case. Many people have doubts, cloud function is just a function calculation, equivalent to an API interface, what should I do if I want to combine database, storage and other functions?

As you can see from this simple structure diagram, cloud functions need to cooperate with various cloud computing services to fully deploy our complex applications. We can speed up the presentation through CDN services and store file resources through OSS.

You can also understand that the cloud function deploys your website code, uses Redis to save the user's session state, and stores important data in Mysql or mongoDB, etc. If you need queue and log services, we can quickly access them through the products provided by the platform, which can meet most of the commonly used applications. Let's learn step by step.

 title=

Through the practical introduction, we have learned what serverless is, its advantages and application scenarios.

The serverless technology architecture is still developing rapidly, and various cloud vendors are developing their own serverless products.

If there is any inappropriate place in the above content, please leave me a comment and let's exchange and learn together.

Next, let's learn how to deploy popular web architectures on Serverless.

Deploy the web framework to Function Compute

In this part, we also learn from three aspects, first learn the product functions of function computing; then deploy the Web framework Spring Boot; and finally practice the case of deploying the Nodejs framework Express.

Functions of Alibaba Cloud Function Computing Products

Without further ado, let’s take a look at the functions of Alibaba Cloud Function Computing.

Statement Note: You need to log in to your Alibaba Cloud account to start the Function Compute service. The Function Compute service is a traffic fee and may incur traffic charges. Please use with caution. If you incur expenses, please bear by yourself, or you can simply read and learn.

  1. Log in to the Alibaba Cloud website, click Login at the top, and enter your username and password.

 title=

  1. Then click the product in the upper left corner, find the Serverless category in the product service, click the function computing PC below, and then you can see the main panel of the function computing. (If the service is not activated, you can directly agree to activate it).

 title=

  1. Let's learn and experience the product functions of Function Compute step by step. The overview shows the call data and runtime environment of the overall function computation.

 title=

Let's look at the second application in the menu. I think this is a good function, which greatly facilitates the user's threshold. We can use this function to quickly create our own sites and applications.

 title=

The application includes 2 ways of creating from template and importing from warehouse. Later, I will mainly demonstrate the case of template creation.

 title=

The warehouse mode can also be pulled and deployed from common warehouses such as your Github/Gitee, but I won't go into details here.

 title=

What is a service? A service is a unit of Function Compute resource management. Before creating a function, you must create a service. All functions under the same service share some of the same settings, such as service authorization and log configuration. From a business scenario, an application can be split into multiple services. From the dimension of resource usage, a service can be composed of multiple functions. For example, a data processing service is divided into two parts: data preparation and data processing. The resource requirements of the data preparation function are small, so you can choose a small-scale instance. Data processing functions require large resources, so large-scale instances can be selected.

 title=

The resource scheduling and running of Function Compute are based on functions. Functions must belong to services. Multiple functions can be created under the same service. These functions share some of the same settings, such as service authorization and log configuration, but are independent of each other and do not affect each other. This article describes the properties and runtime environment of functions, and how to create, configure, and delete functions through the Function Compute console.

After we create a service, we can create a function under it. The specific code in the function is the implementation of your business scenario. It can be created in 4 forms from standard Runtime; smooth migration of Web Server; container image; template creation.

 title=

What is shown here is the cloud function code of a lottery turntable that we wrote before. It mainly calls the cloud function through the applet, and the cloud function then calls the MongoDB data. Because when there are many lottery draws, good concurrency is required, so we use a combination of MongoDB and cloud functions. So that users do not have any stuttering phenomenon during the lottery, and at the same time can save hardware costs when there are few users, the cloud function meets our needs very well.

 title=

 title=

Tasks are really where asynchronous functions are managed. Provides a fully managed, out-of-the-box, observable-friendly large-scale task processing platform. After you upload a code package/container image to create a task handler, you can submit, view, stop, and retry tasks. It is also possible to pause/resume the execution of handler functions. Click here for more details.

The advanced functions of Function Compute mainly include monitoring, domain name, elastic management, and layer management. The monitoring panel as shown in the figure is the monitoring of the overall data.

  1. It is easy to understand that the domain name is to bind your own cloud function application, which can be a web application or an API interface.
  2. Elastic management is to configure the resource usage of cloud functions to go online to prevent excessive costs. At the same time, you can also configure the minimum instance to reduce the number of cold starts.
  3. Layer management provides common dependency libraries, runtime environments, etc., functions can be shared before, reducing the size of code packages.
  4. Audit logs, permission assistants, workflows, and application engine function products in other functions are not elaborated here. Interested friends can try it when developing their own cloud functions.

 title=

1. What scenarios need to use a custom domain name? When your function is fired using an HTTP request, you may need to use your own custom domain name to access your function. For example, you have created a function as a web application, and you want users to access the web application through a fixed domain name, then you can bind a custom domain name to the web application. In addition, you can also set the CDN acceleration function for the domain name, use the application deployed on Function Compute as the source station, and publish the source content to the edge nodes, so that end users can quickly read the required content.

2. What is flexible management? Function Compute provides you with two instance usage modes: On-demand mode and reserved mode. You can configure the maximum number of instances to control the upper limit of resource usage of a single function to prevent instance occupation caused by excessive calls to a single function, protect back-end resources, and avoid unexpected costs. At the same time, you can activate reserved mode instances by configuring the minimum number of instances. The Function Compute system will use the configured minimum number of instances as the resident instance of the function to quickly respond to function call requests and reduce the number of cold starts. Online businesses provide better service responsiveness. Pay-as-you-go mode and reservation mode are billed differently.

3. What is a layer? Layer can provide you with customized public dependency libraries, runtime environments, and function extensions and other publishing and deployment capabilities. You can extract common libraries that functions depend on into layers to reduce the size of code packages during deployment and update, or you can deploy custom runtimes in layers and share them among multiple functions. Layers are deployed by default in the /opt directory of the function execution environment, as a code directory to which functions can additionally be attached. When the function configures multiple layers, the contents of these layers will be merged into the /opt directory.

The product functions are discussed here, from which you can comprehend and sort out the operability and characteristic points of function computing.

Deploy the web framework Spring Boot

Statement Note: You need to log in to your Alibaba Cloud account to start the Function Compute service. The Function Compute service is a traffic fee and may incur traffic charges. Please use with caution. If you incur expenses, please bear by yourself, or you can simply read and learn.

Next to the product functions in the previous chapter, we click "Apply" in the left menu.

  1. Click Apply on the left menu, select Create an application from a template, and select the Spring Boot template. It is best to carefully read the instructions in the details before creating.
  2. Then click Create Now to go to the detailed page, where I choose to deploy directly.

 title=

  1. The application name directly follows the default, and then clicks Create.

 title=

We will see a process of creation. After the creation, you can see that the framework has been generated, is it very simple and foolish?

 title=

 title=

  1. Click to visit the domain name to see the website page. This is the generated framework home page, and the cloud platform automatically configures a subdomain for us.

 title=

 title=

  1. Click View All on the creation page above to see the log information of the creation process.

The deployment is divided into 4 parts, pre-environment; resource synchronization; resource inspection; execution deployment. Those who are interested can read it carefully, which is helpful to understand the overall process.

  1. Go through the services and functions in the function resource at the bottom to see the specific content.

 title=

When you click on the service web-framework, you will jump to the specific service and function information. At this time, you can see that the Spring Boot framework created by the template has been deployed. If you are interested, you can view the framework code and modify the code.

 title=

At this point, we have all completed the deployment of the Spring Boot framework to the function computing platform, and you can experiment by yourself.

Deploy the web framework Express case

Statement Note: You need to log in to your Alibaba Cloud account to start the Function Compute service. The Function Compute service is a traffic fee and may incur traffic charges. Please use with caution. If you incur expenses, please bear by yourself, or you can simply read and learn.

Continue to deploy the TodoList example of the Node.js language Express framework. Also find Express's TodoList example in the app.

Select a popular application, select the corresponding template case, and then click Details.

 title=

We can read the installation instructions in the layer that pops up on the right. Remember here we don't have to click create now, we go back to the machine to create the application with the client.

 title=

This time we use the open source tool Serverless Devs to deploy the application locally. Before installing the Serverless Devs client, you need to have npm pre-installed on your machine, that is, you need to install the Node.js environment. You can download node.js on its official website:

https://nodejs.org/en-us

Be sure to select the default Paht and add it to the global variable, then open CMD, check whether the node installation is normal, and enter the following command into cmd.

 node -v

The version number shown in the figure below is normal. If there are other problems, please install the npm package environment on Baidu.

 title=

  1. Install Serverless Devs first, and you can install it directly from the official documentation in the figure.

https://docs.serverless-devs.com/serverless-devs/install?spm

 $ npm install @serverless-devs/s -g
br

The following means the installation is successful.

 title=

Then we configure our own key through the command line. Click the avatar icon in the upper right corner of Alibaba Cloud for the key. In the popup layer that appears, select "AccessKey Management" to find our key and secret.

 title=

 title=

  1. We open the Windows cmd command prompt tool to configure the key and enter the following command:
 s config add

Select Alibaba Cloud according to the prompts and select Paste step by step. (Before entering the s command, remember to install the Serverless Devs client according to the instructions above. I will not give detailed instructions here).

 title=

  1. Initialize the template project, enter the following command, you can see that the template is downloaded to our local.
 s init todolist-app -d todolist-app

 title=

  1. Go to the local directory and deploy the cloud service to the Alibaba Cloud function computing platform. input the command:
 cd todolist-app && s deploy

You can see on the left that the generated domain is the access domain name, and the trigger is in the form of http. Including the name and configuration of the cloud function is clear at a glance. Here is a reminder that cloud functions deployed locally with client tools will not appear in the application list of the Alibaba Cloud platform.

 title=

  1. After deployment, you can see the generated domain name in the console, copy the domain name and access it on the browser, and this page will appear. If you modify the local code and then run the deploy command again, our serverless application will be updated.

 title=

Go back to the cloud platform, and you can see the deployed services in the "Services and Functions" menu on the left, and click on it to see the function just now.

 title=

Well, do you think cloud functions are not difficult to get here? In addition, remember that if you are just practicing learning and deploying applications, you can delete the corresponding application or service after the learning is completed to prevent additional costs.

Looking back, the deployment of the two frameworks has been completed, which is beyond imagination!

Here I would like to mention that I am quite satisfied with Alibaba Cloud's function computing products, especially the launch of this function, which makes it easier for beginners to learn and deploy.

In the above article, you learned how to deploy web applications to the function computing platform.

Make a Web IDE

Detailed analysis

Next, I will teach you how to deploy your own Web IDE tool, including how to configure the officially generated domain name as your own.

 title=

Introducing Open Source Web IDE Tools

Before the actual operation, let's take a brief look at this Web IDE tool. It is an out-of-the-box, ready-to-go lightweight Web IDE service based on Serverless architecture and Vscode. With a large number of plug-ins; real-time data storage; low cost and so on.

It uses the golang language to achieve reverse proxy access. It is based on the open source openvscode-server and provides the functions of the vscode Web IDE. Friends who are interested and know golang can learn how to debug locally.

 title=

Through the basic process, we can learn the operation principle of this open source tool, which is convenient for debugging and secondary development.

 title=

The focus here is on development and debugging. If you need to build in a local environment, you should read it carefully to understand the directory structure and related configuration information.

 title=

After the introduction of open source tools, let's strike while the iron is hot to practice!

Make your own Web IDE tools

To start making your own Web IDE tool, follow the steps below.

Statement Note: You need to log in to your Alibaba Cloud account to start the Function Compute service. The Function Compute service is a traffic fee and may incur traffic charges. Please use with caution. If you incur expenses, please bear by yourself, or you can simply read and learn.

  1. Find the template of serverless vscode webide from the official template and click to create it now.

 title=

Disclaimer Note: Object storage OSS services are traffic charges, and traffic charges will be incurred. Please use it with caution, if you incur costs, please bear by yourself. Of course, you can also just read and learn. You need to pay attention that this web ide tool needs to create OSS object storage, and OSS is convenient for storing user data. When creating an application, click Create New OSS Bucket, and after jumping to the OSS product, you can directly create an OSS Bucket. Click Create New OSS Bucket below the red box.

 title=

  1. Jump to the product page of object storage OSS. We click the bucket list on the left, then click Create Bucket, fill in a name webidepractise, and click Save.

You can also create an OSS bucket in advance through this link.

https://oss.console.aliyun.com/overview

 title=

  1. Go back to the application creation page, click Refresh, and select the OSS you just created. Remember that the cloud function and the OSS need to be in the same Region. After the creation is successful, it will appear in the application list.

 title=

  1. Click the configuration in the above figure, you can see the following page to understand the configuration of the following function. The red box is the OSS we selected, and it can also be adjusted to suit your own configuration.

 title=

  1. Click the access domain name in the list, you can see the Web IDE tool here, you can open the code on the Github repository here, and write the program directly online, isn't it very convenient.

 title=

  1. Configure your own domain name (if you have not purchased an Alibaba Cloud domain name, you can ignore the following).

Statement Note: You need to purchase a domain name from the Alibaba Cloud platform, and a fee will be charged for the domain name. Please use it with caution, if you incur costs, please bear by yourself. Click Domain Name Management on the left menu, click Add Custom Domain Name, you can see this page, configure the subdomain name here, then copy the public network CNAME domain name, click the cloud DNS DNS console to configure the domain name.

53.png

  1. After clicking the DNS console, you will be redirected to the domain name resolution product page, select the domain name you have purchased, and in the domain name resolution, we can configure the CNAME of the public network by clicking the resolution setting.

 title=

  1. A subdomain record of the IDE's cname is added, and the record value is copied from the cloud function, thus forming a mapping relationship. After adding, we go back to the cloud function domain name configuration page just now, select the corresponding routing configuration, select the service name, function name, version name corresponding to your domain name, click Create, and you're done.

 title=

9. Then enter your own domain name in the browser, our Web IDE tool appears, do you have a little excitement here?

 title=

At this point, we have learned to make our own Web IDE tools. Interested friends can develop and improve their own online tools.

All the knowledge learned and practiced in this practice has been finished. Review the overall knowledge, from understanding the concept, architecture, and application scenarios of Serverless, to learning to operate functional computing products, to deploying popular web frameworks, and finally, we also made a web IDE tool of our own.

I believe that for an introductory learner, you can follow me to learn and practice, which proves that you have basically mastered Serverless. Later, you can migrate and deploy your products, or you can develop and deploy your own new projects or new products on function computing.

Thinking summary

Here are a few questions for you to think about and summarize:

  • Serverless, what is the relationship between function computing?
  • Do you prefer local deployment or online deployment?
  • Study how Function Compute configures instances and how to set the maximum instance limit?
  • Think again...what should be migrated to cloud functions?

Like, comment and let me see what you think! If there are inappropriate places in the content, please leave me a comment and let's exchange and learn together.

Original address:

https://developer.aliyun.com/article/987303


阿里云云原生
1k 声望302 粉丝