How to run your Spring Boot application on OpenShift?

05.10.2015 10:41:29

How to run your Spring Boot application on OpenShift? Spring Boot documentation is currently outdated, so I created this howto. Configuration files use "openshift" Spring and Maven profile. I think that it's very useful. If you don't use Spring and/or Maven profiles, you can leave the files as it is. If you have differently named profiles (like "prod" or "Production" etc.), just simply change the files.

First create a DIY application:

rhc app create OPENSHIFT_APPLICATION_NAME diy-0.1

You will get GIT_REMOTE_URL.

Copy to your application .openshift directory and call:

git remote add openshift -f GIT_REMOTE_URL

Add these files:

.openshift/action_hooks/build:

#!/bin/bash
cd $OPENSHIFT_REPO_DIR
mvn clean package -P openshift -s .openshift/settings.xml -DskipTests=true

.openshift/action_hooks/start:

#!/bin/bash
cd $OPENSHIFT_REPO_DIR
nohup java -jar -Dspring.profiles.active=openshift target/*.jar --server.port=${OPENSHIFT_DIY_PORT} --server.address=${OPENSHIFT_DIY_IP} &

.openshift/action_hooks/stop:

#!/bin/bash
source $OPENSHIFT_CARTRIDGE_SDK_BASH
PID=$(ps -ef | grep java.*\.jar | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
    client_result "Application is already stopped"
else
    kill $PID
fi

.openshift/settings.xml:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
	<localRepository>$OPENSHIFT_DATA_DIR</localRepository>
</settings>

And finally call:

git update-index --chmod=+x .openshift/action_hooks/build
git update-index --chmod=+x .openshift/action_hooks/start
git update-index --chmod=+x .openshift/action_hooks/stop
git add .
git commit -m "moved to openshift"
git push -f openshift master:master

EDIT: Just made a GitHub repo with a sample application which contains all these steps: https://github.com/jirkapinkas/spring-boot-openshift




© Jiri Pinkas | this project on GitHub | related: JavaVids | Maven Central Repository Search | Java školení | Top SQL Blogs | monitored using: sitemonitoring

Top Java Blogs is a Java blog aggregator (with English-written blogs only) focused on Java SE, Java EE, Spring Framework and Hibernate.