This project demonstrates a minimal configuration to set up and use Apache Kafka with Spring Boot and a simple Thymeleaf-based front-end. It is designed for developers who seek a basic understanding of Kafka, its purpose, how to integrate it into Spring Boot, and how to provide a real-time UI to display and send messages.
Apache Kafka is a distributed event-streaming platform designed for high-performance data pipelines, stream processing, and data integration. Kafka is widely used for building real-time data pipelines and streaming applications, ensuring durability and fault-tolerance.
- Producers: Applications or services that send messages to Kafka topics.
- Consumers: Applications or services that read messages from Kafka topics.
- Topics: Categories to which messages are sent.
- Zookeeper: Manages Kafka's configuration, leader election, and health monitoring.
This project highlights:
- Minimal Kafka configuration: A basic setup using
docker-composefor Kafka and Zookeeper. - Spring Boot integration: Using Spring Kafka to send and receive serialized objects (
Person). - Thymeleaf-based Server-Side Rendering (SSR): A simple UI rendered by Thymeleaf, allowing you to view messages in real-time without manual page reloads.
- Server-Sent Events (SSE): Real-time updates from the back-end to the browser as new messages arrive.
- Random Data Generation: A button on the front-end that auto-fills the
Persondata (first name, last name, age) randomly, facilitating easy testing.
-
DTO (Data Transfer Object):
Person: Represents the data model exchanged through Kafka.
-
Sender:
KafkaSender: Implements theISenderinterface to sendPersonobjects to specific topics.
-
Receiver:
KafkaReceiver: Implements theIReceiverinterface to listen to Kafka topics and process messages viaIProcess.
-
Configurations:
KafkaSenderConfig: Configures the Kafka producer.KafkaReceiverConfig: Configures the Kafka consumer.KafkaTopicConfig: Manages Kafka topics.
-
UI Layer (Thymeleaf + SSE):
- Thymeleaf Templates (
index.html): Rendered server-side, these pages include JavaScript to connect via SSE. - SSE Endpoint (
/stream): Keeps a server-sent event connection open, pushing new messages to the client in real-time. - AJAX Submission: The form is submitted with
fetch()to avoid page reloads, maintaining a continuous SSE stream. - Random Data Generation Button: Quickly fill in the form with random
Persondata, making testing simpler.
- Thymeleaf Templates (
-
Docker Compose:
- Sets up Zookeeper, Kafka, and Kafka Manager for a local development environment.
+-------------------+ +--------------------+
| Thymeleaf UI | <---- | SSE (Spring Boot) |
| (Rendered by SB) | | KafkaSSEController |
| - Live updates | | |
| - Send Person AJAX| | |
+-------------------+ +--------------------+
^ |
| v
| +-------------+
| | Kafka Topics|
| | - Tuto1 |
| | - Tuto2 |
| +-------------+
| ^
| |
v |
+-------------------+ +--------------------+
| KafkaSender | | KafkaReceiver |
| - Sends Person | ---> | - Listens Topics |
| | | - Pushes SSE |
+-------------------+ +--------------------+
|
v
+--------------------+
| Kafka Manager |
| & Zookeeper |
| + Kafka Broker |
+--------------------+.
├── HELP.md
├── README.md
├── mvnw
├── mvnw.cmd
├── pom.xml
├── project-structure.txt
└── src
└── main
├── java
│ └── com
│ └── kitano
│ └── kafka
│ ├── KafkaApplication.java
│ ├── dto
│ │ └── Person.java
| ├── iface
│ │ ├── ISender.java
│ │ ├── IReceiver.java
│ ├── receiver
│ │ ├── KafkaReceiverImpl.java
│ │ ├── KafkaReceiverConfig.java
│ ├── sender
│ │ ├── KafkaSenderImpl.java
│ │ ├── KafkaSenderConfig.java
│ │ └── KafkaTopicConfig.java
│ ├── service
│ │ └── KafkaListenerService.java
│ └── ui
│ ├── KafkaSSEController.java
│ └── KafkaUIController.java
└── resources
├── application.properties
└── templates
└── index.html
- Docker Desktop: Download and install Docker Desktop.
- Verify installation:
docker --version docker-compose --version
-
Clone the project repository:
git clone <repository-url> cd <repository-directory>
-
Locate the
docker-compose.ymlfile in theDOCSdirectory and run:docker-compose up
- Open Kafka Manager:
- Navigate to
http://localhost:9000. - Add a new cluster:
- Cluster Name:
kafka1 - Zookeeper Host:
zookeeper:2181
- Cluster Name:
- Navigate to
-
Start the Spring Boot application:
./mvnw spring-boot:run
-
Access the UI at
http://localhost:8080:- A form to send a
Personobject. - A "Générer aléatoirement" button to fill the form fields with random data.
- A live-updating message list, showing new Kafka messages in real-time via SSE.
- A form to send a
-
Sending Data to Kafka:
- Either manually fill in
firstName,lastName,ageor click the random button. - Click "Send Person" to submit the data asynchronously to the server, which sends it into Kafka.
- Either manually fill in
-
Receiving Data:
- The
KafkaListenerServicelistens onTuto1andTuto2and updates the UI via SSE. - Messages appear instantly in the message list as they are received.
- The
-
Kafka Manager:
- Check
http://localhost:9000. - Verify that
Tuto1andTuto2topics exist and are receiving messages.
- Check
-
UI Real-Time Updates:
- The
index.htmlathttp://localhost:8080updates as soon as messages arrive. - The random data generation button streamlines testing multiple
Personobjects.
- The
-
Console Output (Optional):
- The application logs display sent and received messages.
Console:
Message To Send : topicName=Tuto1, person={firstName:"Jean", lastName:"DUPOND", age:10}
Message Received: info=Tuto1Info, person={firstName:"Jean", lastName:"DUPOND", age:10}
UI:
- After clicking "Générer aléatoirement" and "Send Person", the UI lists the received message:
Received: Person(firstName=Alice, lastName=Martin, age=29)
- Customizing Kafka Address: Update
application.propertiesor environment variables forkafka.bootstrapAddress. - Docker Volumes: Modify
docker-compose.ymlif persistent storage is needed. - Error Handling: For production, consider adding more robust error handling and retries.
-
Cannot connect to Kafka:
- Ensure
docker-compose upis running. - Verify
kafka.bootstrapAddressin your configuration.
- Ensure
-
Topics not visible in Kafka Manager:
- Confirm Zookeeper and Kafka are running.
- Double-check Zookeeper host:
zookeeper:2181.
-
No messages visible in UI:
- Check Kafka Manager to ensure topics have data.
- Ensure no network or firewall issues are blocking SSE.
This project provides a starting point for integrating Apache Kafka with a Spring Boot application, showcasing how to send, receive, and display messages in real-time with a Thymeleaf-based front-end and SSE. The random data generation feature simplifies testing. For more details on Kafka setup, please refer to the original article.


