Building an HTTP Function with Spring Boot
Introduction
This chapter describes how to deploy services on FunctionGraph using Spring Boot.
Usually, you may build Spring Boot applications using SpringInitializr or IntelliJ IDEA. This chapter uses the Spring.io project in https://spring.io/guides/gs/rest-service/ as an example to deploy an HTTP function on FunctionGraph.
Procedure
To deploy an existing project to FunctionGraph, change the listening port of the project to 8000, and create a file named bootstrap in the same directory as the JAR file to include the command for executing the JAR file.
In this example, a Maven project created using IntelliJ IDEA is used.
Building a Code Package
- Open the Spring Boot project and click package in the Maven area to generate a JAR file.
Figure 1 Generating a JAR file
- Set the web port to 8000 (do not change this port) using the application.properties file or specify the port during startup. HTTP functions only support this port.
Figure 2 Configuring port 8000
- Create a file named bootstrap in the same directory as the JAR file, and enter the startup parameters.
/opt/function/runtime/java11/rtsp/jre/bin/java -jar -Dfile.encoding=utf-8 /opt/function/code/rest-service-complete-0.0.1-SNAPSHOT.jar
The Java runtime environment can be directly invoked in the function, and no additional installation is required.
- Compress the JAR file and bootstrap file into a ZIP package.
Creating an HTTP Function and Uploading Code
Create an HTTP function and upload the ZIP file. For details, see Creating an HTTP Function.
Verifying the Result
- Using a test event
- On the function details page, select a version and click Configure Test Event.
- On the Configure Test Event page, select the event template, and modify the path and pathParameters parameters in the template to construct a simple GET request.
Figure 3 Configuring a test event
- Click Create.
- Click Test to obtain the response.
When debugging a function, increase the memory size and timeout, for example, increase them to 512 MB and 5s.
Figure 4 Viewing the returned result
- Using an APIG trigger
- Create an APIG trigger by referring to Using an APIG Trigger. Set the authentication mode to None for debugging.
- Copy the generated URL, add the request parameter greeting?name=fg_user to the end of the URL (see Figure 5), and access the URL using a browser. The response shown in the following figure is displayed.
The default APIG trigger URL is in the format "Domain name/Function name". In this example, the URL is https://your_host.com/springboot_demo, where the function name springboot_demo is the first part of the path. If you send a GET request for https://your_host.com/springboot_demo/greeting, the request address received by Spring Boot contains springboot_demo/greeting. If you have uploaded an existing project, you cannot access your own services because the path contains a function name. To prevent this from happening, use either of the following methods to annotate or remove the function name:
- Method 1: Modify the mapping address in the code. For example, add the first part of the default path to the GetMapping or class annotation.
Figure 6 Modifying the mapping address
- Method 2: Click the trigger name to go to the APIG console, and delete the function name in the path.
- Method 1: Modify the mapping address in the code. For example, add the first part of the default path to the GetMapping or class annotation.
FAQ
- What Directories Are Accessible to My Code?
An uploaded code package is stored in the /opt/function/code/ directory of the function (runtime environments, compute resources, or containers). However, the directory can only be read and cannot be written. If some data must be written to the function during code running and logged locally, or your dependency is written by default to the directory where the JAR file is located, use the /tmp directory.
- How Are My Logs Collected and Output?
Function instances that have not received any requests during a specific period of time will be deleted together with their local logs. You will be unable to view the function logs during function running. Therefore, in addition to writing logs to your local host, output logs to the console by setting the output target of Log4j to System.out or by using the print function.
Logs output to the console will be collected. If you have enabled LTS, the logs will also be stored in LTS for near real-time analysis.
Suggestion: Enable LTS, and click Go to LTS to view and analyze logs on the Real-Time Logs tab page.
Figure 7 Accessing LTS for log analysis
- What Permissions Does My Code Have?
Similar to common event functions, code does not have the root permission. Code or commands requiring this permission cannot be executed in HTTP functions.
- How Do I Package Spring Boot Projects of Multiple Modules?
Configure the following to package these Spring Boot projects.
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.YourServiceMainClass</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot