Introduction: SpringBoot 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 the cloud-native world, 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 order to make the analysis more representative, I chose the e-commerce application mall more than 50k stars on Github as an example.
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 introduce how to deploy the Mall application to the function computing platform. superior.
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 the Mall application depends on, such as MySQL and Redis.
- You need Git, Docker, Java and Maven software installed on the machine where the dependent software is running
- You need install and configure 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.
1. Deploy 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
docker
folder of the code root directory, there is a Dockerfile corresponding to each dependent software. run.sh
script in the code root directory 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
2. Deploy the mall application
2.1. Modify the mall application configuration
Modify mall-admin/src/main/resources/application-prod.yml
, mall-portal/src/main/resources/application-prod.yml
, mall-search/src/main/resources/application-prod.yml
3 Ge yaml file, which will be host
field into the public network Step 1 You install software such as MySQL node of the ip.
2.2. Generate the mall application container image
Execute the maven package command to generate the docker image.
The local is java8 or java11 environment.
sudo -E mvn package
After success, the following success message will be displayed.
Execute sudo docker images
, you should be able to see the mirrors of the 1.0-SNAPSHOT version of mall/mall-admin, mall/mall-portal, and mall/mall-search.
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 quanxi-hryang
According to the previous steps, we have generated images of mall/mall-admin, mall/mall-portal, mall/mall-search locally. Execute the following command to push the mall-admin image to the mirror repository under the quanxi-hryang
cn-hangzhou
and quanxi-hryang
in the command below to your own image repository region and namespace. 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. In the project root directory, there is the s.yaml
file, which is the project definition file of the Serverless Devs tool. 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
iss config
, the default is default. If you are using the default settings, no changes are required hereregion
is the region you want to deploy. There are options such as cn-hangzhou, cn-shanghai, cn-beijing, and cn-shenzheng.- 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 the form of registry-vpc.cn-hangzhou.aliyuncs.com/fc-demo/mall-admin:1.0-SNAPSHOT
2.5. Deploy the mall application to the function computing platform
Execute the s deploy
command. After the deployment is successful, you will see the corresponding access URL.
Enter the generated URL in the browser. If "Not logged in yet or the token has expired" is displayed, 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 startup. It usually takes about 30 seconds for the Mall application to start. Later we will review this problem in the performance tuning article, using a series of means to optimize.
Visit the corresponding swagger api debugging page host/swagger-ui.html to debug the related back-end API.
2.6. View application logs
We set s.yaml
for each service in logConfig:auto
, which means that the serverless-devs tool will automatically create a log store (LogStore) for the service, and all services share a log store. All logs of the application are output to . You can use the 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 logs of the mall-admin function; you can also use s mall-admin logs -t
to display the logs after the current time point in real-time in follow mode; you can also use s mall-admin logs --keyword=abc
view the logs containing the key Log with word abc. s logs are useful for you to understand how your service is running and diagnosing problems. For example, we execute s mall-admin logs -t
enter the follow mode, and then visit mall-admin
service in the browser to see the startup and request processing logs of the entire application.
2.7. Deploy the mall front-end project (optional)
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
and change the BASE_API to the mall-admin endpoint that was successfully deployed on Function Compute.
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. docker.sh
script in the project root directory to generate an image.
sudo bash docker.sh
Run the docker images
command and you will see that the mall/mall-admin-web image has been successfully generated. Push the image to the Alibaba Cloud image repository. In the same way, please cn-hangzhou
and quanxi-hryang
in the command below to your own image repository region and namespace.
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
s.yaml
project root directory, similar to deploying mall-admin, adjust access
and region
according to your configuration, and change image
to the mirror address that was successfully pushed in the previous step.
Execute s deploy
, when the deployment is successful, you can see the URL of the mall-admin-web service. Access through a browser and you will see the login page. Fill in the password macro123
to see the complete effect.
Note: The login page may report a timeout error for the first time due to a cold start. Just refresh the page again. We will optimize cold start performance in a later performance tuning article.
3. Summary
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 completely 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.
- Clone project code
- Find a VM, run the script to install MySQL, Redis and other dependent software with one click
host
application configuration, and fill in the value as the VM public network ip in step 2- Generate an application image and push it to the Alibaba Cloud Image Warehouse
- Deploy the application to the function computing platform
https://spring.io/projects/spring-boot
https://github.com/macrozheng/mall
http://serverless-devs.com/zh-cn/docs/installed/cliinstall.html
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。