A simple application showing how to publish JSON messages to kafka with Spring Boot.
- Java 11;
- Docker and Docker Compose;
The project uses docker-compose to set up zookeeper and kafka.
- Clone the project;
- Open a bash window inside the project root folder;
- Run
docker-compose up
command; - Start the spring application;
Messages can be published by requesting the REST API like below:
[POST] http://localhost:8085/kafka-example/producers
Content-Type: application/json
Body: { "message": "Test message" }
The topic should be created automatically, but in case it doesn't, execute the command below:
docker-compose exec kafka kafka-topics --create --topic kafka-producer-example --partitions 1 --replication-factor 1 --if-not-exists --zookeeper zookeeper:32181
To consult the published messages, execute the command below:
docker-compose exec kafka kafka-console-consumer --bootstrap-server kafka:29092 --topic kafka-producer-example --from-beginning