Steps I Have Followed(windows):
- First of all download apache kafka. from here.
- After downloading the kafka_2.12-2.3.0.tgz file extracted it with winzip.
- Go to the kafka_2.12-2.3.0 directory.
- Run the command from the cmd
bin/windows/zookeeper-server-start.bat C:/Apache/kafka_2.12-2.3.0/config/zookeeper.properties
. - I have followed their official site at Apache Kafka site.
- I had use the full path for configuration file, otherwise it was giving error(not find).
- After lots of messages I got zookeeper running on port 2181 which is default in the configuration file.
INFO binding to port 0.0.0.0/0.0.0.0:2181
. - I have updated my configuration for
dataDir
as../logs
.
- Go to the kafka_2.12-2.3.0 directory.
- Run the command from the cmd
bin/windows/kafka-server-start.bat C:/Apache/kafka_2.12-2.3.0/config/server.properties
. - I couldn't run it at the first attempt! Got an error as
'wmic' is not recognized as an internal or external command, operable program or batch file
. - So I had to add
C:\Windows\System32\wbem
as path variable in the Environment Variables. - Again run the command.
- This time after lots of messages I got kafka server running on port 9092 which is default in the configuration file.
INFO Awaiting socket connections on 0.0.0.0:9092
. - I have updated my configuration for
dataDir
as../logs
here as well.
- Got to the kafka_2.12-2.3.0 directory.
- Run the command from the cmd
bin/windows/kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic users
. - Here my replica is 1, partition is 1 and topic name is users(in my code I named the topic name as users).
- I have checked the topic list by running the command
bin/windows/kafka-topics.bat --list --bootstrap-server localhost:9092
. - That's all command line part.
So, my zookeeper and kafka server are up & running.
- Go to Spring Initializer.
- Add Web & Kafka dependency.
- Create the project.
- Configure the kafka configuration in
application.properties
. - Created a producer, a consumer & one controller with an endpoint.
- Run the project.
- When the project is running go to the endpoint in the browser
http://localhost:9000/kafka/publish/myMessage
. - The producer will publish the message & the consumer will consume it.
- Both will log the message in the console.