Author: Tao Yuhang
This article is selected from the "Serverless Function Calculation Solicitation Order", and the background reply "Function" for details
background
What is a function? Not long after I finished the math test, I immediately thought of independent variables, dependent variables, and function values, that is, y=f(x). Of course, in a computer, a function often refers to a defined code program. We can call this defined function by passing parameters to achieve the function we need. So, what is functional computing FC today?
In the era of cloud computing, DevOps, which is developed with containerization technology and various tools, has carried out a new coordination of development and operation and maintenance. After the developers have finished writing the code, they do not need to consider the environment and directly submit them to various pipelines. Testing, development, and deployment can be completed, the project builds microservices, and the container completes the encapsulation of the environment. But often we still need to devote energy to the cluster where the business goes online. Is it a private cloud environment or a public cloud? Is it a bare metal server or a cloud instance ECS? Buy or rent?
Of course, the implementation of DevOps and the operation and maintenance of servers and clusters all require a lot of resources and energy. DevOps is a shortcut, but not the only way out. The emergence of function computing FC has brought a serverless architecture, so that developers no longer feel the complexity of deploying services when developing and deploying.
Indifferent to the server, developers can really focus on their own code. Alibaba Cloud's serverless functional minimalist programming can focus on business innovation, without procurement and deployment costs, and provides complete observability capabilities such as monitoring and alarming. Alibaba Cloud Function Compute FC is an event-driven fully managed computing service. There is really no need to consider the operation and maintenance management of the server. You only need to upload the developed code. Function Compute will use the role strategy to plan computing resources and execute functions in an elastic way. , and finally execute the deployment efficiently. grace!
What are the applicable scenarios for Serverless? Is it a person? Or production? So this time I will deploy two applications in different directions to evaluate Serverless!
1. Notification system and Webhook, Trigger trigger and Chat robot.
Push functions involved in many systems, such as mail, SMS, Webhook. Of course, the ability of Webhook is not only information notification, but the notification function referred to here must require infrastructure, that is, the server to support the operation. If these functions are directly operated by Serverless, we do not need to pay for the operation and maintenance of a server, saving A lot of work and expense. Similarly, we can use the serverless event-driven model to achieve timing and automatic triggering of tasks, and automatic check-in and automatic sending.
Second, the second, of course, is the application of the Web class.
Based on the application deployment of various web frameworks and the construction of sites based on Java, Python, PHP and other languages, Serverless can easily implement the online blog application such as wordpress. In conjunction with other cloud products, Codeup, OSS, RDS, etc., high-availability and high-performance web applications can be realized, such as the official Kod cloud disk system.
Cloud transformation of serverless agricultural products e-commerce website
Here I take a large project of the next door software major (I am a network technology) as a deployment case. The project is very simple and has a single function, but it is also a very classic front-end and back-end separation project. Since I am not very good at development, the specific technology of the project will not be ugly. It was said before that there is any copyright, so I will not release the source code hhhh.
Before the transformation, I believe that it is the classic AllinOne structure of many small and medium-sized companies. All the service middleware involved in the business is run on a server/virtual machine. Although it may be completely a laboratory environment now, the actual There are still many projects that do this. Not to mention the disadvantages, such as difficult deployment, operation and maintenance, difficulty in resource expansion, complicated follow-up transformation, poor performance, and no high-availability technology applied. . .
Serverless's agricultural product e-commerce platform is on the cloud, the project is relatively simple, and the planning is in the same region. Mainly, the front-end HTML page and the back-end Jar package are run by two separate serverless functions to complete the running calculation. The rest of the supporting services are implemented on the cloud using the corresponding cloud products. Here, the Redis service runs on my own ECS, so I no longer need to purchase the ApsaraDB for Redis version. Of course, Alibaba Cloud's Redis cloud database product is recommended.
1. Serverless service creation
Enter the Function Compute console, select [Services and Functions], and click [Create Service]
Enter the name and description of the created service, enable the log function, and click OK to complete the creation of the service.
After the service is created, go to [Service Details], find [Network Configuration], and click [Edit]
Select Allow access to VPC, select custom configuration, select VPC, vSwitch, and security group. This needs to be kept under the same VPC as other supported cloud products (databases, etc.). Therefore, it is necessary to do a good job in the planning of the network on the cloud, and also check whether the product is in stock in the region.
2. Front-end page serverless function creation
After the service is created, click [Create Function] to enter the function creation page
Select [Use Custom Runtime Smooth Migration WebServer], enter the function name, select the running environment as [Nginx], upload the front-end HTML code and Nginx configuration file, and select the listening port as 80, which is the service port of the original Nginx middleware.
After the function is created, enter the function details interface, and you can edit and modify the code in the function code. After the code is changed, you need to click the deploy code to re-upload. As mentioned above, the Nginx service configuration needs to be uploaded together, and a copy of the /etc/nginx/mime.types file needs to be copied to the current directory to avoid mime file type mapping errors.
The key configuration of Nginx is put here, and it is modified according to your own business situation. Note that the port monitoring in the configuration needs to be consistent with the monitoring port created by the function, and the path of the webpage code is set to /code
http {
include mime.types; #注意引入此文件
keepalive_timeout 900;
server {
listen 80;
server_name localhost;
location / {
root /code;
index index.html index.htm;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /users {
proxy_pass http://serverless.后端函数访问地址.run;
#Nginx反代传给后端的函数,地址在后续后端函数创建完可以拷贝
}
location /items {
proxy_pass http://serverless.后端函数访问地址.run;
#Nginx反代传给后端的函数,地址在后续后端函数创建完可以拷贝
}
You can query related logs in the call log
When the function is triggered to run, Serverless will automatically start the instance according to the access request. Here, you can manually log in to the instance to check and troubleshoot the logs of the middleware service and the running environment.
3. Backend Serverless Function Creation
Go back to the function service interface, and click [Create function] again to enter the function creation page.
Select [Use Custom Runtime Smooth Migration WebServer], enter the function name, select the running environment as [Java], and upload the packaged Jar package file.
Modify the [Startup Command] and [Listening Port] according to the specific business. I need to listen to the business port as 8080, and need to pass in the parameters of the database connection at runtime. The database source username and password connection address configured here will be set in the subsequent ApsaraDB for RDS.
After the back-end function is created, enter the function details interface. The rest of the functions are the same as the above-mentioned front-end functions, and will not be repeated.
Click [Trigger Management], where the HTTP trigger provides the public network and intranet access addresses. We copy the intranet address and fill in this domain name in the reverse generation configuration of the front-end middleware to access the front-end and trigger the back-end function.
4. ApsaraDB for RDS MySQL Serverless creation, migration and import
The original Allinone architecture did not achieve independence and high availability of database services. In this cloud deployment, we will use the serverless version of ApsaraDB for Alibaba Cloud's public beta. RDS MySQL Serverless provides real-time elastic capabilities of CPU and memory. It has the advantages of low resource consumption, ease of use, flexibility, and low price. It optimizes usage costs reasonably and further reduces costs and increases efficiency.
- RDS MySQL Serverless instance creation
Enter the ApsaraDB for RDS console, click [Create Instance] to start the process of creating an instance, select the Serverless version on the [Basic Resources] setting interface, and select the rest according to the actual situation
When creating a Serverless RDS, you need to pay attention to the settings of the network VPC in [Instance Configuration], which must be consistent with the VPC set by the Serverless service to achieve intranet data exchange. After confirming the order, wait for the instance creation to complete.
- RDS database user settings
After the instance is created, select Manage Instance. Select [Account Management] on the left task bar, click [Create Account] to create a database account to connect to the backend of the supplier platform. Enter the account name and password, select the normal account, and click OK to complete the user creation.
- RDS business database creation
Select [Database Management] in the left column, select [Create Database]
Enter the library name required for the e-commerce business of agricultural products, and authorize the account to the user set in the previous step, click Create to complete the library setting.
- RDS service connection address
The address of the connection library required in our serverless function can be found in the [Database] connection in the RDS instance, but the whitelist needs to be set in advance.
We copy the intranet address, and also complete the configuration of the connection user, password, and library, and then configure it into the serverless function or the back-end code.
- Import recovery of RDS business database
The key to the migration of databases to the cloud is the migration of data content. The deployed business databases are few and small, so a simple backup SQL script file is used as the migration method. There are many ways to export data from MySQL/Mariadb, which can be backed up and exported according to actual needs. Of course, large-scale business databases have dedicated backup and migration tools, which will not be discussed in detail here.
Enter the DMS data management service, select [Database Development], and click [Data Import] under [Data Change].
Import the database according to the specific backup method, here I choose to upload the SQL script of the backup, submit an application to start importing the data
Data import is complete, data migration is complete
5. Redis service on cloud and static resource CDN
The Redis service is migrated to the cloud. As mentioned above, the Redis service is running on my own ECS, so I no longer need to purchase the ApsaraDB for Redis version separately. The ECS is also under the same VPC, which enables intranet interoperability. Of course, Alibaba Cloud's Redis cloud database product is recommended.
The CDN of static resources, including the acceleration of css\js\ images, has been stored in Alibaba's CDN service in the original architecture, so I won't make any changes here.
6. Public network business access domain name configuration
In the last step, the user finally accesses the front-end serverless function. As suggested by Alibaba Cloud, accessing the default public network address will not do any middleware parsing, but directly download the HTML static file of the home page. Therefore, we need to configure the access domain name by ourselves. .
Go back to the Function Compute console, select [Domain Name Management], and click [Add Custom Domain Name]
Enter a custom domain name, configure routing, and select the service name, function name, and version number of the corresponding function LATEST (latest)
Copy the CNAME value that needs to be resolved
Copy the value of the CNAME record, click [Cloud DNS Console], enter the resolution settings, and click [Add Record]
Select the record type [CNAME], enter the host header, fill in the copied record value, and confirm to complete the addition
7. The effect of serverless cloud migration of agricultural products e-commerce project
Home page, access effect, front-end functions are correct
Agricultural product details page visit
User registration function test, database connection and writing are correct
Data in RDS has been successfully written
User login test
Order submission test, the backend function is correct
Quickly build litemall e-commerce application system using Serverless application module
1. Create an application based on the official template
Serverless applications provide a large number of official application templates. We can modify our business according to the templates given, so it is also important to be familiar with the deployment of templates.
Go to the console page of Function Compute FC, click [Apply], select [Create Application from Template], and select [Mall Case]
View the deployment template information through details, and query the source code. Click Create Now to quickly experience the creation of Serverless applications. Local deployment can be deployed through Serverless Devs tools.
2. Configure the application creation
After clicking Create Now, we perform the initial configuration of the application.
There are two types of deployment: 1. Deploy through a third-party code repository, 2. Deploy directly
The difference between the two is that you can use your own warehouse code to update the project release later through push, while direct deployment requires manual configuration.
As you can see here, our delivery trigger is also based on the Git repository push submission, and each submission will automatically trigger the deployment.
If you configure the application yourself, you need to configure the s.yaml file according to the business, refer to:
https://www.serverless-devs.com/fc/yaml/readme
Here I choose the gitee warehouse for deployment, but the authorization of the third-party application of the warehouse is required
Click to authorize, jump to the gitee site to make an OAuth authorization request, and click to agree to the authorization
When Alibaba users use FC function computing for the first time, they need to add a role strategy. I have already used Alibaba Cloud Function Computing FC here. If you are prompted to add a strategy, follow the prompts and click Add.
Other advanced configurations need to be modified according to the business. Here, pay attention to the selection of the region. Other elastic resources will be located in this region. I choose the local Hangzhou here.
After completing the configuration, click Create, and the code has been newly uploaded to my gitee repository. The s.yaml provided here can be used as a reference for configuration, and then modify the yaml according to the deployed business
3. Application deployment goes live
The application is created and deployed automatically for the first time. Here you can see the deployment status is being deployed.
View the deployment log. If there is an error in the deployment, you can also query the log information to report the error.
After the deployment has gone through the four steps of pre-environment, resource synchronization, resource inspection, and execution of deployment, our e-commerce application has been deployed.
The first deployment is completed, and it is also a deployment version of the latest latest, which can be rolled back freely through the deployment history
4. Access to deployed e-commerce applications
Visit the tested domain name, you can see our online litemall e-commerce system, and enter the background management of the e-commerce application
The litemall e-commerce system is an open source e-commerce system with WeChat applet separated from the front and back ends. It has membership management, mall management, commodity management, promotion management, system management, configuration management, and statistical reports based on the e-commerce platform.
The litemall e-commerce system needs to be configured with the minimum development environment: MySQL, JDK1.8 or above, Maven, Nodejs
5. Change business domain name access
Similarly, when we launched the FC business normally, Serverless uses the default access address
The domain name provided by Alibaba Cloud Function Compute FC is ..fc.aliyuncs.com//proxy///[action?queries] as the default. For normal business access, we must modify the visited domain name.
Go to the homepage of Alibaba Cloud Function Compute FC and click Domain Name Management under Advanced Features. Here you can see the routing information of the default domain name when we launch the e-commerce application.
We choose to add a custom domain name
Enter the name of the domain name, that is, the custom second-level domain name under the registered domain name
Click on the routing configuration, select the service name, here is the e-commerce system litemall we deployed, select the function name and version
Copy the public network CNAME address, and then add resolution in the DNS domain name management
Enter the domain name management, add a record, select CNAME for the record type, enter the host record, paste the public network CNAME address just copied to the record value, and click Add.
Go back to Alibaba Cloud Function Compute FC, click Create at the end, and go back to the home page to see our newly bound domain name
Finally, take the mobile phone to access the public network address I customized
The e-commerce service is online normally, and the public address is normal to access the homepage
Product details purchase page:
at last
Compared with other solutions, Serverless is also a very easy-to-use and efficient technical solution. In fact, there are still many areas for improvement in the above deployment test, but I want to focus on the serverless server inaction . I have also used a lot of Alibaba Cloud technology products, and I know the difficulty of operating and maintaining the underlying infrastructure. .
Although these months have been busy with study and life, I am still very interested in various activities in the community and want to do some better tests. This time Serverless is for both individual users and enterprise users. Both are a very good choice, flexible resources and pay-as-you-go, saving resources and Money, and more elegant!
*This article is reproduced from: Tao Yuhang\
Original link: https://developer.aliyun.com/article/980852 *
Official opening! Alibaba Cloud Apsara Technology Summit on August 11 invites you to explore the best practices of cloud native
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。