Serverless Practices — Quickly Build a SpringBoot Application
By Tan Hehe (nicknamed Xiaomo)
Preface
First, let’s take a look at the definitions of some key terms used in this article:
Function Compute: is an event-driven service that allows users to write and upload code without having to manage server health or consider other factors. After Function Compute allocates required computing resources, it elastically runs your code. The fees of using these resources are billed based on their actual consumption. For more information about Function Compute, visit the official product page.
Fun: is a developer tool for serverless applications. It helps you manage resources such as Function Compute, API Gateway, and Log Service. You can use Fun to develop, build, and deploy resources by describing them in the template.yml file. For more information about Fun, click here.
ROS (Resource Orchestration Service): helps you simplify the management of cloud computing resources. By following the template specifications defined by ROS, you can define the collection of required cloud computing resources and their dependencies. ROS automatically creates and configures all resources to implement automated deployment and O&M. For more documents on ROS, visit the official product page.
Note: The techniques described in this article are only applicable to Fun 3.0.8 or later.
Environment Preparation
First, follow the Fun installation document to install Fun on your local computer.
After the installation is completed, run fun --version
to check that Fun has been successfully installed.
Quick Deployment
1. Initialize a project
Run fun init
to initialize a project.
fun init -n SpringBootDemo
After the initialization is completed, a directory named SpringBootDemo is added to the current directory structure.
The result is as follows:
2. Compile the project locally
Run fun build
to compile and build the project:
fun build
The result is as follows:
3. Run the project locally
Note: You can skip this step if you do not need to run the project locally.
Run fun local start
to run the FUN function locally.
Run the following command.
fun local start demo-springboot
The result is as follows:
4. Perform local single-step debugging
Note: You can skip this step if you do not need to debug the project locally.
Run fun local start
to perform local single-step debugging on the function.
Both VSCode and IDEA can run and debug Function Compute. For more information, see the VSCode plug-in serverless-vscode and the IDEA plug-in Cloud Toolkit.
Considering our audience, we will not use plug-ins for debugging here.
The result is as follows:
5. Package and deploy the project
Run fun deploy --use-ros
to deploy the project through ROS.
fun package --oss-bucket fun-local-test
fun deploy --use-ros --stack-name staging
Here, the --oss-bucket
name indicates the name of an OSS bucket that you can read and write.
--stack-name
indicates the environment where the project is deployed. You can create multiple development environments by setting different names for them, such as test, staging, and prod.
The following example shows how to quickly create a staging environment. The result is as follows:
After the test is completed, you can log on to the ROS console to delete the staging resource stack just created with a single click. After the deletion, all resources contained in this stack will also be deleted.