This project demonstrates the usage of Apache Kafka with Spring Boot by implementing a simple library event system. It consists of a producer application and a consumer application that communicate through Kafka topics.
- Producer Service: Accepts REST API requests to create or update library events and publishes these events to a Kafka topic.
- Consumer Service: Listens to the Kafka topic, processes library events, and persists them to a database. It also handles validation, error handling, and retry logic.
- Spring Boot based microservices for both producer and consumer.
- REST endpoints to publish (
POST
) and update (PUT
) library events. - Kafka integration for event-driven communication.
- Error handling with retries and dead-letter topics.
- Persistence using Spring Data JPA.
- Integration and unit tests for robust validation.
kafka-poc/
│
├── library-producer/ # Producer application
│ └── src/
│ └── main/
│ └── java/com/kafkalearning/libraryproducer/
│ ├── controller/ # Exposes REST API endpoints
│ ├── producer/ # Publishes events to Kafka
│ └── domain/ # Event and domain models
│
├── library-consumer/ # Consumer application
│ └── src/
│ └── main/
│ └── java/com/learnkafka/libraryconsumer/
│ ├── consumer/ # Kafka listener
│ ├── service/ # Event processing logic
│ ├── jpa/ # Data persistence layer
│ └── entity/ # JPA entities
-
Producer Side:
- Exposes REST endpoints (
/v1/libraryevent
) for creating and updating library events. - Serializes event objects to JSON and publishes them to the
library-events
Kafka topic.
- Exposes REST endpoints (
-
Consumer Side:
- Listens to the
library-events
topic. - Deserializes the event, validates, and saves it to the database.
- If errors occur, retries are handled and failed records may be persisted for further analysis.
- Listens to the
- Java
- Spring Boot
- Spring Data JPA
- Apache Kafka
- Lombok
- Embedded Kafka (for testing)
- JUnit
- Java 17+
- Maven
- Docker
-
Start Kafka and Zookeeper using Docker Compose
(Ensure Docker is running on your machine)docker-compose -f docker-compose-multi-broker.yml up
-
Start the Producer:
cd library-producer mvn spring-boot:run
-
Start the Consumer:
cd library-consumer mvn spring-boot:run
-
Create a Library Event (POST):
POST /v1/libraryevent Content-Type: application/json { "libraryEventId": null, "libraryEventType": "NEW", "book": { "bookId": 123, "bookName": "Kafka Using Spring Boot", "bookAuthor": "tanish" } }
-
Update a Library Event (PUT):
PUT /v1/libraryevent Content-Type: application/json { "libraryEventId": 123, "libraryEventType": "UPDATE", "book": { "bookId": 456, "bookName": "Kafka Using Spring Boot 3.0 and latest", "bookAuthor": "Tanish" } }
Both producer and consumer modules include unit and integration tests. To run tests:
mvn test