You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add a main method class to Initial folder
A Java class with a main method class is required to build the application
with CI. Additionally, the Initial folder is meant to mirror the
content that is donwloaded from Spring Initializr, which comes
with a main method class.
* Add CI file for GitHub Actions
* Include Docker Compose Support
This commit implements Spring Boot Docker Compose Support
to run the externaly Redis container. This simplifies the
learning experince and makes the guide more repeatable.
* Rewrite content about running the example
The new content will walk the user through 4 different
ways to build the application. First through a standalone jar file,
next through a docker container, next through a native compile,
and finally through a native container.
* Replace Lombok with Record
Lombok was initially used to reduce boilerplate code
before Record functionality was introduced in Java.
Now that we have records, we can switch out Lombok for
Java records and still have no boilerplate code in the
Coffee class. In general, the fewer dependencies that
we have the more the learner can focus on the core
teaching.
* Polish for README
* Add dependabot and remove Jenkins
Copy file name to clipboardExpand all lines: README.adoc
+87-80Lines changed: 87 additions & 80 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,108 +2,62 @@
2
2
:icons: font
3
3
:source-highlighter: prettify
4
4
:project_id: gs-spring-data-reactive-redis
5
+
:java_version: 17
6
+
:build_system: maven
7
+
:build_name: demo
8
+
:build_version: 0.0.1-SNAPSHOT
9
+
:network_container: guide-redis
10
+
5
11
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.
6
12
7
13
== What You Will Build
8
14
9
15
You'll build a Spring application that uses https://projects.spring.io/spring-data-redis/[Spring Data Redis] and https://projectreactor.io/[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).
If you choose to run the Redis server yourself instead of using Spring Boot Docker Compose support, you have a few options:
26
+
- https://redis.io/download[Download the server] and manually run it
27
+
- Install with Homebrew, if you use a Mac
28
+
- Manually run the `compose.yaml` file with `docker compose up`
29
+
30
+
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.
31
+
You also need to add configuration to an `application.properties` file, as described in greater detail in the <<_preparing_to_build_the_application>> section.
32
+
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.
17
33
18
34
[[scratch]]
19
35
== Starting with Spring Initializr
20
36
21
-
You can use this https://start.spring.io/#!type=maven-project&language=java&packaging=jar&jvmVersion=17&groupId=com.example&artifactId=demo&name=demo&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.demo&dependencies=data-redis-reactive,lombok,webflux[pre-initialized project] and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial.
37
+
You can use this https://start.spring.io/#!type=maven-project&language=java&packaging=jar&groupId=com.example&artifactId=demo&name=demo&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.demo&dependencies=data-redis-reactive,webflux,docker-compose[pre-initialized project] and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial.
22
38
23
39
To manually initialize the project:
24
40
25
41
. Navigate to https://start.spring.io.
26
42
This service pulls in all the dependencies you need for an application and does most of the setup for you.
27
43
. Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.
28
-
. Click *Dependencies* and select *Spring Reactive Web*, *Spring Data Reactive Redis*, and *Lombok*.
44
+
. Click *Dependencies* and select *Spring Reactive Web*, *Spring Data Reactive Redis*, and *Docker Compose Support*.
29
45
. Click *Generate*.
30
46
. Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.
31
47
32
48
NOTE: If your IDE has the Spring Initializr integration, you can complete this process from your IDE.
33
49
34
-
NOTE: You can also fork the project from Github and open it in your IDE or other editor.
35
-
36
-
[[scratch]]
37
-
== Standing up a Redis Server
38
-
39
-
Before you can build a messaging application, you need to set up the server that will
40
-
handle receiving and sending messages.
41
-
42
-
Redis is an open source, BSD-licensed, key-value data store that also comes with a
43
-
messaging system. The server is freely available at https://redis.io/download. You can
44
-
download it manually, or, if you use a Mac, with Homebrew, by running the following
45
-
command in a terminal window:
46
-
47
-
====
48
-
[source,bash]
49
-
----
50
-
brew install redis
51
-
----
52
-
====
53
-
54
-
Once you unpack Redis, you can launch it with its default settings by running the following command:
55
-
56
-
====
57
-
[source,bash]
58
-
----
59
-
redis-server
60
-
----
61
-
====
62
-
63
-
You should see a message similar to the following:
64
-
65
-
====
66
-
[source,text]
67
-
----
68
-
[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
69
-
[35142] 01 May 14:36:28.940 * Max number of open files set to 10032
70
-
_._
71
-
_.-``__ ''-._
72
-
_.-`` `. `_. ''-._ Redis 2.6.12 (00000000/0) 64 bit
73
-
.-`` .-```. ```\/ _.,_ ''-._
74
-
( ' , .-` | `, ) Running in stand alone mode
75
-
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
76
-
| `-._ `._ / _.-' | PID: 35142
77
-
`-._ `-._ `-./ _.-' _.-'
78
-
|`-._`-._ `-.__.-' _.-'_.-'|
79
-
| `-._`-._ _.-'_.-' | https://redis.io
80
-
`-._ `-._`-.__.-'_.-' _.-'
81
-
|`-._`-._ `-.__.-' _.-'_.-'|
82
-
| `-._`-._ _.-'_.-' |
83
-
`-._ `-._`-.__.-'_.-' _.-'
84
-
`-._ `-.__.-' _.-'
85
-
`-._ _.-'
86
-
`-.__.-'
87
-
88
-
[35142] 01 May 14:36:28.941 # Server started, Redis version 2.6.12
89
-
[35142] 01 May 14:36:28.941 * The server is now ready to accept connections on port 6379
90
-
----
91
-
====
92
-
93
50
[[initial]]
94
51
== Create a Domain Class
95
52
96
-
Create a class representing a type of coffee we wish to stock in our coffee catalog:
53
+
Create a record representing a type of coffee we wish to stock in our coffee catalog:
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()`).
105
-
106
-
107
61
== Create a Configuration Class
108
62
109
63
Create a class that includes Spring Beans that support reactive Redis operations:
@@ -114,7 +68,6 @@ Create a class that includes Spring Beans that support reactive Redis operations
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.
98
+
You can configure your IDE to look in the correct place or you could use the command line to run the application.
99
+
The `./gradlew bootRun` and `./mvnw spring-boot:run` commands will launch the application and automatically find the compose.yaml file.
143
100
144
-
Although you can package this service as a traditional link:/understanding/WAR[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 link:/understanding/Netty[Netty] an asynchronous "container" as the HTTP runtime instead of deploying to an external instance.
101
+
== Test the Application
145
102
103
+
With the application running, run the following command from a new terminal:
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.
156
+
This is because we run our containers in a https://docs.docker.com/compose/networking/[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.
161
157
162
-
Now that the application is running, you can test it by accessing `http://localhost:8080/coffees` from HTTPie, curl, or your favorite browser.
158
+
First, obtain an interactive shell to a new container that runs on the same network as the Redis container and the application:
159
+
[source, bash]
160
+
----
161
+
docker run --rm --network container:guide-redis -it alpine
162
+
----
163
+
164
+
Next, from the shell inside the container, install curl:
165
+
[source, bash]
166
+
----
167
+
apk add curl
168
+
----
163
169
170
+
Finally, you can run the curl commands as described in <<_test_the_application>>.
0 commit comments