Implementation to Containerize Spring WebFlux Application

Below is the implementation to Containerize the Spring WebFlux Application.

Step 1: Create the WebFlux Application

Create the Spring project using spring initializer and add the below dependencies.

Dependencies:

  • Spring Web Reactive
  • Lombok
  • Spring DevTools

After creating the project, the structure will be like below:


Step 2: Configure the Application properties

Open application.properties file and add the below necessary configurations.

spring.application.name=webflux-Containerization
server.port=8080


Step 3: Create the HelloService class

Go to src > main > java > org.example.webfluxcontainerization > service > HelloService and put the below code.

Java
package org.example.webfluxcontainerization.service;

import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

@Service
public class HelloService {
    public Mono<String> getHelloMessage() {
        return Mono.just("Hello, Spring WebFlux!");
    }
}


Step 4: Create the HelloController class

Go to src > main > java > org.example.webfluxcontainerization > controller > HelloController and put the below code.

Java
package org.example.webfluxcontainerization.controller;

import org.example.webfluxcontainerization.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    public Mono<String> sayHello() {
        return helloService.getHelloMessage();
    }
}


Step 5: Main Class

No changes are required in the main class.

Java
package org.example.webfluxcontainerization;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebfluxContainerizationApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebfluxContainerizationApplication.class, args);
    }

}


Step 6: Create the Docker file

The Docker file is the script that contains the instructions to build the Docker image.

FROM openjdk:17-jdk-alpine
LABEL authors="User"

EXPOSE 8080

WORKDIR /app
COPY target/webflux-Containerization-0.0.1-SNAPSHOT.jar /app/webflux-Containerization-0.0.1-SNAPSHOT.jar

ENTRYPOINT ["java", "-jar","webflux-Containerization-0.0.1-SNAPSHOT.jar"]


pom.xml:

XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.example</groupId>
    <artifactId>webflux-Containerization</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>webflux-Containerization</name>
    <description>webflux-Containerization</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>

    </build>

</project>

Step 7: Build the JAR file

./mvnw clean package

Output:


Build the Docker image:

docker build -t my-spring-demo .

Output:


Run the docker file:

docker run -p 8080:8080 my-spring-demo

Output:


Access the application by using the below URL.

GET http://localhost:8080/hello

Output:




Containerize Spring WebFlux Application

In today’s Web development, containerization has become a dominant way of deploying and managing applications. The containers package application provides a consistent environment in the development and production phases. Docker is a popular tool for building, deploying, and running applications using containers. In this article, we will discuss how to containerize a Spring WebFlux application using Docker.

Containerization requires the creation of an isolated environment containing everything necessary to run the application, including code, libraries, runtime, and environment variables Docker does this by representing small packages of known executables as an image that includes everything needed to run a piece of software.

Key Terminologies:

  • Container: A small, stand-alone, and deployable piece of software that can include everything necessary to make application code, runtimes, libraries, and settings work
  • Docker: A platform for developing, shipping, and running applications in containers. It can automate applications in small containers.
  • Docker File: A text file can contain instructions for creating a Docker image. It can define the base image, the files copied to the working directory, and the command to run the application.

Steps to Containerize Spring WebFlux Application

  1. Create the Spring WebFlux Application: First, we can develop the simple webflux application using spring boot and WebFlux.
  2. Package the Application: We can use the Maven to build the JAR file of the application.
  3. Create the Docker file: Define the steps to the package the application into the Docker image.
  4. Build the Docker Image: We can use the Docker CLI to build the image from the Docker file.
  5. Run the Docker Container: We can use the Docker CLI to run the container from the image.

Similar Reads

Implementation to Containerize Spring WebFlux Application

Below is the implementation to Containerize the Spring WebFlux Application....