Help Center> FunctionGraph> Best Practices> Building an HTTP Function with Spring Boot
Updated on 2023-10-11 GMT+08:00

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

  1. Open the Spring Boot project and click package in the Maven area to generate a JAR file.
    Figure 1 Generating a JAR file
  2. 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
  3. 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
  4. 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
    1. On the function details page, select a version and click Configure Test Event.
    2. On the Configure Test Event page, select the event template apig-event-template, and modify the path and pathParameters parameters in the template to construct a simple GET request.
    3. Click Create.
    4. 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.

  • Using an APIG trigger
    1. Create an APIG trigger by referring to Using an APIG Trigger. Set the authentication mode to None for debugging.
    2. Copy the generated URL, add the request parameter greeting?name=fg_user to the end of the URL (see Figure 3), and access the URL using a browser. The response shown in the following figure is displayed.
      Figure 3 Invoking the function

      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 4 Modifying the mapping address
      • Method 2: Click the trigger name to go to the APIG console, and delete the function name in the path.

FAQ

  1. 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.

  2. 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.

  3. 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.

  4. 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>