Chapter 5. Developing and deploying a Spring Boot runtime application

In addition to using an example, you can create new Spring Boot applications from scratch and deploy them to OpenShift.

The recommended approach for specifying and using supported and tested Maven artifacts in a Spring Boot application is to use the OpenShift Application Runtimes Spring Boot BOM.

5.1. Developing Spring Boot application

For a basic Spring Boot application, you need to create the following:

  • A Java class containing Spring Boot methods.
  • A pom.xml file containing information required by Maven to build the application.

The following procedure creates a simple Greeting application that returns "{"content":"Greetings!"}" as response.

Note

For building and deploying your applications to OpenShift, Spring Boot 2.1.x only supports builder images based on OpenJDK 8 and OpenJDK 11. Oracle JDK and OpenJDK 9 builder images are not supported.

Prerequisites

  • Maven installed.
  • OpenJDK 8 or OpenJDK 11 installed.

Procedure

  1. Create a new directory myApp, and navigate to it.

    $ mkdir myApp
    $ cd myApp

    This is the root directory for the application.

  2. Create directory structure src/main/java/com/example/ in the root directory, and navigate to it.

    $ mkdir -p src/main/java/com/example/
    $ cd src/main/java/com/example/
  3. Create a Java class file MyApp.java containing the application code.

    package com.example;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @RestController
    public class MyApp {
    
        public static void main(String[] args) {
            SpringApplication.run(MyApp.class, args);
        }
    
        @RequestMapping("/")
        @ResponseBody
        public Message displayMessage() {
            return new Message();
        }
    
        static class Message {
            private String content = "Greetings!";
    
            public String getContent() {
                return content;
            }
    
            public void setContent(String content) {
                this.content = content;
            }
        }
    }
  4. Create a pom.xml file in the application root directory myApp with the following content:

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.example</groupId>
      <artifactId>my-app</artifactId>
      <version>1.0.0-SNAPSHOT</version>
    
      <name>MyApp</name>
      <description>My Application</description>
    
      <!-- Specify the JDK builder image used to build your application. -->
      <!-- Use OpenJDK 8 and OpenJDK 11-based images. OracleJDK-based images are not supported. -->
      <properties>
        <fabric8.generator.from>registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift:latest</fabric8.generator.from>
      </properties>
    
      <!-- Import dependencies from the RHOAR Spring Boot BOM. -->
      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>me.snowdrop</groupId>
            <artifactId>spring-boot-bom</artifactId>
            <version>2.1.6.SP3-redhat-00001</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>
    
      <dependencies>
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
          </dependency>
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
          </dependency>
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.1.6.RELEASE</version>
          </plugin>
        </plugins>
      </build>
    
      <!-- Specify the repositories containing RHOAR artifacts -->
      <repositories>
        <repository>
          <id>redhat-ga</id>
          <name>Red Hat GA Repository</name>
          <url>https://maven.repository.redhat.com/ga/</url>
        </repository>
      </repositories>
    
      <pluginRepositories>
        <pluginRepository>
          <id>redhat-ga</id>
          <name>Red Hat GA Repository</name>
          <url>https://maven.repository.redhat.com/ga/</url>
        </pluginRepository>
      </pluginRepositories>
    
    </project>
  5. Build the application using Maven from the root directory of the application.

    $ mvn spring-boot:run
  6. Verify that the application is running.

    Using curl or your browser, verify your application is running at http://localhost:8080.

    $ curl http://localhost:8080
    {"content":"Greetings!"}

Additional information

  • As a recommended practice, you can configure liveness and readiness probes to enable health monitoring for your application when running on OpenShift. To learn how application health monitoring on OpenShift works, try the Health Check example.

5.2. Deploying Spring Boot application to OpenShift

To deploy your Spring Boot application to OpenShift, configure the pom.xml file in your application and then use the Fabric8 Maven plugin. You can specify an OpenJDK image by replacing the fabric8.generator.from URL in the pom.xml file.

5.2.1. OpenJDK images for Red Hat Enterprise Linux

You can select the OpenJDK image used to build and deploy your application to OpenShift. OpenJDK images are available for RHEL 7 and RHEL 8 with OpenJDK 8 or OpenJDK 11.

Docker or podman authentication into the Red Hat Container Catalog is required to access RHEL 8 images. The authentication procedures are provided in the Red Hat Container Catalog links in the following table.

OSJDKRed Hat Container Catalog

RHEL 7

OpenJDK 8

RHEL 7 with OpenJDK 8

RHEL 7

OpenJDK 11

RHEL 7 with OpenJDK 11

RHEL 8

OpenJDK 8

RHEL 8 with OpenJDK 8

RHEL 8

OpenJDK 11

RHEL 8 with OpenJDK 11

Note

The use of a RHEL 8-based container on a RHEL 7 host, for example with OpenShift 3 or OpenShift 4, has limited support. For more information, see the Red Hat Enterprise Linux Container Compatibility Matrix.

5.2.2. Preparing Spring Boot application for OpenShift deployment

For deploying your Spring Boot application to OpenShift, it must contain:

  • Launcher profile information in the application’s pom.xml file.

In the following procedure, a profile with Fabric8 Maven plugin is used for building and deploying the application to OpenShift.

Prerequisites

Procedure

  1. Add the following content to the pom.xml file in the application root directory:

    ...
    
    <profiles>
        <profile>
          <id>openshift</id>
          <build>
            <plugins>
              <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>fabric8-maven-plugin</artifactId>
                <version>4.1.0</version>
                <executions>
                  <execution>
                    <goals>
                      <goal>resource</goal>
                      <goal>build</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>
            </plugins>
          </build>
        </profile>
    </profiles>
  2. Replace the fabric8.generator.from property in the pom.xml file to specify OpenJDK image.

    • RHEL 7 with OpenJDK 8

      <fabric8.generator.from>registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift:latest</fabric8.generator.from>
    • RHEL 7 with OpenJDK 11

      <fabric8.generator.from>registry.access.redhat.com/openjdk/openjdk-11-rhel7:latest</fabric8.generator.from>
    • RHEL 8 with OpenJDK 8

      <fabric8.generator.from>registry.redhat.io/openjdk/openjdk-8-rhel8:latest</fabric8.generator.from>
    • RHEL 8 with OpenJDK 11

      <fabric8.generator.from>registry.redhat.io/openjdk/openjdk-11-rhel8:latest</fabric8.generator.from>

5.2.3. Deploying Spring Boot application to OpenShift using Fabric8 Maven plugin

To deploy your Spring Boot application to OpenShift, you must perform the following:

  • Log in to your OpenShift instance.
  • Deploy the application to the OpenShift instance.

Prerequisites

  • oc CLI client installed.
  • Maven installed.

Procedure

  1. Log in to your OpenShift instance with the oc client.

    $ oc login ...
  2. Create a new project in the OpenShift instance.

    $ oc new-project MY_PROJECT_NAME
  3. Deploy the application to OpenShift using Maven from the application’s root directory. The root directory of an application contains the pom.xml file.

    $ mvn clean fabric8:deploy -Popenshift

    This command uses the Fabric8 Maven Plugin to launch the S2I process on OpenShift and start the pod.

  4. Verify the deployment.

    1. Check the status of your application and ensure your pod is running.

      $ oc get pods -w
      NAME                             READY     STATUS      RESTARTS   AGE
      MY_APP_NAME-1-aaaaa               1/1       Running     0          58s
      MY_APP_NAME-s2i-1-build           0/1       Completed   0          2m

      The MY_APP_NAME-1-aaaaa pod should have a status of Running once it is fully deployed and started.

      Your specific pod name will vary.

    2. Determine the route for the pod.

      Example Route Information

      $ oc get routes
      NAME                 HOST/PORT                                                     PATH      SERVICES        PORT      TERMINATION
      MY_APP_NAME         MY_APP_NAME-MY_PROJECT_NAME.OPENSHIFT_HOSTNAME      MY_APP_NAME      8080

      The route information of a pod gives you the base URL which you use to access it.

      In this example, http://MY_APP_NAME-MY_PROJECT_NAME.OPENSHIFT_HOSTNAME is the base URL to access the application.

    3. Verify that your application is running in OpenShift.

      $ curl http://MY_APP_NAME-MY_PROJECT_NAME.OPENSHIFT_HOSTNAME
      {"content":"Greetings!"}

5.3. Deploying Spring Boot application to stand-alone Red Hat Enterprise Linux

To deploy your Spring Boot application to stand-alone Red Hat Enterprise Linux, configure the pom.xml file in the application, package it using Maven and deploy using the java -jar command.

Prerequisites

  • RHEL 7 or RHEL 8 installed.

5.3.1. Preparing Spring Boot application for stand-alone Red Hat Enterprise Linux deployment

For deploying your Spring Boot application to stand-alone Red Hat Enterprise Linux, you must first package the application using Maven.

Prerequisites

  • Maven installed.

Procedure

  1. Add the following content to the pom.xml file in the application’s root directory:

      ...
      <!-- Specify target artifact type for the repackage goal. -->
      <packaging>jar</packaging>
      ...
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot.version}</version>
            <executions>
              <execution>
                  <goals>
                   <goal>repackage</goal>
                 </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      ...
  2. Package your application using Maven.

    $ mvn clean package

    The resulting JAR file is in the target directory.

5.3.2. Deploying Spring Boot application to stand-alone Red Hat Enterprise Linux using jar

To deploy your Spring Boot application to stand-alone Red Hat Enterprise Linux, use java -jar command.

Prerequisites

  • RHEL 7 or RHEL 8 installed.
  • OpenJDK 8 or OpenJDK 11 installed.
  • A JAR file with the application.

Procedure

  1. Deploy the JAR file with the application.

    $ java -jar my-project-1.0.0.jar
  2. Verify the deployment.

    Use curl or your browser to verify your application is running at http://localhost:8080:

    $ curl http://localhost:8080