Using Spring Boot on OpenShift

Spring Boot is certainly an interesting framework for the development of microservices and other lightweight Java based applications.  I spent some time recently working with a plethora of Java microframeworks to determine which one I would like to use for some personal projects that I have been working on. Namely, I have tried out spark, WildFly Swarm, and Spring Boot.

This post is not to declare which one I like the best as that isn’t really relevant to why you are probably reading this post and depends greatly on the specific project.  I am not from the one framework to rule them all camp but more of use the right framework for the job.

That being said, in my exploration of these frameworks my goal was to be able to deploy them on OpenShift 3 using the recently released WildFly 10 application server.  I really enjoy using WildFly for my projects and the benefits provided from a full application server.  Therefore, this blog will focus on deploying a Spring Boot application packaged as a .war file to WildFly on OpenShift 3 as a docker based container.

Not what you are you looking for?  If you want to learn how to deploy a Spring Boot application to the current version of OpenShift Online, refer to the official Spring Boot documentation that provides details on how to do that.

 

First things first, how do I get OpenShift 3

You may be asking yourself how you get  your hands on OpenShift 3 at this point.  I am happy to report that the OpenShift team provides an up to date version of the upstream project that is very simple to install on your local machine.  If you don’t currently have access to an OpenShift 3 environment, point your browser to the following post I wrote that explains how to get it up and running in Windows, OS X, or linux:

https://blog.openshift.com/using-openshift-3-on-your-local-environment/

If you are not familiar with the basics of the OpenShift Container Application Platform, head on over to this post that walks through the project:

https://blog.openshift.com/openshift-3-walkthrough/

 

Step 1: Authenticate and create a new project

The first thing we need to do is authenticate to the OpenShift 3 web console and create a new project to hold all deployments related to our application.  In the spring boot world, this could include many containers running that same microservice all being load balanced.  Once you have authenticated to the platform, click on the New Project button as shown in the following image:

New Project

After you have clicked on the New Project button, you will be presented with a screen that allows you to enter in your project name, display name and description.  Enter in the following information:

  1. Name = springbootwildfly
  2. Display Name = Spring Boot
  3. Description = Project for my Spring Boot applications

Once you have entered in the above information, click on the Create button.

Name Project

Step 2: Using WildFly as the application server

After you have created your project, you will be dropped in the application creation wizard which will allow you to select the docker image to use as the basis for your application.  For this blog post, we will select to use the wildfly:10.0 builder image.

 

Select Base Image

This will use the extremely powerful S2I (Source to Image) based builders.

You may not be familiar with this awesome feature inside of OpenShift 3.  What you need to know is that the OpenShift team firmly believes that developers want to take advantage of all of the benefits that running applications in containers provides, but they don’t want to spend their day creating Dockerfiles or running docker builds while also having to do all of the orchestration by hand.  

In it’s simplest form, the S2I based docker images allows developers to interact with the platform from a pure source code perspective.  What this means is that OpenShift only needs to know the URL of your git repository and then the platform takes care of the rest.

Under the covers, when you create a new project and container, the platform matches a base image of the desired runtime up with your source, performs a build, and then creates a new docker image on the fly.  Pretty neat stuff.

 

Step 3: Adding the source code

After you select to use the WildFly builder image as your base, you will then be presented with the next screen in the create application wizard which will allow you to enter in your application name and your git source code repository URL that contains your application.  For this example, we are going to use a git repository that I have created that has the pom.xml file configured to include the Spring Boot framework as well as configured to create a .war archive as the artifact of the build.  That being said, if you want to work on your own source code, I suggest that you fork my repo and use that as a starting point for your Spring Boot application.

Enter in the following information on the Add to Project page:

 

  1. Name = bootwildfly
  2. Git Repository URL = https://github.com/gshipley/bootwildfly.git

Application Details

Finally, click on the Create button.

 

Step 4: Bask in the glory

After you click on the Create button, that magic of S2I will begin.  After a couple of minutes, you will have a Spring Boot application deployed inside of a docker container all orchestrated with Kubernetes.  While the build is running, feel free to watch the progress by looking at the build logs.  Once the build has completed, you will see one running container as shown in the following image:

Running Application

Now that our application is deployed, you can test it out by clicking on the exposed route that was created for you.  In the above image, the URL is:

http://bootwildfly-springbootwildfly.apps.10.2.2.2.xip.io/

To test out the rest endpoint, hit the following URL:

http://bootwildfly-springbootwildfly.apps.10.2.2.2.xip.io/hello
Step 5: Turn up the heat

One of the great benefits of using a platform designed specifically for containers and microservice deployments is the ability to quickly scale up your application components.  To illustrate this, go ahead and scale up to 5 containers by clicking on the up arrow next to the pod in the above image. Go ahead, try it now!

Wait a second, are you serious?  Indeed I am.  You were able to scale up to 5 containers in less than a few seconds.  That is some pretty awesome stuff huh?

Scaled

A note for command line users:

I know, I know.  You love the command line.  To perform all of the steps we did in this blog post on the command line, enter in the following commands:

 

$ oc new-project bootwildfly –display-name=’Spring Boot Applications’

$ oc new-app wildfly:10~https://github.com/gshipley/bootwildfly.git

$ oc scale dc bootwildfly –replicas=5

This post was great and all but what about those that want to use Spring Boot outside of an application server?  Fortunately, Jorge has you covered with his post on how to create your own Spring Boot builder image so you can run outside of a application server:

https://blog.openshift.com/using-openshift-enterprise-grade-spring-boot-deployments/
What’s next:

This post was intended to get you up and running with a scalable platform for your Spring Boot applications in a matter of minutes.  The next step if for you to take the base Spring Boot repository I have provided and create something great!

 

Categories
Java, News, OpenShift Container Platform, OpenShift Dedicated, OpenShift Online, OpenShift Origin
Tags
, , , , , ,
  • Misel Mesnjak

    Hi,

    Is it possible to use this tutorial to run Spring Boot Apps on Red Hat Cloud platform (Openshift Online)?

    • As of this writing, OpenShift Online is still version 2 and
      not using Docker and Kubernetes.

    • yes..you can run ..i have used java 1.7 and deployed on Apache tomcat 7 gear with mysql

      • Gowsik K C

        Hi I am trying to use openshift 3 and spring boot can u guide me or send me a link pls

  • Manuel Silva

    Thanks

  • Tore Gard

    Hi!

    You only need to add @SpringBootApplication in class Application

    @Configuration
    @ComponentScan
    @EnableAutoConfiguration
    @SpringBootApplication
    public class Application extends SpringBootServletInitializer {