Getting Started with VMware Tanzu GemFire for Redis Apps and Spring Data Redis

July 7, 2022 John Martin

The release of VMware Tanzu GemFire 9.15 marks the launch of the VMware Tanzu GemFire for Redis Apps add-on. This add-on enables compatibility between Redis applications and Tanzu GemFire for the first time ever, unlocking enterprise-ready features for your Redis applications. 

In this article, users will get detailed instructions on how to convert an existing Spring Data Redis application from Redis to Tanzu GemFire, using the VMware Tanzu GemFire for Redis Apps add-on. 

Prerequisites 

Getting started with GemFire for Redis Apps 

This example will start with a simple Spring Data Redis application connecting to a Redis server; and then we’ll shut down the Redis server and switch the application over to using GemFire.

Let’s start by creating a simple `HelloWorld` application with Spring.

  1. Navigate to https://start.spring.io/.
  2. Change the `Artifact` and `Name` to TanzuGemFireForRedisApps. 
  3. Add the “Spring Data Redis (Access+Driver)” dependency. 
  4. Set the Java version to 11. 
  5. Click Generate. 
  6. Open the downloaded app in your IDE.

Get the app running with Redis 

The goal of this article is to show users how they can quickly convert their applications from Redis to Tanzu GemFire as the backend data store. This example app is meant to be very simple. As this is a Spring Boot application, much of the connection and configuration is auto-configured. 

Configure the Spring Boot App 

  1. First, we’ll add a RedisTemplate bean to the TanzuGemFireForRedisAppsApplication class. 

    @SpringBootApplication 
    public class TanzuGemFireForRedisAppsApplication { 
    
       	public static void main(String[] args) { 
            SpringApplication.run(TanzuGemFireForRedisAppsApplication.class, args); 
     
       	 } 
     
       	@Bean 
       	public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory connectionFactory) { 
             RedisTemplate<String, String> template = new RedisTemplate<>(); 
             template.setConnectionFactory(connectionFactory); 
             return template; 
       	 } 
     
    }
    
  2. Next, create a SimpleRedisClass to call SET and GET, and then print the value to the command line.

@Component 
public class SimpleRedisClass implements CommandLineRunner { 

  private final RedisTemplate<String,String> redisTemplate; 

  public SimpleRedisClass( 
            RedisTemplate<String, String> redisTemplate) { 
            this.redisTemplate = redisTemplate; 
 } 

  @Override 
  public void run(String... args) throws Exception { 

            redisTemplate.opsForValue().set("Key1", "HelloWorld"); 
            String key1 = redisTemplate.opsForValue().get("Key1"); 

            System.out.println("Key1: " + key1); 
  } 
} 
  1. Lastly, set the Redis properties in the application.properties file. For this example, we’re using default values for localhost and port. 

    spring.redis.host=localhost 
    spring.redis.port=6379
    
  • In a terminal, start the Redis Server using the following command: 
    redis-server
    
  • Run your Spring Boot App. 
    ./mvnw spring-boot:run 
  • In your console, you should see that “Key 1: HelloWorld” was printed out.
    2022-04-13 10:15:10.452 INFO 95884 --- [main] .e.T.TanzuGemFireForRedisAppsApplication : Started TanzuGemFireForRedisAppsApplication in 0.908 seconds (JVM running for 1.135) 
    Key1: HelloWorld 
    

Great! This confirms that the application runs and is compatible with Redis. Now we’ll switch over to using Tanzu GemFire with the Tanzu GemFire for Redis Apps add-on. 

Start a GemFire cluster with the GemFire for Redis Apps add-on

You must have downloaded and installed Tanzu GemFire 9.15 (or later) and the Tanzu GemFire for Redis Apps add-on from Tanzu Network. 

  1. Shut down your Redis server and re-run the app to confirm that the server is not running. You should receive an error like the following: 

    Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379 
  2. Confirm you have Tanzu GemFire 9.15 installed by opening a terminal and running the following command:

    gfsh version 
    

The output should be text that reads 9.15.0 or greater. If it is not, please make sure you have downloaded and installed Tanzu GemFire 9.15, according to the documentation

  1. Download Tanzu GemFire for Redis Apps (the .tgz file) from Tanzu Network. 

  2. Unzip the contents of the .tgz file. 

  3. In a terminal, start the GemFire command-line interface (CLI)/Shell (GFSH) with the following command: 

    gfsh 

You should see the following output: 

  1. Start a locator. Locators are used to tell the new connecting members where running members are located, and provide load balancing for server use. Learn more here

    start locator
    

10. After the locator has started, start a server with the path to the add-on folder and any additional parameters that need to be set. 

start server --name=redisServer1 --locators=localhost[10334] --server-port=40404 
--classpath=/path/to/gemfire-for-redis-apps/folder/lib/* 
--J=-Dgemfire-for-redis-port=6379 
--J=-Dgemfire-for-redis-enabled=true

11. Add an additional server/node, run the start server command and change the --name , --server-port, and --J=-Dgemfire-for-redis-port= parameters. For example: 

start server --name=redisServer2 --locators=localhost[10334] 
--server-port=40405 
--classpath=/path/to/gemfire-for-redis-apps/folder/lib/* 
--J=-Dgemfire-for-redis-port=6380 
--J=-Dgemfire-for-redis-enabled=true

Your Tanzu GemFire cluster should now be up and running (one locator and two servers) and ready to connect with a Redis client. Keep the terminal with GFSH open and running so that you can easily shut down the GemFire cluster when you are done working. 

Get the Spring Boot App running with Tanzu GemFire for Redis Apps 

To get the Spring Boot application to connect and run with the GemFire cluster, you’ll need to change a few properties in the application.properties file. 

12. Remove the “port” and “host” properties and replace them with “cluster.nodes”: 

spring.redis.cluster.nodes= 127.0.0.1:6379

13. Run the Spring Boot App. 

./mvnw spring-boot:run

In your console you should see that “Key 1: HelloWorld” was printed out. 

2022-04-13 10:21:14.638  INFO 24362 --- [main] .e.T.TanzuGemFireForRedisAppsApplication : Started TanzuGemFireForRedisAppsApplication in 1.022 seconds (JVM running for 1.268) 
Key1: HelloWorld

That’s it! You now have a Spring Boot App using Tanzu GemFire with the GemFire for Redis Apps add-on.

Spring Session Data Redis 

 If your application is using Spring Session Data Redis you will need to add the following code to disable Spring Session from calling CONFIG (CONFIG is not supported). 

@Bean 
public static ConfigureRedisAction configureRedisAction() { 
      return ConfigureRedisAction.NO_OP; 
      }

This is a known solution for many commercial Redis products (e.g., ElastiCache, Azure Cache for Redis, etc.) that disable the CONFIG command for security reasons. You can read more about why this is done here

Shutting down the GemFire cluster 

To shut down the GemFire cluster, in the GFSH CLI type the following command: 

shutdown --include-locators=true

This command will shut down the entire GemFire cluster, so you will be prompted with the following choice: 

As a lot of data in memory will be lost, including possibly events in queues, do you really want to shut down the entire distributed system? (Y/n)

To confirm that everything shut down correctly, re-run the app to confirm that the server is not running.  You should receive an error like the following: 

Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379

Additional resources 

For answers to more commonly asked questions on the GemFire for Redis Apps, check out our list of frequently asked questions. To learn more about what’s new in the GemFire 9.15 release, read the latest GemFire 9.15 release blog or its documentation.

Previous
VMware Application Catalog Now Accessible through VMware Marketplace
VMware Application Catalog Now Accessible through VMware Marketplace

The VMware Application Catalog user interface now surfaces through VMware Marketplace, a one-stop shop for ...

Next
Introducing VMware Tanzu GemFire for Redis Apps
Introducing VMware Tanzu GemFire for Redis Apps

The new VMware Tanzu GemFire for Redis Apps provides game-changing functionality, unlocking enterprise-read...

SpringOne 2022

Register Now