There are several broker examples

Keywords: kafka Zookeeper Windows Attribute

Original address: http://blog.csdn.net/shangboerds/article/details/38944743

-- Start

1. Start ZooKeeper

Start ZooKeeper first.

  1. set KAFKA_HOME=C:/dev/kafka_2.10-0.8.2.2  
  2. cd %KAFKA_HOME%/bin/windows  
  3. zookeeper-server-start.bat %KAFKA_HOME%/config/zookeeper.properties  

2. Start Kafka borker

First, copy two copies of server.properties under config directory, named server-1.properties and server-2.properties respectively. Then modify the following attributes of these two files to ensure the existence of the log directory.

server-1.properties:

  1. broker.id=1  
  2. port=9093  
  3. log.dirs=C:/dev/kafkaLogDir/broker1  

server-2.properties:

  1. broker.id=2  
  2. port=9094  
  3. log.dirs=C:/dev/kafkaLogDir/broker2  


broker.id is used to uniquely identify each broker. Since we are running three brokers on the same machine, we also need to modify the port and log.dirs attributes. Of course, there are many other attributes in this file that we can set, and each attribute is explained. You can take a closer look at this attribute file.

Now we have three broker s, which are started on three command lines.

  1. set KAFKA_HOME=C:/dev/kafka_2.10-0.8.2.2  
  2. cd %KAFKA_HOME%/bin/windows  
  3. kafka-server-start.bat %KAFKA_HOME%/config/server.properties  

  1. set KAFKA_HOME=C:/dev/kafka_2.10-0.8.2.2  
  2. cd %KAFKA_HOME%/bin/windows  
  3. kafka-server-start.bat %KAFKA_HOME%/config/server-1.properties  

  1. set KAFKA_HOME=C:/dev/kafka_2.10-0.8.2.2  
  2. cd %KAFKA_HOME%/bin/windows  
  3. kafka-server-start.bat %KAFKA_HOME%/config/server-2.properties  


3. Create topic

Open a new command prompt, and the following command creates a topic named topic1.

  1. set KAFKA_HOME=C:/dev/kafka_2.10-0.8.2.2  
  2. cd %KAFKA_HOME%/bin/windows  
  3. kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 2 --partitions 3 --topic topic1  

Have you ever wondered what these parameters of create mean? Where can I find out what they mean? Try the following commands.

  1. kafka-topics.bat  

Maybe you've seen what these parameters mean, but you don't necessarily understand what replication-factor s and partitions really mean. Now let's look at the relationship between topic, partition and log. You can understand topic as folder, partition. For subfolders under topic, log is under partition, and messages are saved in log. So in the above command -- partitions 3 means creating three partitions under topic1. So what does replication-factor 2 mean? It means copying any partition onto two brokers, so if one broker hangs up, we can still get messages from another broker. What if two brokers hang up? Ha-ha, that's really hanging up. What about? Not yet understood? Never mind, just look at the directory structure below.


After a while, you may have forgotten how topic1 is defined. If you want to see the details of topic1, try the following command.

  1. kafka-topics.bat --describe --zookeeper localhost:2181 --topic topic1  

The above command yields the following output:

  1. Topic: topic1   PartitionCount:3        ReplicationFactor:2     Configs:  
  2. Topic: topic1   Partition: 0    Leader: 0       Replicas: 0,2   Isr: 0,2  
  3. Topic: topic1   Partition: 1    Leader: 1       Replicas: 1,0   Isr: 1,0  
  4. Topic: topic1   Partition: 2    Leader: 2       Replicas: 2,1   Isr: 2,1  

The first line is an overview of topic. The next three lines describe each partition.

Each partition has a broker as leader, which is responsible for all read and write operations within the partition, while the other brokers passively copy leader broker. If leader broker hangs up, one of the other brokers will automatically become the new leader of the partition.

Replicas: 0,1 means that the partition is stored under broker 0 and broker 1.

Isr: 0,1 means that we can access the partition under broker 0 and broker 1 at present. If broker 0 hangs up, it will look like this.


4. Send messages

The following command sends a message to topic1.

  1. set KAFKA_HOME=C:/dev/kafka_2.10-0.8.2.2  
  2. cd %KAFKA_HOME%/bin/windows  
  3. kafka-console-producer.bat --broker-list localhost:9092 --topic topic1  
  4. This is a message  


5. Receiving messages

The following command receives messages from topic1.

  1. set KAFKA_HOME=C:/dev/kafka_2.10-0.8.2.2  
  2. cd %KAFKA_HOME%/bin/windows  
  3. kafka-console-consumer.bat --zookeeper localhost:2181 --topic topic1 --from-beginning  

6. Close broker 1

Now we close the command window that starts broker 1. Then use the following command to review the status of topic1 again.

  1. kafka-topics.bat --describe --zookeeper localhost:2181 --topic topic1  

  1. Topic:topic1    PartitionCount:3        ReplicationFactor:2     Configs:  
  2. Topic: topic1   Partition: 0    Leader: 0       Replicas: 0,2   Isr: 0,2  
  3. Topic: topic1   Partition: 1    Leader: 0       Replicas: 1,0   Isr: 0  
  4. Topic: topic1   Partition: 2    Leader: 2       Replicas: 2,1   Isr: 2  

You can try sending another message to see if you can receive it.

You can also close another broker and try sending a message to see what happens.

Posted by majik_sheff on Thu, 11 Jul 2019 17:43:47 -0700