Skip to content

Commit 088140f

Browse files
authored
Docker Compose Guide Update (#31)
* Add code in complete required for Docker Compose This commit adds dependencies and a Docker Compose file that is required to change the guide to use Docker compose support. In this commit, only the complete folder is considered. * Update README instructions for Docker Compose Documented instructions for using Spring Boot Docker Compose support to run the guide. This is the preferred option for runnign the guide. Additional instructions for homebrew installation were removed. A section was added about running the application as a jar and as a container. Native compilation was excluded as it would require a custom hint because of how the guide is written. Native was excluded as it was not simple to implement and custom hints are beyond the scope of this guide. * Update Initial folder to represent empty project The initial folder is modified so that it is a true representation of what will be downloaded from Spring Initializr. This includes adding the new Docker Compose dependency as well as a compose.yaml file. * Remove reference to a web application * Update typo in README * Fix grammatical errors in README
1 parent 30aabe9 commit 088140f

File tree

8 files changed

+85
-96
lines changed

8 files changed

+85
-96
lines changed

README.adoc

Lines changed: 53 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
:icons: font
33
:source-highlighter: prettify
44
:project_id: gs-messaging-redis
5+
:java_version: 17
6+
:build_system: maven
7+
:build_name: messaging-redis
8+
:build_version: 0.0.1-SNAPSHOT
9+
:network_container: guide-redis
10+
:omit_native_build: y
511

612
This guide walks you through the process of using Spring Data Redis to publish and
713
subscribe to messages sent with Redis.
@@ -15,90 +21,39 @@ NOTE: It may sound strange to be using Spring Data Redis as the means to publish
1521
but, as you will discover, Redis provides not only a NoSQL data store but a messaging
1622
system as well.
1723

24+
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/guide_introduction.adoc[]
1825

19-
== What You Need
20-
21-
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/prereq_editor_jdk_buildtools.adoc[]
22-
+
23-
- A Redis server (See <<scratch>>)
24-
25-
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/how_to_complete_this_guide.adoc[]
26-
27-
[[scratch]]
28-
== Standing up a Redis server
26+
== Setting up the Redis server
2927

3028
Before you can build a messaging application, you need to set up the server that will
3129
handle receiving and sending messages.
30+
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/docker_compose_support.adoc[]
3231

33-
Redis is an open source, BSD-licensed, key-value data store that also comes with a
34-
messaging system. The server is freely available at https://redis.io/download. You can
35-
download it manually, or, if you use a Mac, with Homebrew, by running the following
36-
command in a terminal window:
37-
38-
====
39-
[source,bash]
40-
----
41-
brew install redis
42-
----
43-
====
32+
If you choose to run the Redis server yourself instead of using Spring Boot Docker Compose support, you have a few options:
33+
- https://redis.io/download[Download the server] and manually run it
34+
- Install with Homebrew, if you use a Mac
35+
- Manually run the `compose.yaml` file with `docker compose up`
4436

45-
Once you unpack Redis, you can launch it with its default settings by running the following command:
46-
47-
====
48-
[source,bash]
49-
----
50-
redis-server
51-
----
52-
====
53-
54-
You should see a message similar to the following:
55-
56-
====
57-
[source,text]
58-
----
59-
[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
60-
[35142] 01 May 14:36:28.940 * Max number of open files set to 10032
61-
_._
62-
_.-``__ ''-._
63-
_.-`` `. `_. ''-._ Redis 2.6.12 (00000000/0) 64 bit
64-
.-`` .-```. ```\/ _.,_ ''-._
65-
( ' , .-` | `, ) Running in stand alone mode
66-
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
67-
| `-._ `._ / _.-' | PID: 35142
68-
`-._ `-._ `-./ _.-' _.-'
69-
|`-._`-._ `-.__.-' _.-'_.-'|
70-
| `-._`-._ _.-'_.-' | https://redis.io
71-
`-._ `-._`-.__.-'_.-' _.-'
72-
|`-._`-._ `-.__.-' _.-'_.-'|
73-
| `-._`-._ _.-'_.-' |
74-
`-._ `-._`-.__.-'_.-' _.-'
75-
`-._ `-.__.-' _.-'
76-
`-._ _.-'
77-
`-.__.-'
78-
79-
[35142] 01 May 14:36:28.941 # Server started, Redis version 2.6.12
80-
[35142] 01 May 14:36:28.941 * The server is now ready to accept connections on port 6379
81-
----
82-
====
37+
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.
38+
You also need to add configuration to an `application.properties` file, as described in greater detail in the <<_preparing_to_build_the_application>> section.
39+
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.
8340

8441
[[initial]]
8542
== Starting with Spring Initializr
8643

87-
You can use this https://start.spring.io/#!type=maven-project&groupId=com.example&artifactId=messaging-redis&name=messaging-redis&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.messaging-redis&dependencies=data-redis[pre-initialized project] and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial.
44+
You can use this https://start.spring.io/#!type=maven-project&groupId=com.example&artifactId=messaging-redis&name=messaging-redis&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.messaging-redis&dependencies=data-redis,docker-compose[pre-initialized project^] and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial.
8845

8946
To manually initialize the project:
9047

9148
. Navigate to https://start.spring.io.
9249
This service pulls in all the dependencies you need for an application and does most of the setup for you.
9350
. Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.
94-
. Click *Dependencies* and select *Spring Data Redis*.
51+
. Click *Dependencies* and select *Spring Data Redis* and *Docker Compose Support*.
9552
. Click *Generate*.
96-
. Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.
53+
. Download the resulting ZIP file, which is an archive of an application that is configured with your choices.
9754

9855
NOTE: If your IDE has the Spring Initializr integration, you can complete this process from your IDE.
9956

100-
NOTE: You can also fork the project from Github and open it in your IDE or other editor.
101-
10257
== Create a Redis Message Receiver
10358

10459
In any messaging-based application, there are message publishers and messaging receivers.
@@ -166,44 +121,53 @@ container bean starts listening for messages. The `main()` method then retrieves
166121
`Hello from Redis!` message on the `chat` topic. Finally, it closes the Spring application
167122
context, and the application ends.
168123

169-
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/build_an_executable_jar_mainhead.adoc[]
124+
== Run the Application
170125

171-
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/build_an_executable_jar_with_both.adoc[]
126+
You can run the main method through your IDE.
127+
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.
128+
You can configure your IDE to look in the correct place or you could use the command line to run the application.
129+
The `./gradlew bootRun` and `./mvnw spring-boot:run` commands launch the application and automatically find the compose.yaml file.
172130

173-
You should see output similar to the following:
131+
You should see the output:
174132

175133
====
176-
[source,text]
134+
[source,bash]
177135
----
178-
179-
. ____ _ __ _ _
180-
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
181-
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
182-
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
183-
' |____| .__|_| |_|_| |_\__, | / / / /
184-
=========|_|==============|___/=/_/_/_/
185-
:: Spring Boot :: (v2.1.8.RELEASE)
186-
187-
2019-09-23 12:57:11.578 INFO 35396 --- [ main] c.e.m.MessagingRedisApplication : Starting MessagingRedisApplication on Jays-MBP with PID 35396 (/Users/j/projects/guides/gs-messaging-redis/complete/target/classes started by j in /Users/j/projects/guides/gs-messaging-redis/complete)
188-
2019-09-23 12:57:11.581 INFO 35396 --- [ main] c.e.m.MessagingRedisApplication : No active profile set, falling back to default profiles: default
189-
2019-09-23 12:57:11.885 INFO 35396 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
190-
2019-09-23 12:57:11.887 INFO 35396 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
191-
2019-09-23 12:57:11.914 INFO 35396 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 13ms. Found 0 repository interfaces.
192-
2019-09-23 12:57:12.685 INFO 35396 --- [ container-1] io.lettuce.core.EpollProvider : Starting without optional epoll library
193-
2019-09-23 12:57:12.685 INFO 35396 --- [ container-1] io.lettuce.core.KqueueProvider : Starting without optional kqueue library
194-
2019-09-23 12:57:12.848 INFO 35396 --- [ main] c.e.m.MessagingRedisApplication : Started MessagingRedisApplication in 1.511 seconds (JVM running for 3.685)
195-
2019-09-23 12:57:12.849 INFO 35396 --- [ main] c.e.m.MessagingRedisApplication : Sending message...
196-
2019-09-23 12:57:12.861 INFO 35396 --- [ container-2] com.example.messagingredis.Receiver : Received <Hello from Redis!>
136+
yyyy-mm-ddT07:08:48.646-04:00 INFO 18338 --- [main] c.e.m.MessagingRedisApplication: Sending message...
137+
yyyy-mm-ddT07:08:48.663-04:00 INFO 18338 --- [container-1] com.example.messagingredis.Receiver : Received <Hello from Redis!>
197138
----
198139
====
199140

141+
== Preparing to Build the Application
142+
143+
To run the code without Spring Boot Docker Compose support, you need a version of Redis running locally.
144+
To do this, you can use Docker Compose, but you must first make two changes to the `compose.yaml` file.
145+
First, modify the `ports` entry in `compose.yaml` to be `'6379:6379'`.
146+
Second, add a `container_name`.
147+
148+
The `compose.yaml` should now be:
149+
----
150+
services:
151+
redis:
152+
container_name: 'guide-redis'
153+
image: 'redis:latest'
154+
ports:
155+
- '6379:6379'
156+
----
157+
158+
You can now run `docker compose up` to start the Redis server.
159+
Now you should have an external Redis server that is ready to accept requests.
160+
You can rerun the application and see the same output using your external Redis server.
161+
162+
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.
163+
164+
include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/build_and_execute_guide.adoc[]
165+
200166
== Summary
201167

202168
Congratulations! You have just developed a publish-and-subscribe application with Spring
203169
and Redis.
204170

205-
NOTE: https://pivotal.io/products/redis[Redis support] is available.
206-
207171
== See Also
208172

209173
The following guides may also be helpful:

complete/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ repositories {
1414

1515
dependencies {
1616
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
17+
developmentOnly 'org.springframework.boot:spring-boot-docker-compose'
1718
testImplementation 'org.springframework.boot:spring-boot-starter-test'
19+
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
1820
}
1921

20-
test {
22+
tasks.named('test') {
2123
useJUnitPlatform()
2224
}
25+

complete/compose.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
redis:
3+
image: 'redis:latest'
4+
ports:
5+
- '6379'

complete/pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111
<groupId>com.example</groupId>
12-
<artifactId>messaging-redis-complete</artifactId>
12+
<artifactId>messaging-redis</artifactId>
1313
<version>0.0.1-SNAPSHOT</version>
1414
<name>messaging-redis-complete</name>
1515
<description>Demo project for Spring Boot</description>
@@ -21,7 +21,12 @@
2121
<groupId>org.springframework.boot</groupId>
2222
<artifactId>spring-boot-starter-data-redis</artifactId>
2323
</dependency>
24-
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-docker-compose</artifactId>
27+
<scope>runtime</scope>
28+
<optional>true</optional>
29+
</dependency>
2530
<dependency>
2631
<groupId>org.springframework.boot</groupId>
2732
<artifactId>spring-boot-starter-test</artifactId>

complete/src/main/resources/application.properties

Whitespace-only changes.

initial/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ repositories {
1414

1515
dependencies {
1616
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
17+
developmentOnly 'org.springframework.boot:spring-boot-docker-compose'
1718
testImplementation 'org.springframework.boot:spring-boot-starter-test'
19+
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
1820
}
1921

20-
test {
22+
tasks.named('test') {
2123
useJUnitPlatform()
22-
}
24+
}

initial/compose.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
redis:
3+
image: 'redis:latest'
4+
ports:
5+
- '6379'

initial/pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111
<groupId>com.example</groupId>
12-
<artifactId>messaging-redis-initial</artifactId>
12+
<artifactId>messaging-redis</artifactId>
1313
<version>0.0.1-SNAPSHOT</version>
1414
<name>messaging-redis-initial</name>
1515
<description>Demo project for Spring Boot</description>
@@ -21,7 +21,12 @@
2121
<groupId>org.springframework.boot</groupId>
2222
<artifactId>spring-boot-starter-data-redis</artifactId>
2323
</dependency>
24-
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-docker-compose</artifactId>
27+
<scope>runtime</scope>
28+
<optional>true</optional>
29+
</dependency>
2530
<dependency>
2631
<groupId>org.springframework.boot</groupId>
2732
<artifactId>spring-boot-starter-test</artifactId>

0 commit comments

Comments
 (0)