Spring Boot Project using Kafka and R2DBC (Part I)
This tutorial is about creating a simple application using Spring Boot and Spring Kafka with R2DBC.
We’ll need:
- JDK 11
- Gradle
- Lombok
- Spring WebFlux
- Spring Kafka
- Spring R2DBC
- PostgreSQL Driver
- Docker
Imagine the scenario below:
Someone wants to store measurements using a health monitor (such as a heart rate sensor, etc.). Data is collected and sent through MQTT to a broker (like HiveMQ for example).
After that, the Measurement Service subscribes to the topic and sends the read measurements to Health Service, which will analyze whether signs are Normal, High, or Low.
In the end, the Notification Service can send an SMS (or other kind of notification) to the person or to a doctor if some sign seems critical.
Therefore, in this tutorial, we will only develop the Health Service that will receive the measurements and publish the events of the analyzed data.
This tutorial will be divided into 4 parts:
- Setup Project
- Modeling Domain Classes
- Modeling Persistence Classes
- Modeling Web Classes
- Modeling Event Classes
- Running application and services using docker
Now, let’s setup our project !!!
Setup Project
Go to Spring Initialzr (https://start.spring.io/) and choose the following dependencies:
- Lombok
- Spring for Apache Kafka
- Spring Boot Actuator (Optional)
- Sleuth (Optional)
- Spring Data R2DBC
- Spring Reactive Web
CAUTION: Don’t use spring-boot-devtools, you may have problems with avro deserialization. See https://github.com/spring-projects/spring-kafka/issues/1665
The build.gradle file will be something like that:
And settings.gradle should be something like that:
Application Architecture
We could use an approach multi-module based, but instead, we are going to use a basic package organization.
For this application, I believe that Hexagonal Architecture looks like a good option.
The image below shows how this works:
Related Links
Next Tutorial: Tutorial Part II