Deploy your SpringBoot applications on Openshift

Spring Boot is a new paradigm for developing Spring applications with more agility while focusing on your business methods rather than the thought of configuring Spring itself. In this tutorial we will learn how to deploy a Spring Boot application on Openshift

First of all, we will need a Spring Boot application to be deployed. Why not using JBoss Forge to code it in less than a minute? (BTW if you are using JBoss Developer Studio then you have it out of the box).

Start forge:

$ ./forge

Now create a simple SpringBoot application, from forge shell:

project-new --named demo-spring-boot --type microservice --top-level-package com.mastertheboss

Apart from the main application class, and Hello World REST controller will be created for us:

package com.mastertheboss;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {
    @Value("${welcome}")
    private String welcome;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String ipaddress() throws Exception {
        return "Reply: " + welcome;
    }
}

Now we need to have our project available in a public repository. For demo purposes, I have put this project on git hub: https://github.com/fmarchioni/mastertheboss/tree/master/spring/demo-spring-boot

Now start your Openshift and login:

$ oc login 

We will create a project named spring-boot-sample:

$ oc new-project spring-boot-sample

Now in order to build our project we will need an Image which is capable to build a Source code, through the Source to Image (S2I) project. if you don't want to build yourself an image capable to build your SpringBoot project there's already one available, named codecentric/springboot-maven3-centos

So let's add to our project the docker image codecentric/springboot-maven3-centos passing as source the demo-spring-boot application:

$ oc new-app codecentric/springboot-maven3-centos~https://github.com/fmarchioni/mastertheboss.git --context-dir=spring/demo-spring-boot

Please note that the context-dir has been used to create the application. This option allows us to build a project which is not in the root folder of your Git repository

Check your output, which will inform you that the Service is being created:

--> Found Docker image d1289f6 (12 days old) from Docker Hub for "codecentric/springboot-maven3-centos"

    Spring Boot Maven 3 
    ------------------- 
    Platform for building and running Spring Boot applications

    Tags: builder, java, java8, maven, maven3, springboot

    * An image stream will be created as "springboot-maven3-centos:latest" that will track the source image
    * A source build using source code from https://github.com/fmarchioni/mastertheboss.git will be created
      * The resulting image will be pushed to image stream "mastertheboss:latest"
      * Every time "springboot-maven3-centos:latest" changes a new build will be triggered
    * This image will be deployed in deployment config "mastertheboss"
    * Port 8080/tcp will be load balanced by service "mastertheboss"
      * Other containers can access this service through the hostname "mastertheboss"

--> Creating resources with label app=mastertheboss ...
    imagestream "springboot-maven3-centos" created
    imagestream "mastertheboss" created
    buildconfig "mastertheboss" created
    deploymentconfig "mastertheboss" created
    service "mastertheboss" created
--> Success
    Build scheduled, use 'oc logs -f bc/mastertheboss' to track its progress.
    Run 'oc status' to view your app.

Let's check that the service has been created:

$ oc get services
NAME            CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
mastertheboss   172.30.54.29   <none>        8080/TCP   16m

Last thing will be exposing our service externally:

$ oc expose service mastertheboss
route "mastertheboss" exposed

Here's your nice pod running on the console:

Spring Boot Openshift tutorial

You can click on it to check it's working: nothing fancy but it does work:

Spring Boot Openshift tutorial

 

Follow us on Twitter