Hands-On Reactive Programming in Spring 5

Hands-On Reactive Programming in Spring 5

This is the code repository for Hands-On Reactive Programming in Spring 5, published by Packt.

Build cloud-ready, reactive systems with Spring 5 and Project Reactor

What is this book about?

If you feel this book is for you, get your copy today!

Authors

Oleh Dokuka

Oleh Dokuka is an experienced software engineer, Pivotal Champion, and one of the top contributors to Project Reactor and Spring Framework. He knows the internals of both frameworks very well and advocates reactive programming with Project Reactor on a daily basis. Along with that, the author applies Spring Framework and Project Reactor in software development, so he knows how to build reactive systems using these technologies.

Igor Lozynskyi

Igor Lozynskyi is a senior Java developer who primarily focuses on developing reliable, scalable, and blazingly fast systems. He has over seven years of experience with the Java platform. He is passionate about interesting and dynamic projects both in life and in software development.

General requirements

All code samples should run on any operating system where the appropriate Java runs. Some examples require Docker Engine.

OS requirements

There is no hard requirement for OS. However, examples use JDK8 and later, Docker Engine 18.06 and later, and also download native executables (embedded MongoDB). Consequently, for successful execution, all software described above should work on your OS.

Tested operation systems: MacOS High Sierra, Windows 10, Ubuntu Linux 16.04 LTS.

Java

All examples were developed with JDK8 and tested to be compatible with JDK11. Because of the significant changes introduced by JDK11, some examples may display warnings or run only on JDK8.

To install JDK8, please refer to these instructions: http://jdk.java.net/8

To install JDK11, please refer to these instructions: http://jdk.java.net/11

Docker

A couple of examples assume running Docker Engine to start supporting services (MongoDB, PostgreSQL, Prometheus, etc.)

Only Chapter 10's example requires running Docker commands manually, other examples do automatic service provisioning.

To install Docker Engine on your PC, please follow the official instructions: https://docs.docker.com/install

IDE

All examples use Gradle build system, so they are not IDE dependent. However, we recommend using a free version of IntelliJ IDEA for a seamless exploration of the provided examples. We recommend using the latest version of IntelliJ IDEA as it has better support for JDK11.

The Community Edition of IntelliJ IDEA may be downloaded here: https://www.jetbrains.com/idea/download

Many examples use a handy the Lombok library (https://projectlombok.org/) which require annotation processing. Consequently, please install IntelliJ Lombok plugin (https://github.com/mplushnikov/lombok-intellij-plugin).

IDEA Lombok plugin

Also, please enable annotation processing in IDE (Preferences -> Build, Execution, Deployment -> Compiler -> Annotation Processors -> Check Enable annotation processing).

IDEA annotation processing

When IDE is installed and configured, you may open this project as Gradle project. Then, please define JDK for the project (File -> Project Structure -> Project Settings -> Project -> Project SDK).

Then, rebuild all modules of the project (Build -> Rebuild Project). Now, you may explore and run all examples.

Disclaimer

All code in this repository is for demonstration purposes and, consequently, often oversimplified.

Chapter 1: Why Reactive Spring?

Contains the following examples:

Chapter 2: Reactive Programming in Spring - Basic Concepts

Chapter 3: Reactive Streams - the New Streams' Standard

Chapter 4: Project Reactor - the Foundation for Reactive Apps

Chapter 5: Going Reactive with Spring Boot 2

Chapter 6: WebFlux Async Non-blocking Communication

Chapter 7: Reactive Database Access

Chapter 8: Scaling Up with Cloud Streams

Cloud-Stream

this is an example of Chat application that is fully decoupled and ready to be run in the cloud. The following is a steps required to prepare this sample:

Preparing MongoDB

The Project uses MongoDB as the primary database for all data's querying and storing.

There are two available options in order to install MongoDB:

(Option 1) Dockerized MongoDB

Note, that option requires the essential understanding of the [Docker's CLI] (https://docs.docker.com/engine/reference/commandline/cli/)

Before starting that option, please ensure that Docker (has already been installed on the local machine).

It is necessary to execute the following command in the terminal to run MongoDB image in the Docker container:

docker run --name test-mongo -p 27017:27017 -d mongo
(Option 2) Local Community MongoDB Server

There is an option to install MongoDB locally. All required information related to the local installation is available by the following link.

Preparing RabbitMQ

The Project uses RabbitMQ as a message broker for this example

There are two available options in order to install RabbitMQ:

(Option 1) Dockerized RabbitMQ

Note, that option requires the essential understanding of the [Docker's CLI] (https://docs.docker.com/engine/reference/commandline/cli/)

Before starting that option, please ensure that Docker (has already been installed on the local machine).

It is necessary to execute the following command in the terminal to run MongoDB image in the Docker container:

docker run --name test-rabbit -p 4369:4369 -p 5671:5671 -p 5672:5672 -p 25672:25672 -d rabbitmq
(Option 2) Local Community MongoDB Server

There is an option to install RabbitMQ locally. All required information related to the local installation is available by the following link.

Preparing the Environment

To properly run the Project the proper environment variables / YAML properties are required. The following is the list of available Spring Framework properties/environment variables:

Spring property Environment variable Description
gitter.auth.token GITTER_TOKEN Personal Access Token which can be used to access the Gitter API.
gitter.api.endpoint - The address of public Gitter REST API endpoint. The default value is https://api.gitter.im/. To learn more, see following API docs
gitter.api.version - The version of the Gitter REST API. The default value is v1.
gitter.api.messages-resource GITTER_ROOM Path to the Messages Resource. Note, in environment variable case, it is unnecessary defining the whole path since it has already been defined as the following: rooms/${GITTER_ROOM}/chatMessages. To get created Gitter room id, please see API docs.
gitter.stream.endpoint - The address of public Gitter Streaming API endpoint. The default value is https://stream.gitter.im/. To learn more, see following API docs
gitter.stream.version - The version of the Gitter Streaming API. The default value is v1.
gitter.stream.messages-resource GITTER_ROOM Path to the Messages Resource. Note, in environment variable case, it is unnecessary defining the whole path since it has already been defined as the following: rooms/${GITTER_ROOM}/chatMessages. To get created gitter room id, please see API docs.

Chapter 9: Testing the Reactive Application

Chapter 10: And, Finally, Release It!

This sample depicts a reactive application based on Spring Boot 2 and WebFlux with all required infrastructure for operational monitoring.

Prometheus pulls metrics, Grafana has a simple dashboard with app metrics, Zipkin gathers traces.

Application structure

Start or stop infrastructural services

To start services run the following command:

docker-compose -f chapter-10/docker/docker-compose.yml up -d

To stop services run the following command:

docker-compose -f chapter-10/docker/docker-compose.yml down

Start Spring Boot application

To start the application, run class org.rpis5.chapters.chapter_10.Chapter10CloudReadyApplication.

Accessing application components

Feedback

Your feedback is important for us, so do not hesitate creating issues if some examples do not work with your environment.

Also, please star of fork this repository if you find it useful!

Thanks!