Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Migrate Spring boot project to Serverless in three minutes

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Preface

Spring Boot has become the most popular Java back-end development framework. A typical application method is to buy a virtual machine on the cloud and run Java programs on it 24 hours a day. In this case, users must maintain their virtual machine environment and pay for it on a monthly basis.

Huawei Cloud FunctionGraph (functional Workflow Service) has the characteristics of zero OPS and low-cost computing. FunctionGraph runs the code on demand, and there is no need to configure and manage the host. You only need to pay for each 100ms and number of times the code is executed. If the code is not run, there will be no charge, and there is more free quota every month.

FunctionGraph has obvious cost and maintenance advantages, but how can a standard Spring Boot application be run on FunctionGraph as a function? Now let's take a local SpringBoot project (link https://functionstage-examples.obs.cn-north-1.myhwclouds.com/ServerlessSpringBootDemo.zip) as an example to demonstrate the process of rapid migration to Huawei Cloud FunctionGraph.

Preparatory work

Download ServerlessSpringBoot2-1.0.0.jar (link https://functionstage-examples.obs.cn-north-1.myhwclouds.com/ServerlessSpringBoot2-1.0.0.jar).

Migration proc

01. Making function zip package

Add the fgs.properties configuration file according to the above diagram, add two configuration items fgs.component-scan and fgs.mapper-scan, and then import the package.

The resulting ServerlessSpringBootDemo.zip is the final function code package.

02. Create function

Find the FunctionGraph service at Huawei Cloud Portal, and select the create function. It is recommended to set the function name to the root path in Controller, such as webtest in this example, select Java8, set the function execution entry to com.huawei.fgs.ext.handler.Main.handler, and choose zip package to upload code (or you can pass the code to OBS bucket first and create it using OBS upload method), and the function is created successfully.

03. Create an APIG trigger

After the function is created, the modified memory is 1024, the modification timeout is 30 (the first startup time is long) and saved. Next, switch to the trigger tab, click create trigger, select APIG, change the security authentication to NONE, set the backend timeout to 30000, which is consistent with the function timeout, and click OK to complete the creation.

Test results

Access the URL generated by APIG directly in the browser, because there is no RequestMapping matching the / webtest path in the Controller in demo, so the path cannot be found at first, but you can see the effect after slight modification:

Matters needing attention

1. When using the AOP feature of SpringBoot, do not define the section to the Controller layer, otherwise it will make it impossible to use

2. At present, Controller will be regarded as RestController, and all APIs will be returned in the form of ResponseBody. Html pages are not supported for the time being.

3. Remove the server.port configuration from the application.properties and add the spring.main.web-environment=false configuration item to slightly improve the first startup speed.

4. If the code needs to be changed frequently, package all dependent packages into a zip, upload them to OBS, enter the address of the dependent package when creating the function, and then upload a small jar package when updating the code later.

5. If filter is used in the business code, the code needs to be modified, and the specific method will be provided later. (there are simple usage examples in this demo, which rely on FunctionGraph's Java SDK (link Runtime-1.1.0.jar and ServerlessSpringBoot2-1.0.0.jar in https://functionstage-sdk.obs.myhwclouds.com/java-sdk/fss-java-sdk-1.1.0.zip)).

6. If you need to use the code of this demo, first change the mysql information in application.properties to your own public network access configuration:

In addition, create users table and books table in the database.

The users table is structured as follows:

The books table is structured as follows:

Summary

To sum up, the whole migration process is very simple. Users do not need to modify their own business code, but only need to add a fgs.properties file under the resources directory. The package guide process is slightly different from the normal situation, and it can be completed in a few seconds according to the above steps. After the functions and triggers are finally created, the whole process is completed. For more information about the API gateway, you can click here to experience.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report