Spring Boot + Redis Cloud Configuration and Setup Tutorial

If you are looking to work on a String Boot + Redis Cloud Database project and looking for ways to set it up! well, in this tutorial, I will quickly show you how do that with a simple "Hello Redis Cloud!" project.

  • Step 1: Go to https://start.spring.io/ and create your project. Make sure to add Spring Data Redis (Access+Driver) NOSQL dependency.

    Spring Data Redis NOSQL Spring Boot Dependency


    Make sure that you have added the dependencies correctly.

    Redis Spring Boot Dependency

    build.gradle

    dependencies {
    	implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    }

    pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    

  • Step 2: As we are using Redis Cloud Database, I assume you have setup your account on redis.com. Login to your Redis Cloud Console and get the Public endpoint of your database and the password.
  • Step 3: Open your application.properties file and add the below property,
    spring.data.redis.url=redis://default:your-db-password@redis-host..cloud.redislabs.com:your-db-port
    
    Redis Spring Boot application.properties setup

    Note: Fill in your password, host:port, username remains default.

  • Step 4: Now to test if your connection to the Redis Cloud Database is working good we will write some basic "Hello Redis Cloud!" code by modifying the class that has our main method with annotation @RedisdemoApplication.
    package com.example.redisdemo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.ApplicationArguments;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.data.redis.core.RedisTemplate;
    
    //Example by Code2care.org
    
    @SpringBootApplication
    public class RedisdemoApplication implements ApplicationRunner {
    
        @Autowired
        public RedisTemplate redisTemplate;
    
        public static void main(String[] args) {
            SpringApplication.run(RedisdemoApplication.class, args);
    
        }
    
        @Override
        public void run(ApplicationArguments args) throws Exception {
    
            redisTemplate.opsForValue().set("message", "Hello Redis!");
            System.out.println(redisTemplate.opsForValue().get("message"));
        }
    }
  • Step 5: Thats it! We are all good to run our first Spring Boot + Redis Cloud Db example!

    Hello Redis Cloud!



    Hope you found this tutorial useful! If yes, do write in a comment, if not, I am happy to help with the problems you are facing.

    Comments & Discussion

    Facing issues? Have questions? Post them here! We're happy to help!