Author:

, and reply to "Learn" in the background to get the Serverless technology resource package!

Guide:

Spring Boot is a suite based on the Java Spring framework. It pre-installs a series of Spring components, allowing developers to create stand-alone applications with minimal configuration. In a cloud-native environment, there are a large number of platforms that can run Spring Boot applications, such as virtual machines, containers, etc. But the most attractive of them all is to run Spring Boot applications in a serverless fashion.

Through a series of articles, I will analyze the advantages and disadvantages of running Spring Boot applications on the Serverless platform from five aspects: architecture, deployment, monitoring, performance, and security. In the previous article "Spring Boot on FC Architecture", we had a basic introduction to the Mall application architecture and the Serverless platform. In this article, I will tell you how to deploy the Mall application to the function computing platform. In order to make the analysis more representative, I chose the e-commerce application mall with more than 50k stars on Github as an example.

precondition

preparation stage:

  • You need to have an Alibaba Cloud account;
  • You need to have a machine that can be accessed through the public network ip, and install the software that Mall applications such as MySQL, Redis depend on;
  • You need Git, Docker, Java and Maven software installed on the machine running the dependent software;
  • You need to install and configure the Serverless Devs tools.

Note that if you use a cloud host, please first check whether the security group configuration corresponding to the host allows inbound network requests. After the general host is created, the network port access in the inbound direction is strictly restricted. We need to manually allow access to port 3306 for MySQL, port 6379 for Redis, etc. As shown in the image below, I manually set the security group to allow all incoming network requests.

Deployment dependent software

The Mall application relies on MySQL, Redis, MongoDB, ElasticSearch, RabbitMQ and other software. These software have corresponding cloud products on the cloud. In production environments, cloud products are recommended for better performance and availability. In personal development or POC prototype demonstration scenarios, we choose a VM to containerize and deploy all dependent software.

1.1 Clone code repository

git clone https://github.com/hryang/mall

It is not very good to access the Github network in China. If the clone is too slow, you can use the Gitee address.

git clone https://gitee.com/aliyunfc/mall.git

1.2 Building and running a Docker image

In the docker folder of the code root directory, there is a Dockerfile corresponding to each dependent software. Running the run.sh script in the root directory of the code will automatically build the Docker image of all dependent software and run it locally.

sudo bash docker.sh

1.3 Verify the running status of dependent software

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

sudo docker ps

Deploy the Mall application

2.1 Modify the Mall application configuration

The following three modifications yaml file by the ​host​ field You step into a software installation MySQL public network IP node, as shown:

​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 Generate Mall application container image

Execute the maven package command to generate a Docker image, either in a Java8 or Java11 environment locally.

sudo -E mvn package

After success, the following success message will be displayed.

Execution ​sudo docker images​ , should be able to see the mall / mall-admin, mall / mall-portal, 1.0-SNAPSHOT version of the mall / mall-search of a mirror.

2.3 Push the image to the Alibaba Cloud image repository

First, log in to the Alibaba Cloud Mirror Warehouse console, select the personal version instance, and follow the prompts to let docker log in to the Alibaba Cloud Mirror Warehouse.

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

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

Run the following commands, the mall-admin mirror region pushed to Hangzhou, ​quanxi-hryang​ image repository of a namespace.

Add the following command ​cn-hangzhou​ and ​quanxi-hryang​ modify for your own image warehouse area and namespaces. mall/mall-portal, mall/mall-search and so on.

sudo docker tag mall/mall-admin:1.0-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-SNAPSHOT

2.4 Modify the application definition of the Serverless Devs tool

We use Serverless Devs tools to define and deploy applications. Under the project root directory, there ​s.yaml​ file, which is Serverless Devs tool for project definition file. This defines the resources of function computing.

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

  • ​access​ you use ​s config​ identity configuration, the default is the default. If you are using the default settings, no changes are required here.
  • ​region​ is the area you want to deploy, there are cn-hangzhou, cn-shanghai, cn-beijing, cn-shenzheng other options.
  • When the function uses the custom-container runtime, you need to specify the mirror address. Please change the mirror address in s.yaml to the mall-admin mirror address you pushed in the previous step. Similarly, you also need to change the mirror address of mall-portal and mall-search in s.yaml.

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

2.5 Deploy the Mall application to the function computing platform

Execution ​s deploy​ command, after the deployment is successful, you will see a corresponding visit our Web site.

Enter the generated URL in the browser. If it displays "Not logged in yet or the token has expired", the service deployment is successful.

(Note: The feature of Serverless is that the system will create an instance after the request arrives by default, so the first startup time is relatively long, which is called a cold start. The Mall application generally takes about 30s to start. We will talk about performance tuning later in the article. review the problem, using a series of optimizations.) 161e327c8be27e

Visit the corresponding swagger api debugging page host/swagger-ui.html to debug the related back-end API.

2.6 View application log

We ​s.yaml​ are set up for each service ​logConfig:auto​ , automatically creates a log library service (LogStore), all services share a logging library on behalf of serverless-devs tool. All logs of the application are output to .

  • You can use ​s logs​ command to view a log of all service points in time;
  • You can also use ​s mall-admin logs​ View mall-admin function of the log;
  • You can also use ​s mall-admin logs -t​ real-time display of the log after the current point in time to follow the pattern;
  • You can also use ​s mall-admin logs --keyword=abc​ Log Viewer contains keywords abc's.

s logs are useful for you to understand how your service is running and diagnosing problems. For example, we perform ​s mall-admin logs -t​ enter the following mode, and then access the browser ​mall-admin​ Endpoint service, will be able to see the start request processing and logs the entire application.

2.7 Deploy the Mall front-end project

Mall also provides a front-end interface based on Vue+Element implementation. It mainly includes functions such as commodity management, order management, membership management, promotion management, operation management, content management, statistical reports, financial management, authority management, and settings. The project can also run seamlessly on Function Compute.

First install nodejs12 and npm on your machine, and download the project source code.

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

It is not very good to access the github network in China. If the clone is too slow, you can use the following proxy address.

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

(Note: must be nodejs 12 or 14, too new node version will fail to compile)

Modify ​config/prod.env.js​ , endpoint successfully deployed in the mall-admin function calculation which BASE_API changed before.

Execute the following command in the project root directory to build the front-end project.

npm install
npm run build

After running successfully, the dist directory will be generated. Run the docker.sh script in the project root directory to generate the image.

sudo bash docker.sh

Run ​docker images​ command, you will see the mall / mall-admin-web image has been successfully generated. Push the image to the Alibaba Cloud image repository.

Similarly, add the following command ​cn-hangzhou​ and ​quanxi-hryang​ modify for your own image warehouse area and namespaces.

sudo docker tag mall/mall-admin-web:1.0-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-SNAPSHOT

Under the modified project root directory ​s.yaml​ , and deployment of mall-admin similar, adjusted according to your configuration ​access​ , ​region​ , will ​image​ replaced the previous step to push the success of the mirror address.

Execution ​s deploy​ , when the deployment is successful, you can see the URL mall-admin-web services. Access through a browser and you will see the login page. Enter the password ​macro123​ , you will be able to see the full effect.

(Note: The login page may report a timeout error due to a cold start for the first time.

Summarize

Since the serverless platform has a built-in gateway, which is responsible for routing, instance pulling/running/fault tolerance/automatic scaling and other functions, after the developer uploads the application code package or image, an elastic and highly available service has been released. To sum up, as long as the following 5 steps are completed, the Mall application is fully deployed on the function computing platform. Subsequent updates to the application only need to repeat steps 4 and 5. It can be seen that Serverless eliminates repetitive tasks such as environment configuration and operation and maintenance, and greatly improves the efficiency of development and operation.

  1. Clone project code
  2. Find a VM, run the script to install MySQL, Redis and other dependent software with one click
  3. Modify the host item in the application configuration and fill in the value as the VM public network ip in step 2
  4. Generate an application image and push it to the Alibaba Cloud Image Warehouse
  5. Deploy the application to the function computing platform

Summary of URLs in the text

1)Spring Boot:

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

2)Mall:

https://github.com/macrozheng/mall

3) Serverless Devs installation documentation:

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

Click here , to see more information calculation function!


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