Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
On this page
Help Center/ FunctionGraph/ Best Practices/ Building an HTTP Function with Spring Boot

Building an HTTP Function with Spring Boot

Updated on 2024-11-15 GMT+08:00

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
    NOTE:

    The Java runtime environment can be directly invoked in the function, and no additional installation is required.

  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, and modify the path and pathParameters parameters in the template to construct a simple GET request.
      Figure 3 Configuring a test event
    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.

      Figure 4 Viewing the returned result
  • 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 5), and access the URL using a browser. The response shown in the following figure is displayed.
      Figure 5 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 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.

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.

    Figure 7 Accessing LTS for log analysis
  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>

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback