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).
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 Lombok.
-
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. |
Note
|
You can also fork the project from Github and open it in your IDE or other editor. |
Before you can build a messaging application, you need to set up the server that will handle receiving and sending messages.
Redis is an open source, BSD-licensed, key-value data store that also comes with a messaging system. The server is freely available at https://redis.io/download. You can download it manually, or, if you use a Mac, with Homebrew, by running the following command in a terminal window:
brew install redis
Once you unpack Redis, you can launch it with its default settings by running the following command:
redis-server
You should see a message similar to the following:
[35142] 01 May 14:36:28.939 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
[35142] 01 May 14:36:28.940 * Max number of open files set to 10032
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.6.12 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 35142
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[35142] 01 May 14:36:28.941 # Server started, Redis version 2.6.12
[35142] 01 May 14:36:28.941 * The server is now ready to accept connections on port 6379
Create a class 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[]
Note
|
I use Lombok in this example to eliminate the boilerplate code for constructors and so-called "data class" methods ( accessors/mutators, equals() , toString() , & hashCode() ).
|
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[]
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[]
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[]
Although you can package this service as a traditional WAR file for deployment to an external application server, the simpler approach shown here creates a standalone application. You package everything in a single, executable JAR file, driven by a good old Java main()
method. Along the way, you use Spring’s support for embedding the Netty an asynchronous "container" as the HTTP runtime instead of deploying to an external instance.
src/main/java/com/example/demo/DemoApplication.java
link:complete/src/main/java/com/example/demo/DemoApplication.java[]
Now that the application is running, you can test it by accessing http://localhost:8080/coffees
from HTTPie, curl, or your favorite browser.
Congratulations! You have developed a Spring application that uses Spring Data and Redis for fully reactive, non-blocking database access!