Javarevisited

A humble place to learn Java and Programming better.

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:

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:

  1. Setup Project
  2. Modeling Domain Classes
  3. Modeling Persistence Classes
  4. Modeling Web Classes
  5. Modeling Event Classes
  6. 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
Example of creation

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:

Example of Hexagonal Architecture

Related Links

Next Tutorial: Tutorial Part II

--

--

Javarevisited
Javarevisited

Published in Javarevisited

A humble place to learn Java and Programming better.

Gabriel Martins
Gabriel Martins

Written by Gabriel Martins

Just a brazilian guy who loves technology and wants to share the experiences he has learned

No responses yet