Difficulty: beginner
Estimated Time: 15 minutes

In this scenario you will learn more about developing Spring Boot applications using the Red Hat OpenShift Application Runtimes platform. We will be building a simple Spring Boot Web Application.

How is Spring Boot supported on OpenShift?

Spring is one of the most popular Java Frameworks and offers an alternative to the Java EE programming model. Spring is also very popular for building applications based on Microservices Architecture due to its support for packaging applications into self-contained, deployable JAR files. For web applications Spring Boot can embed a Servlet Container like Apache Tomcat to create a completely self-sufficient JAR file which can be deployed and run without any external tools (other than the Java Runtime of course!). These bootable applications (sometimes also called fat JARs or über JARs) fit the container model very well since in a container platform like OpenShift responsibilities like starting, stopping and monitoring applications are then handled by the container platform instead of an Application Server. And since you are deploying a single JAR file the container definition is also much simpler.

Red Hat tests and certifies a certain version of Spring and Spring Boot for usage on the OpenShift platform as part of Red Hat OpenShift Application Runtimes. Red Hat also provides full support for Apache Tomcat, Hibernate and Apache CXF (for REST services) when used in a Spring Boot application on OpenShift Application Runtimes.

In this scenario you learned about the basics of Spring Boot. We will add additional scenarios that explain more how to build Spring Boot applications on OpenShift shortly, so check back to learn.openshift.com

Spring Boot is a powerful and easy to use framework for developing everything from monolithic applications to microservices, but Spring Boot becomes every better when we package it in a container and use an orchestrated platform like OpenShift. When using OpenShift we can, in a very consistent and fast way, deploy our Spring application to multiple environments without worry of environment differences. OpenShift also enables things like Deployment Pipelines and Blue/Green deployments but that was not covered in this scenario.

Additional Resources

More background and related information on Red Hat OpenShift Application Runtimes and Eclipse Vert.x can be found here:

  • Red Hat OpenShift Application Runtimes for Developers - Here you can get started with a project using different boosters and clone that project to your local computer. This also enables you to deploy your application on your own private OpenShift Container Platform or use OpenShift Online that is provided for free from Red Hat.
  • Project Snowdrop homepage - This site has a lot of details of the work that Red Hat is doing to make Spring work great on Kubernetes and OpenShift.

Don’t stop now! The next scenario will only take about 10 minutes to complete.

Getting Started with Spring Boot on RHOAR

Step 1 of 3

Step 1

Review the base structure of the application

For your convenience, this scenario has been created with a base project using the Java programming language and the Apache Maven build tool.

Initially the project is almost empty and doesn't do anything. Start by reviewing the base project's content by executing a tree in your terminal.

The output should look something like this:

.
├── pom.xml
└── src
    ├── main
    │   ├── fabric8
    │   │   ├── deployment.yml
    │   │   └── route.yml
    │   ├── java
    │   │   └── com
    │   │       └── example
    │   │           └── Application.java
    │   └── resources
    │       └── static
    │           └── index.html
    └── test
        └── java
            └── com
                └── example
                    └── ApplicationTests.java

Except for the fabric8 directory and the index.html, this matches what you would get if you generated an empty project using the Spring Initializr. For the moment you can ignore the content of the fabric8 folder (we will discuss this later).

One thing that differs slightly is the pom.xml file.

As you review the content, you will notice that there are a couple TODO comments. Do not remove them! These comments are used as markers for later exercises in this scenario.

Notice that we are not using the default BOM (Bill of material) that Spring Boot projects typically use. We are using a BOM provided by Red Hat as part of the Snowdrop project instead. We use this BOM to make sure that we are using only the dependency versions supported by Red Hat.

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>me.snowdrop</groupId>
        <artifactId>spring-boot-bom</artifactId>
        <version>${spring-boot.bom.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

1. Adding Spring Boot Web to the application

Since our application will be a web application we need to use a servlet container like Apache Tomcat or Undertow to handle incoming connections from clients. Since Red Hat offers support for Apache Tomcat (e.g., security patches, bug fixes, etc.) we will use it here.

NOTE: Undertow is another an open source project that is maintained by Red Hat and therefore Red Hat plans to add support for Undertow shortly.

To add these dependencies to our project, add the following lines in the pom.xml (If you left the TODO comments in you can click the Copy to Editor button to do this automatically)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

There are other dependencies brought in by this Spring Starter POM. For this scenario we are mostly focusing on bringing in the necessities for a simple web page. We will dive into this dependency in more detail in the next couple modules.

You may notice that there is no <version> tag here. That's because the version numbers are managed and automatically derived by the BOM we included earlier.

2. Test the application locally

As we develop the application we want to be able to test and verify our change at different stages. One way we can do that locally is by using the spring-boot maven plugin.

Run the application by executing the following command:

mvn spring-boot:run

NOTE: The Katacoda terminal window is like your local terminal. Everything that you run here you should be able to execute on your local computer as long as you have Java SDK 1.8 and Maven installed. In later steps, we will also use the oc command line tool for OpenShift commands.

3. Verify the application

To begin with click on the Local Web Browser tab in the console frame of this browser window which will open another tab or window of your browser pointing to port 8080 on your client. Or use this link.

You should now see an HTML page with the Welcome to Spring Boot welcome message. If you see this then you've successfully set up the application! If not check the logs in the terminal. Spring Boot adds a couple helper layers to catch common errors and print helpful messages to the console so check for those first.

4. Stop the application

Before moving on, click in the terminal window and then press CTRL-C to stop the running application!

Congratulations

You have now successfully executed the first step in this scenario. In the next step we will connect to OpenShift to deploy our application. This example is extremely simple as it is meant to only introduce you to Spring Boot and RHOAR.

Terminal
Local Web Browser
OpenShift Console