tencent cloud

Serverless Cloud Function

Release Notes and Announcements
Release Notes
Announcements
User Guide
Product Introduction
Overview
Related Concepts
How It Works
Strengths
Scenarios
Related Products
Purchase Guide
Billing Overview
Billing Mode
Billable Items and Billing Modes
Function Computing Power Support
Free Tier
SCF Pricing
Billing Example
Payment Overdue
Getting Started
Creating Event Function in Console
User Guide
Quota Management
Managing Functions
Web Function Management
Log Management
Concurrence Management
Trigger Management
Function URL
A Custom Domain Name
Version Management
Alias Management
Permission Management
Running Instance Management
Plugin Management
Managing Monitors and Alarms
Network Configuration
Layer Management
Execution Configuration
Extended Storage Management
DNS Caching Configuration
Resource Managed Mode Management
Near-Offline Resource Hosting Model
Workflow
Triggers
Trigger Overview
Trigger Event Message Structure Summary
API Gateway Trigger
COS Trigger
CLS Trigger
Timer Trigger
CKafka Trigger
Apache Kafka Trigger
MQTT Trigger
Trigger Configuration Description
MPS Trigger
CLB Trigger Description
TencentCloud API Trigger
Development Guide
Basic Concepts
Testing a Function
Environment Variables
Dependency Installation
Using Container Image
Error Types and Retry Policies
Dead Letter Queue
Connecting SCF to Database
Automated Deployment
Cloud Function Status Code
Common Errors and Solutions
Developer Tools
Serverless Web IDE
Calling SDK Across Functions
Third-Party Tools
Code Development
Python
Node.js
Golang
PHP
Java
Custom Runtime
Deploying Image as Function
Web Framework Development
Deploying Framework on Command Line
Quickly Deploying Egg Framework
Quickly Deploying Express Framework
Quickly Deploying Flask Framework
Quickly Deploying Koa Framework
Quickly Deploying Laravel Framework
Quickly Deploying Nest.js Framework
Quickly Deploying Next.js Framework
Quickly Deploying Nuxt.js Framework
Quickly Deploying Django Framework
Use Cases
Overview
Solutions with Tencent Cloud Services
Business Development
TRTC Practices
COS Practices
CKafka Practice
CLS
CLB Practice
MPS
CDN
CDWPG
VOD
SMS
ES
Scheduled Task
Video Processing
Success Stories
Tencent Online Education
Online Video Industry
Tencent Online Education
Best Practice of Tencent IEG Going Global
API Documentation
History
Introduction
API Category
Making API Requests
Other APIs
Namespace APIs
Layer Management APIs
Async Event Management APIs
Trigger APIs
Function APIs
Function and Layer Status Description
Data Types
Error Codes
SDK Documentation
FAQs
General
Web Function
Billing FAQs
Network FAQs
Log FAQs
SCF utility class
Event Handling FAQs
API Gateway Trigger FAQs
Related Agreement
Service Level Agreement
Contact Us
Glossary
문서Serverless Cloud FunctionUse CasesBusiness DevelopmentImplementing Todo Application with Spring Boot and SCF

Implementing Todo Application with Spring Boot and SCF

PDF
포커스 모드
폰트 크기
마지막 업데이트 시간: 2024-12-02 16:35:34

Overview

Spring Boot is a framework provided by the Pivotal team to simplify the initial build and development of new Spring applications. It uses specific configuration methods, so you don't need to define template-based configuration items.
This document describes how to use Spring Boot through SCF to build a todo application. SCF provides event-triggered functions and HTTP-triggered functions (recommended in Spring Boot scenarios).

Prerequisites

You have prepared the development environment and tools as instructed in Notes on Java.

Directions

Using an HTTP-triggered function

SCF provides template functions. You can use an HTTP-triggered function as follows to quickly create a todo application and add, delete, modify, and query todos.
Note:
This template is for demonstration only. Todo data is actually stored in the instance cache instead of being persistently stored.

Creating a function

1. Log in to the SCF console.
2. Click Create on the Function Service page.
3. On the Create page, select Template and search for springboot and webfunc. In the search results, select SpringBootToDoApplication and click Next, as shown in the following figure:


4. Keep the default configuration and click Complete to complete the function creation.

Testing a function

On the Function Code tab, follow the steps below to initiate a simulated request based on the test template. In this way, you can try out the CRUD features of the todo application.
Query the todo list: Select GET as the request method, enter /todos for path, click Test, and you will see the current todos in the response body as shown below:



Add a todo: Select POST as the request method, enter /todos for path and {"key":"3","content":"Third todo","done": false} for body, and click Test to add a todo as shown below:



Delete a todo: Select DELETE as the request method, enter /todos/2 for path (to delete the todo whose key is 2 for example), and click Test as shown below:



Modify a todo: Select PUT as the request method, enter /todos/3 for path (to mark the todo whose key is 3 as completed for example), enter {"key":"3","content":"Third todo","done": true} for body, and click Test as shown below:




Sample codes

In the Creating a function step, you can also modify the function template based on your business needs. On the Create page, click Learn more in the upper-right corner of the selected template card. On the pop-up page, click Download Template Function to get the source code of the template function.
You can migrate a native Spring Boot project to an HTTP-triggered function as follows:
Make sure that the Spring listening port is 9000 (the specified listening port of the SCF HTTP-triggered function).

Compile the JAR package. Download the code and run the following compilation command in the Webfunc-Java8-SpringBoot directory:
gradle build
After the compilation, you can get the JAR package in the build/libs directory. Select the JAR package whose suffix is -all.
Prepare the executable file scf_bootstrap to start the web server. Below is the sample file content:
#!/bin/bash
/var/lang/java8/bin/java -Dserver.port=9000 -jar scf-springboot-java8-0.0.2-SNAPSHOT-all.jar
Note:
Run chmod 755 scf_bootstrap in the directory of the scf_bootstrap file to ensure that the file has the execution permission.
Package the scf_bootstrap file and the generated JAR package into a ZIP package and deploy the ZIP package to SCF as follows:
1.1 Log in to the SCF console.
1.2 Click Create on the Function Service page.
1.3 On the Create page, select Create from scratch and configure the following parameters: Function type: Select HTTP-triggered Function. Runtime environment: Select Java 8. Submitting method: Select Local ZIP file. Function codes: Click Upload and select the ZIP package.
1.4 Keep the default values for other parameters and click Complete to complete the function creation, as shown below:




Using an event-triggered function

SCF provides template functions. You can use an event-triggered function as follows to quickly create a todo application and add, delete, modify, and query todos.
Note:
This template is for demonstration only. Todo data is actually stored in the instance cache instead of being persistently stored.

Creating a function

1. Log in to the SCF console.
2. Click Create on the Function Service page.
3. On the Create page, select Template and search for springboot. In the search results, select SpringBoot and click Next.



4. Keep the default configuration and click Complete to complete the function creation.

Creating a trigger

Note:
If you have created an API Gateway trigger during function creation, simply check whether its configuration is the same as that described below.
1. After creating a function, on the Trigger management tab, click Create trigger.



2. Configure the following trigger parameters in the pop-up window, keep the default values for other parameters, and click Submit.
Trigger method: Select API Gateway trigger.
Integration response: Set to Enable.
3. After the creation, you need to adjust the API Gateway trigger parameters. Click API Name to enter the API Gateway console for subsequent operations, as shown below:



4. In the API Gateway console, find the API used by the function and click Edit in the Operation column, as shown below:



5. On the Frontend Configuration page, change the value of path to /todos, click Complete immediately, and release the service as prompted, as shown below:



Testing a function

On the Function Code tab, follow the steps below to initiate a simulated request based on the Api Gateway event template. In this way, you can try out the CRUD features of the todo application.
Query the todo list: Select GET as the request method, enter /todos for path, click Test, and you will see the current todos in the response body as shown below:



Add a todo: Select POST as the request method, enter /todos for path , Content-Type: application/jsonfor headers and {"key":"3","content":"Third todo","done": false} for body, and click Test to add a todo as shown below:


Delete a todo: Select DELETE as the request method, enter /todos/2 for path (to delete the todo whose key is 2 for example), and click Test as shown below:


Modify a todo: Select PUT as the request method, enter /todos/2 for path (to mark the todo whose key is 2 as completed for example), enter {"key":"2","content":"Third todo","done": true} for body, and click Test as shown below:




Sample codes

In the Creating a function step, you can also modify the function template based on your business needs. On the Create page, click Learn more in the upper-right corner of the selected template card. On the pop-up page, click Download Template Function to get the source code of the template function.
You can configure as follows:
Add the ScfHandler class, which is used to receive event triggers and forward messages to the Spring service. After receiving a response from the Spring service, the function will return the response to the invoker. The project structure after the ScfHandler class is added is shown below:
.
├── src
| └── main
| | ├── java
| | | └── com.tencent.scfspringbootjava8
| | | | ├── controller
| | | | ├── model
| | | | └── repository
| | | | | ├── ScfHandler.java
| | | | | └── ScfSpringbootJava8Application.java
| | └── resources
Compile the JAR package Download the code and run the following compilation command in the root directory:
gradle build
After the compilation, you can get the JAR package in the build/libs directory. Select the JAR package whose suffix is -all.
Deploy the generated JAR package to SCF as follows:
1.1 Log in to the SCF console.
1.2 Click Create on the Function Service page.
1.3 On the Create page, select Create from scratch and configure the following parameters: Function type: Select Event-triggered function. Runtime environment: Select Java 8. Submitting method: Select Local ZIP file. Execution: Enter com.tencent.scfspringbootjava8.ScfHandler:: mainHandler. Function codes: Click Upload and select the ZIP package.
1.4 Keep the default values for other parameters and click Complete to complete the function creation, as shown below:




도움말 및 지원

문제 해결에 도움이 되었나요?

피드백