- What You Will Build
- Setting up the Redis Server
- Starting with Spring Initializr
- Create a Domain Class
- Create a Configuration Class
- Create a Spring Bean to Load Data
- Create a RestController
- Run the Application
- Test the Application
- Preparing to Build the Application
- Test the Application in Docker
- Summary
This guide walks you through the process of creating a functional reactive application that uses Spring Data to interact with Redis using the non-blocking Lettuce driver.
You’ll build a Spring application that uses Spring Data Redis and Project Reactor to interact with a Redis data store reactively, storing and retrieving Coffee
objects without blocking. This application uses Reactor’s Publisher
implementations based upon the Reactive Streams specification, namely Mono
(for a Publisher returning 0 or 1 value) and Flux
(for a Publisher returning 0 to n values).
Before you can build a messaging application, you need to set up the server to handle receiving and sending messages. https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/docker_compose_support.adoc
If you choose to run the Redis server yourself instead of using Spring Boot Docker Compose support, you have a few options:
- Download the server and manually run it
- Install with Homebrew, if you use a Mac
- Manually run the compose.yaml
file with docker compose up
If you go with any of these alternate approaches, you should remove the spring-boot-docker-compose
dependency from the Maven or Gradle build file.
You also need to add configuration to an application.properties
file, as described in greater detail in the [_preparing_to_build_the_application] section.
As mentioned earlier, this guide assumes that you use Docker Compose support in Spring Boot, so additional changes to application.properties
are not required at this point.
You can use this pre-initialized project and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial.
To manually initialize the project:
-
Navigate to https://start.spring.io. This service pulls in all the dependencies you need for an application and does most of the setup for you.
-
Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.
-
Click Dependencies and select Spring Reactive Web, Spring Data Reactive Redis, and Docker Compose Support.
-
Click Generate.
-
Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.
Note
|
If your IDE has the Spring Initializr integration, you can complete this process from your IDE. |
Create a record representing a type of coffee we wish to stock in our coffee catalog:
src/main/java/com/example/demo/Coffee.java
link:complete/src/main/java/com/example/demo/Coffee.java[role=include]
Create a class that includes Spring Beans that support reactive Redis operations:
src/main/java/com/example/demo/CoffeeConfiguration.java
link:complete/src/main/java/com/example/demo/CoffeeConfiguration.java[role=include]
Create a Spring Bean to load sample data for our application when we start it:
Note
|
Since we may (re)start our application multiple times, we should first remove any data that may still exist from previous executions. We do this with a flushAll() (Redis) server command. Once we’ve flushed any existing data, we create a small Flux , map each coffee name to a Coffee object, and save it to the reactive Redis repository. We then query the repo for all values and display them.
|
src/main/java/com/example/demo/CoffeeLoader.java
link:complete/src/main/java/com/example/demo/CoffeeLoader.java[role=include]
Create a RestController
to provide an external interface for our application:
src/main/java/com/example/demo/CoffeeController.java
link:complete/src/main/java/com/example/demo/CoffeeController.java[role=include]
You can run the main method through your IDE.
Note that, if you have cloned the project from the solution repository, your IDE may look in the wrong place for the compose.yaml
file.
You can configure your IDE to look in the correct place or you could use the command line to run the application.
The ./gradlew bootRun
and ./mvnw spring-boot:run
commands will launch the application and automatically find the compose.yaml file.
With the application running, run the following command from a new terminal:
curl http://localhost:8080/coffees
You should see the following output:
[
{
"id": "04ce0843-c9f8-40f6-942f-1ff643c1d426",
"name": "Jet Black Redis"
},
{
"id": "e2a0d798-5fa4-48a2-a45c-7770d8bb82bf",
"name": "Black Alert Redis"
},
{
"id": "13f13e3a-0798-44b7-8ae4-b319b227bb19",
"name": "Darth Redis"
}
]
To run the code without Spring Boot Docker Compose support, you need a version of Redis running locally to connect to.
To do this, you can use Docker Compose, but you must first make two changes to the compose.yaml
file.
First, modify the ports
entry in compose.yaml
to be '6379:6379'
.
Second, add a container_name
.
The compose.yaml
should now be:
services: redis: container_name: 'guide-redis' image: 'redis:latest' ports: - '6379:6379'
You can now run docker compose up
to start the Redis server.
Now you should have an external Redis server that is ready to accept requests.
You can rerun the application and see the same output using your external Redis server.
Note
|
No configuration is required in the application.properties file because the default values match the Redis server configuration in compose.yaml . Specifically, the properties spring.data.redis.host and spring.data.redis.port default to localhost and 6379 respectively.
|
If you ran the application using a Docker instruction (shown earlier), a simple curl command from a terminal or command line will no longer work. This is because we run our containers in a Docker network that is not accessible from the terminal or command line. To run curl commands, we can start a third container to run our curl commands and attach it to the same network.
First, obtain an interactive shell to a new container that runs on the same network as the Redis container and the application:
docker run --rm --network container:guide-redis -it alpine
Next, from the shell inside the container, install curl:
apk add curl
Finally, you can run the curl commands as described in [_test_the_application].
Congratulations! You have developed a Spring application that uses Spring Data and Redis for fully reactive, non-blocking database access!