From 2bc938c03cb8e793c6a7405866e34468f5c900cc Mon Sep 17 00:00:00 2001 From: Robert McNees Date: Thu, 20 Jun 2024 07:22:18 -0400 Subject: [PATCH 1/6] 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. --- complete/build.gradle | 5 ++++- complete/compose.yaml | 5 +++++ complete/pom.xml | 7 ++++++- complete/src/main/resources/application.properties | 0 4 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 complete/compose.yaml create mode 100644 complete/src/main/resources/application.properties diff --git a/complete/build.gradle b/complete/build.gradle index ea0c119..9fb61b9 100644 --- a/complete/build.gradle +++ b/complete/build.gradle @@ -14,9 +14,12 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-redis' + developmentOnly 'org.springframework.boot:spring-boot-docker-compose' testImplementation 'org.springframework.boot:spring-boot-starter-test' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } -test { +tasks.named('test') { useJUnitPlatform() } + diff --git a/complete/compose.yaml b/complete/compose.yaml new file mode 100644 index 0000000..1cfb822 --- /dev/null +++ b/complete/compose.yaml @@ -0,0 +1,5 @@ +services: + redis: + image: 'redis:latest' + ports: + - '6379' diff --git a/complete/pom.xml b/complete/pom.xml index e9ed96f..cea3125 100644 --- a/complete/pom.xml +++ b/complete/pom.xml @@ -21,7 +21,12 @@ org.springframework.boot spring-boot-starter-data-redis - + + org.springframework.boot + spring-boot-docker-compose + runtime + true + org.springframework.boot spring-boot-starter-test diff --git a/complete/src/main/resources/application.properties b/complete/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 From 23c6888fe42893602b849393ce2e7bd1aebcc3dd Mon Sep 17 00:00:00 2001 From: Robert McNees Date: Thu, 20 Jun 2024 12:02:31 -0400 Subject: [PATCH 2/6] 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. --- README.adoc | 140 ++++++++++++++++++----------------------------- complete/pom.xml | 2 +- initial/pom.xml | 2 +- 3 files changed, 54 insertions(+), 90 deletions(-) diff --git a/README.adoc b/README.adoc index 1e6430d..02f25c6 100644 --- a/README.adoc +++ b/README.adoc @@ -2,6 +2,12 @@ :icons: font :source-highlighter: prettify :project_id: gs-messaging-redis +:java_version: 17 +:build_system: maven +:build_name: messaging-redis +:build_version: 0.0.1-SNAPSHOT +:network_container: guide-redis +:omit_native_build: y This guide walks you through the process of using Spring Data Redis to publish and 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 but, as you will discover, Redis provides not only a NoSQL data store but a messaging system as well. +include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/guide_introduction.adoc[] -== What You Need - -include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/prereq_editor_jdk_buildtools.adoc[] -+ -- A Redis server (See <>) - -include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/how_to_complete_this_guide.adoc[] - -[[scratch]] -== Standing up a Redis server +== Setting up the Redis server Before you can build a messaging application, you need to set up the server that will handle receiving and sending messages. +include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/docker_compose_support.adoc[] -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: - -==== -[source,bash] ----- -brew install redis ----- -==== +If you choose to run the Redis server yourself instead of using Spring Boot Docker Compose support, you have a few options: +- https://redis.io/download[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` -Once you unpack Redis, you can launch it with its default settings by running the following command: - -==== -[source,bash] ----- -redis-server ----- -==== - -You should see a message similar to the following: - -==== -[source,text] ----- -[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 ----- -==== +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 will 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. [[initial]] == Starting with Spring Initializr -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. +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. 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 Data Redis*. +. Click *Dependencies* and select *Spring Data 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. -NOTE: You can also fork the project from Github and open it in your IDE or other editor. - == Create a Redis Message Receiver 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 `Hello from Redis!` message on the `chat` topic. Finally, it closes the Spring application context, and the application ends. -include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/build_an_executable_jar_mainhead.adoc[] +== Run the Application -include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/build_an_executable_jar_with_both.adoc[] +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. -You should see output similar to the following: +You should see the output: ==== -[source,text] +[source,bash] ---- - - . ____ _ __ _ _ - /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ -( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ - \\/ ___)| |_)| | | | | || (_| | ) ) ) ) - ' |____| .__|_| |_|_| |_\__, | / / / / - =========|_|==============|___/=/_/_/_/ - :: Spring Boot :: (v2.1.8.RELEASE) - -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) -2019-09-23 12:57:11.581 INFO 35396 --- [ main] c.e.m.MessagingRedisApplication : No active profile set, falling back to default profiles: default -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! -2019-09-23 12:57:11.887 INFO 35396 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode. -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. -2019-09-23 12:57:12.685 INFO 35396 --- [ container-1] io.lettuce.core.EpollProvider : Starting without optional epoll library -2019-09-23 12:57:12.685 INFO 35396 --- [ container-1] io.lettuce.core.KqueueProvider : Starting without optional kqueue library -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) -2019-09-23 12:57:12.849 INFO 35396 --- [ main] c.e.m.MessagingRedisApplication : Sending message... -2019-09-23 12:57:12.861 INFO 35396 --- [ container-2] com.example.messagingredis.Receiver : Received +yyyy-mm-ddT07:08:48.646-04:00 INFO 18338 --- [main] c.e.m.MessagingRedisApplication: Sending message... +yyyy-mm-ddT07:08:48.663-04:00 INFO 18338 --- [container-1] com.example.messagingredis.Receiver : Received ---- ==== +== Preparing to Build the Application + +To run the code without Spring Boot Docker Compose support, you need a version of RabbitMQ 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. + +include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/build_and_execute_guide.adoc[] + == Summary Congratulations! You have just developed a publish-and-subscribe application with Spring and Redis. -NOTE: https://pivotal.io/products/redis[Redis support] is available. - == See Also The following guides may also be helpful: diff --git a/complete/pom.xml b/complete/pom.xml index cea3125..3038d30 100644 --- a/complete/pom.xml +++ b/complete/pom.xml @@ -9,7 +9,7 @@ com.example - messaging-redis-complete + messaging-redis 0.0.1-SNAPSHOT messaging-redis-complete Demo project for Spring Boot diff --git a/initial/pom.xml b/initial/pom.xml index c3c0dba..53de838 100644 --- a/initial/pom.xml +++ b/initial/pom.xml @@ -11,7 +11,7 @@ com.example messaging-redis-initial 0.0.1-SNAPSHOT - messaging-redis-initial + messaging-redis Demo project for Spring Boot 17 From 69a0d61d4ec7078c0235a8f7cd54fdf348c74e6b Mon Sep 17 00:00:00 2001 From: Robert McNees Date: Thu, 20 Jun 2024 12:07:05 -0400 Subject: [PATCH 3/6] 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. --- initial/build.gradle | 6 ++++-- initial/compose.yaml | 5 +++++ initial/pom.xml | 11 ++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 initial/compose.yaml diff --git a/initial/build.gradle b/initial/build.gradle index ea0c119..d842806 100644 --- a/initial/build.gradle +++ b/initial/build.gradle @@ -14,9 +14,11 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-redis' + developmentOnly 'org.springframework.boot:spring-boot-docker-compose' testImplementation 'org.springframework.boot:spring-boot-starter-test' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } -test { +tasks.named('test') { useJUnitPlatform() -} +} \ No newline at end of file diff --git a/initial/compose.yaml b/initial/compose.yaml new file mode 100644 index 0000000..1cfb822 --- /dev/null +++ b/initial/compose.yaml @@ -0,0 +1,5 @@ +services: + redis: + image: 'redis:latest' + ports: + - '6379' diff --git a/initial/pom.xml b/initial/pom.xml index 53de838..26af74a 100644 --- a/initial/pom.xml +++ b/initial/pom.xml @@ -9,9 +9,9 @@ com.example - messaging-redis-initial + messaging-redis 0.0.1-SNAPSHOT - messaging-redis + messaging-redis-initial Demo project for Spring Boot 17 @@ -21,7 +21,12 @@ org.springframework.boot spring-boot-starter-data-redis - + + org.springframework.boot + spring-boot-docker-compose + runtime + true + org.springframework.boot spring-boot-starter-test From 210c60d257c307b2b4b22cc8c609f5a78478804e Mon Sep 17 00:00:00 2001 From: Robert McNees Date: Thu, 20 Jun 2024 12:09:47 -0400 Subject: [PATCH 4/6] Remove reference to a web application --- README.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.adoc b/README.adoc index 02f25c6..efc569f 100644 --- a/README.adoc +++ b/README.adoc @@ -50,7 +50,7 @@ This service pulls in all the dependencies you need for an application and does . Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java. . Click *Dependencies* and select *Spring Data 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. +. Download the resulting ZIP file, which is an archive of an application that is configured with your choices. NOTE: If your IDE has the Spring Initializr integration, you can complete this process from your IDE. From b2f4fcbbc6e8da28c239ef1b15faddc7c4ead1e1 Mon Sep 17 00:00:00 2001 From: Robert McNees Date: Fri, 21 Jun 2024 07:15:33 -0400 Subject: [PATCH 5/6] Update typo in README --- README.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.adoc b/README.adoc index efc569f..8767fe7 100644 --- a/README.adoc +++ b/README.adoc @@ -140,7 +140,7 @@ yyyy-mm-ddT07:08:48.663-04:00 INFO 18338 --- [container-1] com.example.messagin == Preparing to Build the Application -To run the code without Spring Boot Docker Compose support, you need a version of RabbitMQ running locally to connect to. +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`. From 4dd6293463487b85d26cbb2784cb058a1ce705f5 Mon Sep 17 00:00:00 2001 From: Robert McNees Date: Wed, 26 Jun 2024 05:34:29 -0400 Subject: [PATCH 6/6] Fix grammatical errors in README --- README.adoc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.adoc b/README.adoc index 8767fe7..99782c6 100644 --- a/README.adoc +++ b/README.adoc @@ -35,7 +35,7 @@ If you choose to run the Redis server yourself instead of using Spring Boot Dock - 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 will also need to add configuration to an `application.properties` file, as described in greater detail in the <<_preparing_to_build_the_application>> section. +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. [[initial]] @@ -126,7 +126,7 @@ context, and the application ends. 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. +The `./gradlew bootRun` and `./mvnw spring-boot:run` commands launch the application and automatically find the compose.yaml file. You should see the output: @@ -140,7 +140,7 @@ yyyy-mm-ddT07:08:48.663-04:00 INFO 18338 --- [container-1] com.example.messagin == Preparing to Build the Application -To run the code without Spring Boot Docker Compose support, you need a version of Redis running locally to connect to. +To run the code without Spring Boot Docker Compose support, you need a version of Redis running locally. 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`.