This repository contains two Spring Boot applications that demonstrate Kafka messaging between microservices:
-
kafka-producer: Service that sends messages to Kafka
-
kafka-consumer: Service that consumes messages from Kafka
HTTP Client → kafka-producer (POST /kafka/send) → Kafka Topic → kafka-consumer
- Java 17+
- Spring Boot 3+
- Maven
- Docker and Docker Compose
- Kafka (local installation or via Docker)
-
Install and start Kafka locally: https://kafka.apache.org/downloads
-
Configure .environment. Use variables .environment_example from resources of app (producer/consumer)
-
Start the applications locally
- Start Kafka with Docker Compose:
cd docker-compose-kafka
docker-compose up -d
-
Configure .environment. Use variables from file .environment_example from resources of app ( producer/consumer)
-
Start the applications locally
-
Create environment file. Use variables from file .env_example.
-
Form jar files
mvn clean install
- Start all services
docker-compose up -d
Copy the example environment files and configure as needed
cp kafka-producer/src/main/resources/.environment_example kafka-producer/src/main/resources/.env
cp kafka-consumer/src/main/resources/.environment_example kafka-consumer/src/main/resources/.env
The Kafka topic is automatically created by the producer application with the following configuration:
-
Topic name: test-topic
-
Partitions: 3
-
Replication factor: 1 (for development)
Send Message
Endpoint: POST /kafka/send
Request:
curl -X POST http://localhost:8080/kafka/send \
-H "Content-Type: application/json" \
-d '{
"message": "My message"
}'
Response:
{
"status": "Message sent successfully",
"timestamp": "2025-09-17T12:36:30.6062484"
}
Check the kafka-consumer application logs to see consumed messages:
2025-09-17T12:36:30.634+03:00 INFO 10440 --- [kafka-consumer] [ntainer#0-0-C-1] c.e.k.consumer.KafkaConsumer : Received message: {
"message": "My message"
}
kafka-app/
├── kafka-producer/ # Producer application
│ ├── src/main/java/
│ ├── src/main/resources/
│ └── pom.xml
├── kafka-consumer/ # Consumer application
│ ├── src/main/java/
│ ├── src/main/resources/
│ └── pom.xml
├── docker-compose-kafka/ # Kafka infrastructure
│ └── docker-compose.yml
├── docker-compose.yml # Main application compose
├── .env_example # Environment variables template
└── README.md