This is the kafka-producer implementation, and some commands to run the server
- Edit zookeeper.properties and server.properties to set the message persistent time, log location etc. [Windows Version]
- Start Zookeeper cluster as follow
.bin\windows\zookeeper-server-start.bat ..\..\config\zookeeper.properties
- Start Kafka cluster as follow
.bin\windows\kafka-server-start.bat ..\..\config\server.properties
- Creating a topic
.\kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 -topic chat
- List all topics
.\kafka-topics.bat --list --bootstrap-server localhost:9092
- Publishing message to kafka cluster
.\kafka-console-producer.bat --broker-list localhost:9092 --topic chat
- Add a consumer to consume the queue
.\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic chat --from-beginning
- Duplicate server.properties
- change
broker.id
to another ID, must be unique - change
listeners
to another port e.g.9093
, on production this will not be needed because differet brokers are on different computers - Change
log.dirs
to another path for it to persist all the data - launch 3 brokers
.\kafka-server-start.bat ..\..\config\server2.properties
& `.\kafka-server-start.bat ....\config\server1.properties - create a replication factor of 3 with 3 partitions
.\kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 3 --partitions 3 -topic purchases
. Do note that only a subset of servers should be bootstrap server - Describe the topic
.\kafka-topics.bat --describe --bootstrap-server localhost:9092 --topic purchases
.Isr
is in-sync-replicas which means they are in-sync if same with replicas. - Try sending 4 messages
.\kafka-console-producer.bat --broker-list localhost:9092 --topic purchases
- Consuming
.\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic purchases --from-beginning
do note that order is not same because within partition is an ordered queue but there is no order queue between different partitions.
- Send message to producer
.\kafka-console-producer.bat --broker-list localhost:9092 --topic events
- Consume message from consumer with a pub/sub pattern, specify all consumer into different groups
java -jar .\kafka-consumer.jar group2
java -jar .\kafka-consumer.jar
same group, only one will receive, kafka will do it in a round robin manner- If a message fails to deliver, Kafka will re-deliver when the consumer pulled check