Deploying Functions with Spring Cloud Function: A Hands‑On Guide
This tutorial walks through using Spring Cloud Function to deploy JAR‑based functions with independent classloaders, covering dependency setup, simple and Spring Boot JAR examples, command‑line configuration, multiple function handling, and component‑scan alternatives, all illustrated with code snippets and console screenshots.
Environment: SpringBoot 2.7.16 + SpringCloud 2021.0.8
1. Introduction
Spring Cloud Function provides a "deployer" library that lets you launch JAR files (or exploded archives, or a set of JARs) with an isolated classloader and expose the functions defined inside. This is powerful for adapting functions to various input/output adapters without modifying the target JAR, and it is used by many serverless platforms.
2. Practical Example
To use the library, add the following Maven dependency:
<code><dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-deployer</artifactId>
<version>${spring.cloud.function.version}</version>
</dependency></code>With this dependency, your program can load third‑party JARs via an independent classloader and expose any Function interfaces as services.
2.1 Introductory Example
The user must provide the spring.cloud.function.location property, which points to the JAR URL (or a Maven coordinate). If the JAR starts successfully, the function is registered in the main application's FunctionCatalog and can be invoked.
<code>@SpringBootApplication
public class SpringcloudFunctionDeployerApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(DeployFunctionDemo.class,
"--spring.cloud.function.location=E:/functions/pack-function.jar",
"--spring.cloud.function.function-class=functions.LengthFunction");
FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
Function<String, Integer> function = catalog.lookup("lengthFunction");
System.out.println(function.apply("hello"));
}
}</code>Running the program prints 5 to the console.
The two parameters can also be supplied via the command line.
2.2 Simple JAR (no Spring dependencies)
Assume the JAR contains the following class:
<code>public class LengthFunction implements Function<String, Integer> {
@Override
public Integer apply(String t) {
return t == null ? 0 : t.length();
}
}</code>Package this class into a JAR and start the Spring Cloud Function project with the JAR location and the specific function class:
<code>--spring.cloud.function.location=E:/functions/pack-function.jar
--spring.cloud.function.function-class=functions.LengthFunction</code>The console shows the function name lengthFunction and it can be accessed.
If you define multiple functions, separate them with a semicolon:
<code>--spring.cloud.function.location=E:/functions/pack-function.jar
--spring.cloud.function.function-class=functions.LengthFunction;functions.UppercaseFunction</code>The console then lists two recognized functions.
When many functions exist, specifying spring.cloud.function.function-class becomes cumbersome. Spring Cloud Function also supports component scanning; the package name must be functions , eliminating the need for the function-class property.
2.3 SpringBoot JAR
This packaging option includes Spring Boot dependencies, producing a Spring Boot JAR. Because the deployed JAR runs in an isolated classloader, it does not conflict with the Spring Boot version used by the deployer.
Write a simple SpringBoot program that defines the required function, for example:
<code>@Configuration
public class FunctionConfig {
@Bean
Function<String, String> uppercase() {
return value -> value.toUpperCase() + " - ";
}
}</code>Package the JAR and configure the deployer as follows:
<code>--spring.cloud.function.location=E:/functions/zzz-pack-springboot-functions-1.0.0.jar
--spring.cloud.function.definition=uppercase</code>After starting, two applications are launched—one using SpringBoot 2.x and the other 3.x—without any version conflict.
Testing the /uppercase endpoint returns the expected transformed string.
All content above constitutes the complete tutorial.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.