Stop Kafka service:kafka_2.12-0.10.2.1> bin/kafka-server-stop.shkafka_2.12-0.10.2.1> bin/ Zookeeper-server-stop.shstep 1: Download Kafka download the latest version and unzip .>
Tar-xzf kafka_2.12-0.10.2.1.tgz> CD Kafka_2.12-0.10.2.1step 2: Start the service Kafka used to zookeeper, all first start Zookper, the following simple to enable a single instance of the Zookkeeper service. You can add a & symbol at the end of the command so that you can start and leave the console. > bin/zookeeper-server-start.sh config/zookeeper.properties & start kafka:> bin/kafka-server-start.sh config now /server.propertiesstep 3: Create topic Create a topic called "Test", which has only one partition and one copy.
> bin/kafka-topics.sh--create--zookeeper localhost:2181--replication-factor 1--partitions 1--topic test
You can view the created topic:> bin/kafka-topics.sh--list--zookeeper localhost:2181test with the list command, in addition to manually creating topic, You can also configure the broker to have it automatically create topic. Step 4: Send a message. Kafka uses a simple command-line producer to read the message from the file or from the standard input and send it to the server. A message is sent by default for each command. Run producer and lose some messages in the console that will be sent to the server:
> bin/kafka-console-producer.sh--broker-list localhost:9092--topic Test this was a messagethis is another message< /c0>
CTRL + C can exit send. MAC press ENTER to send a message Step 5: Start Consumerkafka also has a command line consumer, will dump out messages to standard output. Kafka also has a command line consumer can read the message and output to the standard output:> bin/kafka-console-consumer.sh--zookeeper localhost:2181--topic test-- From-beginningthis is a messagethis is another message you run the consumer command line in one terminal, the other terminal runs the producer command line, you can enter messages in a terminal, The other terminal reads the message. Both commands have their own optional parameters that can be used without any parameters to see the help information at run time. Step 6: Build a cluster of multiple brokers just started a single broker, and now starts a cluster of 3 brokers, all of which are on this machine: first write a configuration file for each node: > CP config/ Server.properties config/server-1.properties
> CP config/server.properties config/server-2.properties
Add the following parameters to the copied new file: config/server-1.properties:broker.id=1 port=9093 log.dir=/tmp/kafka-logs-1 Config/server-2.prope rties:broker.id=2 port=9094 log.dir=/tmp/kafka-logs-2 broker.id The only label in the cluster is a node, because on the same machine, different ports and log files must be developed to prevent data from being overwritten. We already has Zookeeper and our single node started, so we just need to start the two new nodes: just now the Zookeeper and a node have been launched, now At the start of another two nodes:> bin/kafka-server-start.sh config/server-1.properties &...> bin/kafka-server-start.sh config/ Server-2.properties & Create a topic with 3 replicas:
> bin/kafka-topics.sh--create--zookeeper localhost:2181--replication-factor 3--partitions 1--topic My-replicated-topic
Now that we've built a cluster, how do we know what each node is about? Run the "Describe topics" command to:> bin/kafka-topics.sh--describe--zookeeper localhost:2181--topic My-replicated-topictopic:my-replicated-topic partitioncount:1 replicationfactor:3 configs:topic: My-replicated-topic partition:0 leader:1 replicas:1,2,0 isr:1,2,0 explain the output below. The first line is a description of all the partitions, and then each partition corresponds to one row, because we only have a single partition, so we add a row below.
- Leader: Responsible for processing the read and write of messages, leader is randomly selected from all nodes.
- Replicas: Lists all replica nodes, regardless of whether the nodes are in the service.
- ISR: is the node in service.
In our example, Node 1 is run as leader. Send message to topic: > bin/kafka-console-producer.sh--broker-list localhost:9092--topic my-replicated-topic...my Test message 1my test message 2^c consume these messages: > bin/kafka-console-consumer.sh--zookeeper localhost:2181-- from-beginning--topic my-replicated-topic...my Test message 1my Testing message 2^c tests for fault tolerance. Broker 1 runs as a leader, Now we kill it: > ps | grep server-1.properties7564 ttys002 0:15.91/system/library/frameworks/javavm.framework/ Versions/1.6/home/bin/java...> kill-9 7564 Another node has been selected Leader,node 1 no longer appears in the In-sync replica list: > bin/ kafka-topics.sh--describe--zookeeper localhost:218192--topic my-replicated-topictopic:my-replicated-topic partitioncount:1 Replicationfactor:3 configs: topic:my-replicated-topic partition:0 LEADER:2&NBsp; replicas:1,2,0 isr:2,0 Although the original responsible for the continuation of the message leader down, but the previous message can be consumed:> bin/kafka-console-consumer.sh--zookeeper localhost:2181--from-beginning--topic my-replicated-topic...my test Message 1my Test Message 2
Build Kafka Run environment-mac version