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.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Deploying Functions with Spring Cloud Function: A Hands‑On Guide

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:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-function-deployer</artifactId>
  <version>${spring.cloud.function.version}</version>
</dependency>

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.

@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"));
  }
}

Running the program prints 5 to the console.

The two parameters can also be supplied via the command line.

Console output
Console output

2.2 Simple JAR (no Spring dependencies)

Assume the JAR contains the following class:

public class LengthFunction implements Function<String, Integer> {
  @Override
  public Integer apply(String t) {
    return t == null ? 0 : t.length();
  }
}

Package this class into a JAR and start the Spring Cloud Function project with the JAR location and the specific function class:

--spring.cloud.function.location=E:/functions/pack-function.jar
--spring.cloud.function.function-class=functions.LengthFunction

The console shows the function name lengthFunction and it can be accessed.

Function registration
Function registration

If you define multiple functions, separate them with a semicolon:

--spring.cloud.function.location=E:/functions/pack-function.jar
--spring.cloud.function.function-class=functions.LengthFunction;functions.UppercaseFunction

The console then lists two recognized functions.

Multiple functions
Multiple 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:

@Configuration
public class FunctionConfig {
  @Bean
  Function<String, String> uppercase() {
    return value -> value.toUpperCase() + " - ";
  }
}

Package the JAR and configure the deployer as follows:

--spring.cloud.function.location=E:/functions/zzz-pack-springboot-functions-1.0.0.jar
--spring.cloud.function.definition=uppercase

After starting, two applications are launched—one using SpringBoot 2.x and the other 3.x—without any version conflict.

Multiple SpringBoot versions
Multiple SpringBoot versions

Testing the /uppercase endpoint returns the expected transformed string.

Endpoint test
Endpoint test

All content above constitutes the complete tutorial.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Function as a Servicespring-cloud-function
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.