Accessing a Bluemix Redis Service from a Java Spring Boot Application

By: Paul Bennett

Accessing a Bluemix Redis Service from a Java Spring Boot Application

In this post I’ll show how to access a Compose for Redis caching service on Bluemix from a Spring Boot application. Redis is a popular open-source in-memory database implementing a networked, in-memory key-value store. Spring is a popular open-source application framework and inversion of control container for Java.

Prerequisites

You’ll need a Bluemix account, and you’ll need to have Maven and Java installed. It is assumed that you can build and run a basic Spring guide project.

Create the Redis Service

Login to Bluemix and go to the Catalog. Under Data & Analytics select Compose for Redis. Click Create to create the service (the only options are the service name and credential name – the defaults should be fine). When the service is fully deployed you should see an Overview screen similar to the screen below: Note the service’s host, port, and password which you can get from either the Connection String or the Command Line.

Create the Redis Service

Build and Run the Application

We’ll use the Messaging with Redis Spring guide project. Download the project, then build and run the complete project.

The project defaults to using a Redis server on localhost:6379. To use the Bluemix Redis service create a file src/main/resources/application.properties with the host, port, and password properties from the Redis service page (above):

<br>
spring.redis.host=sl-us-south-1-portal.3.dblayer.com<br>
spring.redis.password=OHZNVHGWCMTDQUYA<br>
spring.redis.port=18313<br>

 

Rebuild and run the application by issuing the following commands:

<br>
$ mvn clean install<br>
$ java -jar target/gs-messaging-redis-0.1.0.jar<br>

You should see the following output:

You should see the following output:

Summary

It’s that simple to create a Spring Boot application to accessa Redis service on Bluemix! In my next post I’ll show you how to run the app on a Bluemix Kubernetes cluster using Kubernetes secrets to configure the service credentials.

Be the first to hear about news, product updates, and innovation from IBM Cloud