Author: Xiliu

Pay attention to [Serverless] public account, background reply “learn” can get Serverless technology resource package!

Takeaway:

Spring Boot is a suite based on the Java Spring framework that comes preloaded with Spring components, allowing developers to create standalone applications with minimal configuration. In a cloud native environment, there are a number of platforms that can run Spring Boot applications, such as virtual machines, containers, and so on. One of the most attractive, however, is to run Spring Boot applications Serverless.

I will analyze the merits and demerits of Spring Boot application running on Serverless platform from five aspects, including architecture, deployment, monitoring, performance and security, through a series of articles. We had a basic introduction to the Mall application architecture and Serverless platform in Spring Boot on FC Architecture. In this article, I will show you how to deploy the Mall application to a functional computing platform. To make the analysis more representative, I chose Mall, an e-commerce app with more than 50K stars on Github, as an example.

precondition

Preparation stage:

  • You need to have an Aliyun account;

  • You need to have a machine that can be accessed through public IP, and install MySQL, Redis and other Mall application software.

  • You need to install Git, Docker, Java, and Maven software on the machine running the dependent software.

  • You need to install and configure the Serverless Devs tool.

Note If you use a cloud host, check whether the security group configuration of the host allows inbound network requests. After a host is created, the access to the inbound network port is strictly restricted. We need to manually allow access to MySQL port 3306, Redis port 6379, etc. As shown below, I manually set up the security group to allow all incoming network requests.

Deploying dependent software

Mall relies on MySQL, Redis, MongoDB, ElasticSearch, RabbitMQ, etc. Each of these applications has its own cloud product on the cloud. In a production environment, cloud products are recommended for better performance and availability. In a personal development or POC prototyping scenario, we choose a VM to containerise all dependent software.

1.1 Clone Code Repository

git clone https://github.com/hryang/mall
Copy the code

Domestic access to Github network is not good, if clone is too slow, can use the Gitee address.

git clone https://gitee.com/aliyunfc/mall.git
Copy the code

1.2 Building and Running a Docker image

Under the docker folder in the code root directory, there is the corresponding Dockerfile for each dependent software. Run the run.sh script in the root directory of the code, and the Docker images of all dependent software will be automatically built and run locally.

sudo bash docker.sh
Copy the code

1.3 Verifying the running status of dependent software

Run the Docker ps command to check whether the dependent software is running properly.

sudo docker ps
Copy the code

Deploying the Mall Application

2.1 Modifying Mall Application Configuration

Modify the following three YAML files and change the host field to the public IP address of the node where you installed MySQL and other software in step 1, as shown in the figure:

​mall-admin/src/main/resources/application-prod.yml​

​mall-portal/src/main/resources/application-prod.yml​

​mall-search/src/main/resources/application-prod.yml​​ 

2.2 Generating Mall Application Container Images

Run maven package command to generate Docker image in Java8 or Java11 environment.

sudo -E mvn package
Copy the code

If yes, the following success information is displayed:

Run sudo Docker images and you should see images of the 1.0-snapshot version of mall/mall-admin, mall/mall-portal, and mall/mall-search.

2.3 Pushing an Image to the Alicloud Image Repository

First login ali cloud image warehouse console, select the personal version of the instance, according to the prompt let Docker login Ali cloud image warehouse.

Then create the namespace. As shown in the figure below, we created a namespace named quanxi-hryang.

According to the previous steps, we have generated images of mall/mall-admin, mall/mall-portal, and mall/mall-search locally.

Execute the following command to push the mall-admin image to the mirror warehouse under the quanxi-hryang namespace in hangzhou.

Please modify CN-Hangzhou and Quanxi-hryang in the following commands to use your own mirror warehouse region and namespace. Mall /mall-portal, mall/mall-search and so on.

Sudo docker tag mall/mall - admin: 1.0 the SNAPSHOT registry.cn-hangzhou.aliyuncs.com/quanxi-hryang/mall-admin:1.0-SNAPSHOT Sudo docker push registry.cn-hangzhou.aliyuncs.com/quanxi-hryang/mall-admin:1.0-SNAPSHOTCopy the code

2.4 Modified the Application definition of the Serverless Devs Tool

We used the Serverless Devs tool to define and deploy the application. In the project root, there is the s.aml file, which is the project definition file for the Serverless Devs tool. This defines the resources that the function evaluates to.

As shown in the figure below, we define a service named mall-admin and a function under mall-admin on the function calculation. The function defines attributes such as port, memory size, timeout, runtime, etc. What is in the red box is what you need to modify according to your own configuration.

  • Access is the identity that you configure with S config, default is default. If you use the default Settings, no changes are required here.

  • Region is the region that you want to deploy. There are OPTIONS such as CN-Hangzhou, CN-Shanghai, CN-Beijing, and CN-Shenzheng.

  • If the function runs using custom-Container, you need to specify the image address. Please change the mirror address in S.aml to the mall-admin mirror address you pushed in the previous step. Similarly, you need to change the mirror address of mall-portal and mall-search in S. yaml.

(Suggestion: it is best to use registry-vpc.cn-hangzhou.aliyuncs.com/fc-demo/mall-admin:1.0-SNAPSHOT for the above image address)

2.5 Deploying Mall on the Functional Computing Platform

Run the s deploy command. After the deployment is successful, you can view the corresponding url.

Enter the generated URL in the address box of the browser. If no login or token has expired is displayed, the service is successfully deployed.

(Note: Serverless is typically created only after the request arrives by default, so the first startup takes a long time, called a cold start. The Mall application usually takes about 30 seconds to start. We will review this issue later in the performance tuning article, using a number of techniques.

To debug the swagger API, visit host/swagger-ui.html.

2.6 Viewing Application Logs

In s. aml, we set logConfig:auto for each service, which means that the serverless-devs tool automatically creates a LogStore for the service. All services share a LogStore. All application logs are output to.

  • You can use​s logs​Command to view the logs of all services at a certain point in time.
  • You can also use​s mall-admin logs​View the log of the mall-admin function.
  • You can also use​s mall-admin logs -t​Real-time display of logs after the current point in time in follow mode;
  • You can also use​s mall-admin logs --keyword=abc​View logs that contain the keyword ABC.

S Logs are very useful for you to understand service health and problem diagnosis. For example, if we run s mall-admin logs -t to enter follow mode, and then access the endpoint of the mall-admin service in the browser, we can see the startup and request processing logs of the entire application.

2.7 Deploying the Mall Front-end Project

Mall also provides a front-end interface based on Vue+Element. It mainly includes commodity management, order management, membership management, promotion management, operation management, content management, statistical reports, financial management, authority management, Settings and other functions. The project also runs seamlessly on functional calculations.

Start by installing NodeJs12 and NPM on your machine, and download the project source code.

git clone https://github.com/hryang/mall-admin-web
Copy the code

Domestic access to github network is not good, if clone is too slow, can use the following proxy address.

git clone https://gitee.com/aliyunfc/mall-admin-web.git
Copy the code

(Note: must be NodeJS 12 or 14, too recent node versions will fail to compile)

Modify config/prod.env.js to change BASE_API to the endpoint of the mall-admin function that was successfully deployed on the function calculation.

Build the front-end project by executing the following command in the project root directory.

npm install
npm run build
Copy the code

After a successful run, the dist directory is generated. Run the docker.sh script in the root directory of the project to generate the image.

sudo bash docker.sh
Copy the code

Run the Docker images command to see that the mall/mall-admin-web image has been successfully generated. Push the image to Ali Cloud mirror warehouse.

Similarly, change CN-Hangzhou and Quanxi-hryang in the following commands to your own mirror warehouse region and namespace.

Sudo docker tag mall/mall - admin - web: 1.0 the SNAPSHOT Registry.cn-hangzhou.aliyuncs.com/quanxi-hryang/mall-admin-web:1.0-SNAPSHOT sudo docker push registry.cn-hangzhou.aliyuncs.com/quanxi-hryang/mall-admin-web:1.0-SNAPSHOTCopy the code

Modify the s.yaml in the root directory of the project, similar to the deployment of mall-admin. Adjust access and region according to your configuration, and change image to the image address pushed successfully in the last step.

Run the s deploy command. After the deployment is successful, you can view the URL of the mall-admin-web service. Access the login page through a browser. Fill in the password macro123 to see the full effect.

(Note: The first time due to cold startup, the login page may report a timeout error. Refresh the page again. We will optimize cold start performance in a later performance tuning article.)

conclusion

Because the Serverless platform has built-in gateways for routing, instance pull-up/run/fault-tolerant/automatic scaling, developers upload application code packages or images and publish an elastic and highly available service. To sum up, just complete the following five steps to fully deploy the Mall application on the functional computing platform. For subsequent application updates, repeat steps 4 and 5. Thus, Serverless eliminates the repetitive work of environment configuration, operation and maintenance, and greatly improves the efficiency of development operation and maintenance.

  1. Clone Project Code
  2. Find a VM, run a script to install MySQL, Redis and other dependent software in one click
  3. Modify host in application configuration and set the value to the VM public IP address in Step 2
  4. Generate an application image and push it to the Alicloud image warehouse
  5. Deploy applications to functional computing platforms

Article url summary

1) Spring Boot:

​​https://spring.io/projects/spring-boot​​

2) the Mall:

​​https://github.com/macrozheng/mall​​

3) Serverless Devs installation Documentation

​​http://serverless-devs.com/zh-cn/docs/installed/cliinstall.html​​

Click here for more information on function calculations!