Deploying a Spring Boot Application to IBM Bluemix

Last Update: 19.06.2017. By Jens in Spring Boot

This post is the first part of a series of how to deploy a Spring Boot application to the cloud. We’ll start with IBM Bluemix.

The Sample App

As the sample application, we are going to use the comment-store app from my first Spring Boot book.

It is a simple microservice for handling CRUD operations for storing comments. The comments are stored in a SQL DB and the CRUD operations are partly offered in an REST-like API.

We’ll use Postgres for the tutorial.

Changes I made prior to deployment

I use the already updated version using Spring Boot 2 Milestone 1. You can find it in the with-spring-boot-2m branch.

The sample application in the book used to load a file with spam words directly from the disk. For the cloud deployment, I switched it to loading the file via the Classloader.

So, in the SimpleSpamDetector in the spam-detection submodule, I changed the constructor to

public SimpleSpamDetector(String filename) throws IOException {
    this.spamWords = IOUtils.readLines(getClass().getResourceAsStream(filename));
}

Also, I change the property pointing to the file accordingly to:

sbb.spamwords.filename=/words.spam

Deploy to Bluemix using Cloud Foundry

I am using the Cloud Foundry CLI for deployment.

Step 1

Install the Cloud Foundry CLI. You can download it here

Step 2

Create an account on IBM Bluemix.

Step 3

Open a terminal window and change into the directory of the sample application.

First, we set which endpoint we are using. I used the UK one

cf api https://api.eu-gb.bluemix.net

And then we can log in the first time

cf login -u <username>

Step 4

Now, we can create our commentstore application and deploy it for the first time:

cf push commentstore -p comment-store/target/sbb-comment-app.jar

This will create the commentstore for our jar file using the default Liberty for Java build pack provided by IBM.

It does also directly deploy the application and tries to start it. However, as we haven’t’ connected a DB yet it will fail.

You can view the logs (taling) with:

cf logs commentstore

or use: :::bash cf logs commentstore –recent

Step 5

Create the Postgres service:

cf create-service postgresql 100 postgresql01

It creates a Postgres service called postgresql01 with the account plan 100. Seems to be a free plan.

Step 6

Next, we need to connect aka bind the Postgres service to our application. We do so by executing.

cf bind-service commentstore postgresql01

The first parameter is the name of the application and the second is the name of the service.

Step 7

Now, that we have a database, we define its connection in Spring Boot. You can see the URL, credentials and more by running:

cf env commentstore

Basically, we have two options. First, we can access these environment variables form Spring Boot and configure our database manually or we can use the Spring Cloud Connectors which does it for us.

Step 7a - Manual DB Setup

Cloud Foundry makes the service configuration available in an env variable called VCAP_SERVICES. Spring Boot does detect them automatically and provides them processed in the Spring environment. We can use them also in the Spring Expression language.

spring.datasource.url=${vcap.services.postgresql01.credentials.uri}
spring.datasrouce.username=${vcap.services.postgresql01.credentials.username}
spring.datasource.password=${vcap.services.postgresql01.credentials.password}

Spring Boot maps each service in VCAP_SERVICES by its name under the vcap.services prefix.

Now, run the push form Step 4 again and the application should start.

Step 7b - Using Spring Cloud Connectors

For common cloud services like databases, we have an alternative for the manual set up. Spring Boot provides a starter for using the Spring Cloud Connectors and we only need to add its dependency for get the DB working.

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

Deploy the app (see Step 4) again and it is ready to start.

Make a Request

Use Postman and the collection and adjust the requests to the endpoint of your application.

Remove the App again

When you are done, you can remove the app again by running:

cf delete commentstore

You don’t want to pay for a tutorial app, don’t you?

Conclusion

Thanks to Cloud Foundry it’s easy to deploy common tech stacks and being able to switch providers without changing your deployment setup. Spring Boot integrates smoothly and make it even better by autoconfiguring commons setups.