This is a small Java Spring Boot application demonstration how to use Kafka in your applications. The Kafka broker is set up for local development by using corresponding docker containers.
This application implements the "Guess my number" game. Clients are in competition to find the number as quick as possible. Communication is done via Kafka events/topics.
- Make sure you have installed Docker; e.g. install Docker Desktop: https://www.docker.com/products/docker-desktop/
- Start the Docker containers for Zookeeper and Kafka by going in the folder
./docker
and startingdocker compose up -d
- (For redundancy two containers are started for each service (2x Zookeeper + 2x Kafka)) --> commented out per default
- Background information regarding Docker and Kafka can be found here: kafka-docker-setup
- Test access to the Kafka container. For example using the Big Data Tools from IntelliJ connecting to
localhost:29092
- Test that Zookeeper is running on
port 22181
; e.g. use another IntelliJ Plugin like zoolytic
Just run the application without arguments. It publishes five events to the demoTopic
and a consumer is logging those events into the console.
- List all topics:
kafka-topics --bootstrap-server kafka-1:9092 --list
- Delete a topic:
kafka-topics --bootstrap-server kafka-1:9092 --delete --topic name
- Show config of special topics:
kafka-topics --bootstrap-server kafka-1:9092 --describe --topics-with-overrides
- Read/ Replay / list events of the
demoTopic
:kafka-console-consumer --bootstrap-server kafka-1:9092 --topic demoTopic --from-beginning
- Delete topic (events):
kafka-topics --bootstrap-server kafka-1:9092 --delete --topic demoTopic
- Write events to a topic:
kafka-console-producer --bootstrap-server kafka-1:9092 --topic guessNumberTopic
- Read events from a topic:
kafka-console-consumer --bootstrap-server kafka-1:9092 --topic feedbackNumberTopic
-
Run one NumberIssuer-App. There should be just one instance. It is responsible for publishing a random number and evaluating the answers of the Guessers. Class
com.mos.kafka.kafkaguessnumber.KafkaGuessNumberApplication
in profilenumberIssuer
-
Run as many NumberGuesser instances as you like. Each of them is guessing the number. The feedback of the Issuer would be:
<
,>
,Matched
orInactive
. Start the same main classKafkaGuessNumberApplication
with the profilenumberGuesser
The Issuer and Guesser are communication via Kafka topics (see also next section). Notice the logs that show you what's going on and which Guesser instance is winning a challenge.
The Issuer and Guessers are communicating by using three different topics. The following diagramm summarizes the architecture:
Check also class GlobalDefs for global definitions.