How to configure a Spring Boot Initializer app to use Redis Cache

Overview

This article walks you through creating a Redis cache using the Azure portal, then using the Spring Initializr to create a custom application, and then creating a Java web application that stores and retrieves data using your Redis cache.

Prerequisites

The following prerequisites are required in order to follow the steps in this article:

Create a custom application using the Spring Initializr

  1. Browse to https://start.spring.io/.

  2. Specify that you want to generate a Maven project with Java, enter the Group and Aritifact names for your application, and then click the link to Switch to the full version of the Spring Initializr.

    Basic Spring Initializr options

    Note

    The Spring Initializr will use the Group and Aritifact names to create the package name; for example: com.contoso.myazuredemo.

  3. Scroll down to the Web section and check the box for Web, then scroll down to the NoSQL section and check the box for Redis, then scroll to the bottom of the page and click the button to Generate Project.

    Full Spring Initializr options

  4. When prompted, download the project to a path on your local computer.

    Download custom Spring Boot project

  5. After you have extracted the files on your local system, your custom Spring Boot application will be ready for editing.

    Custom Spring Boot project files

Create a Redis cache on Azure

  1. Browse to the Azure portal at https://portal.azure.com/ and click the item for +New.

    Azure portal

  2. Click Database, and then click Redis Cache.

    Azure portal

  3. On the New Redis Cache page, specify the following information:

    • Enter the DNS name for your cache.
    • Specify your Subscription, Resource group, Location, and Pricing tier.
    • For this tutorial, choose Unblock port 6379.

    Note

    You can use SSL with Redis caches, but you would need to use a different Redis client like Jedis. For more information, see How to use Azure Redis Cache with Java.

    When you have specified these options, click Create to create your cache.

    Azure portal

  4. Once your cache has been completed, you will see it listed on your Azure Dashboard, as well as under the All Resources, and Redis Caches pages. You can click on your cache on any of those locations to open the properties page for your cache.

    Azure portal

  5. When the page that contains the list of properties for your cache is displayed, click Access keys and copy your access keys for your cache.

    Azure portal

Configure your custom Spring Boot to use your Redis Cache

  1. Locate the application.properties file in the resources directory of your app, or create the file if it does not already exist.

    Locate the application.properties file

  2. Open the application.properties file in a text editor, and add the following lines to the file, and replace the sample values with the appropriate properties from your cache:

    # Specify the DNS URI of your Redis cache.
    spring.redis.host=myspringbootcache.redis.cache.windows.net
    
    # Specify the port for your Redis cache.
    spring.redis.port=6379
    
    # Specify the access key for your Redis cache.
    spring.redis.password=57686f6120447564652c2049495320526f636b73=
    

    Editing the application.properties file

    Note

    If you were using a different Redis client like Jedis that enables SSL, you would specify port 6380 in your application.properties file. For example:

    spring.redis.host=myspringbootcache.redis.cache.windows.net
    spring.redis.password=57686f6120447564652c2049495320526f636b73=
    spring.redis.ssl=true
    spring.redis.port=6380
    

    For more information, see How to use Azure Redis Cache with Java.

  3. Save and close the application.properties file.

  4. Create a folder named controller under the source folder for your package; for example:

    C:\SpringBoot\myazuredemo\src\main\java\com\contoso\myazuredemo\controller

    -or-

    /users/example/home/myazuredemo/src/main/java/com/contoso/myazuredemo/controller

  5. Create a new file named HelloController.java in the controller folder. Open the file in a text editor and add the following code to it:

    package com.contoso.myazuredemo;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.data.redis.core.ValueOperations;
    
    @RestController
    public class HelloController {
    
       @Autowired
       private StringRedisTemplate template;
    
       @RequestMapping("/")
       // Define the Hello World controller.
       public String hello() {
    
          ValueOperations<String, String> ops = this.template.opsForValue();
    
          // Add a Hello World string to your cache.
          String key = "greeting";
          if (!this.template.hasKey(key)) {
              ops.set(key, "Hello World!");
          }
    
          // Return the string from your cache.
          return ops.get(key);
       }
    }
    

    Where you will need to replace com.contoso.myazuredemo with the package name for your project.

  6. Save and close the HelloController.java file.

  7. Build your Spring Boot application with Maven and run it; for example:

    mvn clean package
    mvn spring-boot:run
    
  8. Test the web app by browsing to http://localhost:8080 using a web browser, or use the syntax like the following example if you have curl available:

    curl http://localhost:8080
    

    You should see the "Hello World!" message from your sample controller displayed, which is being retrieved dynamically from your Redis cache.

Next steps

For more information about using Spring Boot applications on Azure, see the following articles:

For more information about using Azure with Java, see the Azure for Java Developers and the Java Tools for Visual Studio Team Services.

For more information about getting started using Redis Cache with Java on Azure, see How to use Azure Redis Cache with Java.

The Spring Framework is an open-source solution that helps Java developers create enterprise-level applications. One of the more-popular projects that is built on top of that platform is Spring Boot, which provides a simplified approach for creating stand-alone Java applications. To help developers get started with Spring Boot, several sample Spring Boot packages are available at https://github.com/spring-guides/. In addition to choosing from the list of basic Spring Boot projects, the Spring Initializr helps developers get started with creating custom Spring Boot applications.