by Rafał Borowiec

Menu

Friday, February 20, 2015

OpenShift DIY cartridge is a great way to test unsupported languages on OpenShift. But it is not scalable (you can vote for Scalable DIY cartridge here) which makes it hard to use with production grade Spring Boot applications. But what if we deployed Spring Boot application to WildFly Application Server? Spring Boot can run with embedded servlet container like Tomcat or much faster Undertow, but it also can be deployed to a standalone application server. This would mean that it can be also deployed to WildFly application server that is supported by OpenShift. Let’s see how easy is to get started with creating a Spring Boot application from scratch and deploy it to WildFly 8.2 on OpenShift.

Note: While browsing OpenShift documentation one can think that on WildFly 8.1 and Java 7 is supported (as of time of writing this blog post). But this is fortunately not true anymore: WildFly 8.2 and Java 8 will work fine and it is default in fact!. This was the first time when I was happy about documentation being outdated.

Prerequisite

Before you can start building the application, you need to have an OpenShift free account and client tools (rhc) installed.

Create WildFly application

To create a WildFly application using client tools, type the following command:

rhc create-app boot jboss-wildfly-8 --scaling

jboss-wildfly-8 cartridge is described as WildFly Application Server 8.2.0.Final. Scaling option is used as it will be impossible to set it later (vote here)

When the application is created you should see username and password for an administration user created for you. Please store these credentials to be able to login to the WildFly administration console.

Template Application Source code

OpenShift creates a template project. The project is a standard Maven project. You can browse through pom.xml and see that Java 8 is used by default for this project. In addition, there are two non standard folders created: deployments, that is used to put the resulting archive into, and .openshift with OpenShift specific files. Please note .opensift/config. This is the place where WildFly configuration is stored.

Spring Boot dependencies

As the dependency management Spring IO Platform will be used. The main advantage of using Spring IO Platform is that it simplifies dependency management by providing versions of Spring projects along with their dependencies that are tested and known to work together. Modify the pom.xml by adding:

Configure the application

Initialize Spring Boot Application

Having all dependencies, we can add application code. Create Application.java in demo package. The Application class’s work is to initiate Spring Boot application, so it must extend from SpringBootServletInitializer and be annotated with @SpringBootApplication

@Entity, @Repository, @Controller

Spring Data JPA, part of the larger Spring Data family, makes it easy to easily implement JPA based repositories. For those who are not familiar with the project please visit: http://projects.spring.io/spring-data-jpa/.

Domain model for this sample project is just a Person with some basic fields:

With the domain model in place some test data can be handy. The easiest way is to provide a data.sql file with the SQL script to be executed on the application start-up.

Create src/main/resources/data.sql containing initial data for the people table (see below). Spring Boot will pick this file and run against configured Data Source. Since the Data Source used is connecting to H2 database, the proper SQL syntax must be used:

spring.jpa.hibernate.ddl-auto=create-drop: create structure of the database based on the provided entities.

Deploying to OpenShift

The application is ready to be pushed to the repository. Commit your local changes and then push it to remote:

git push

The initial deployment (build and application startup) will take some time (up to several minutes). Subsequent deployments are a bit faster. You can now browse to: http://appname-yournamespace.rhcloud.com/ and you should see the form:

Please note maven.compiler.executable was removed. Don’t forget to change the @Controller’s code and make it Java 7 compatible.

Summary

In this blog post you learned how to configure basic Spring Boot application and run it on OpenShift with WildfFly 8.2 and Java 8. OpenShift scales the application with the web proxy HAProxy. OpenShift takes care of automatically adding or removing copies of the application to serve requests as needed.

Finally, after a lot of issues it works. So idf you want to deploy the appication on openshift and to be scalable do exactly what this tutorial said, and delete the maven.compiler.executable from pom.xml .

But whenyou create the application, for me it only works with wildfly 8.2.1.Final. With other versions it didn't work for me. Thanks for this awesome tutorial :) !!!