Kafka系列2-producer和consumer报错
1. 使用127.0.0.1启动生产和消费进程:
1)启动生产者进程:
bin/kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic test
输入消息:
this is msg
生产者进程报错:
- [2016-06-03 11:33:47,934] WARN Bootstrap broker 127.0.0.1:9092 disconnected (org.apache.kafka.clients.NetworkClient)
- [2016-06-03 11:33:49,554] WARN Bootstrap broker 127.0.0.1:9092 disconnected (org.apache.kafka.clients.NetworkClient)
- [2016-06-03 11:33:51,177] WARN Bootstrap broker 127.0.0.1:9092 disconnected (org.apache.kafka.clients.NetworkClient)
- [2016-06-03 11:33:53,398] WARN Bootstrap broker 127.0.0.1:9092 disconnected (org.apache.kafka.clients.NetworkClient)
2)启动消费者进程:
bin/kafka-console-consumer.sh --zookeeper 127.0.0.1:2181 --topic test --from-beginning
消费者进程报错:
- [2016-06-03 11:34:53,574] WARN Fetching topic metadata with correlation id 0 for topics [Set(test)] from broker [BrokerEndPoint(0,218.30.64.194,9092)] failed (kafka.client.ClientUtils$)
- java.nio.channels.ClosedChannelException
- at kafka.network.BlockingChannel.send(BlockingChannel.scala:110)
- at kafka.producer.SyncProducer.liftedTree1$1(SyncProducer.scala:80)
- at kafka.producer.SyncProducer.kafka$producer$SyncProducer$$doSend(SyncProducer.scala:79)
- at kafka.producer.SyncProducer.send(SyncProducer.scala:124)
- at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:59)
- at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:94)
- at kafka.consumer.ConsumerFetcherManager$LeaderFinderThread.doWork(ConsumerFetcherManager.scala:66)
- at kafka.utils.ShutdownableThread.run(ShutdownableThread.scala:63)
- [2016-06-03 11:34:53,651] WARN [console-consumer-72675_zzs-1464924871670-2192d80a-leader-finder-thread], Failed to find leader for Set([test,0]) (kafka.consumer.ConsumerFetcherManager$LeaderFinderThread)
- kafka.common.KafkaException: fetching topic metadata for topics [Set(test)] from broker [ArrayBuffer(BrokerEndPoint(0,218.30.64.194,9092))] failed
- at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:73)
- at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:94)
- at kafka.consumer.ConsumerFetcherManager$LeaderFinderThread.doWork(ConsumerFetcherManager.scala:66)
- at kafka.utils.ShutdownableThread.run(ShutdownableThread.scala:63)
- Caused by: java.nio.channels.ClosedChannelException
- at kafka.network.BlockingChannel.send(BlockingChannel.scala:110)
- at kafka.producer.SyncProducer.liftedTree1$1(SyncProducer.scala:80)
- at kafka.producer.SyncProducer.kafka$producer$SyncProducer$$doSend(SyncProducer.scala:79)
- at kafka.producer.SyncProducer.send(SyncProducer.scala:124)
- at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:59)
- ... 3 more
- [2016-06-03 11:35:14,916] WARN Fetching topic metadata with correlation id 1 for topics [Set(test)] from broker [BrokerEndPoint(0,218.30.64.194,9092)] failed (kafka.client.ClientUtils$)
- java.nio.channels.ClosedChannelException
- at kafka.network.BlockingChannel.send(BlockingChannel.scala:110)
- at kafka.producer.SyncProducer.liftedTree1$1(SyncProducer.scala:80)
- at kafka.producer.SyncProducer.kafka$producer$SyncProducer$$doSend(SyncProducer.scala:79)
- at kafka.producer.SyncProducer.send(SyncProducer.scala:124)
- at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:59)
- at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:94)
- at kafka.consumer.ConsumerFetcherManager$LeaderFinderThread.doWork(ConsumerFetcherManager.scala:66)
- at kafka.utils.ShutdownableThread.run(ShutdownableThread.scala:63)
- [2016-06-03 11:35:14,918] WARN [console-consumer-72675_zzs-1464924871670-2192d80a-leader-finder-thread], Failed to find leader for Set([test,0]) (kafka.consumer.ConsumerFetcherManager$LeaderFinderThread)
- kafka.common.KafkaException: fetching topic metadata for topics [Set(test)] from broker [ArrayBuffer(BrokerEndPoint(0,218.30.64.194,9092))] failed
- at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:73)
- at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:94)
- at kafka.consumer.ConsumerFetcherManager$LeaderFinderThread.doWork(ConsumerFetcherManager.scala:66)
- at kafka.utils.ShutdownableThread.run(ShutdownableThread.scala:63)
- Caused by: java.nio.channels.ClosedChannelException
- at kafka.network.BlockingChannel.send(BlockingChannel.scala:110)
- at kafka.producer.SyncProducer.liftedTree1$1(SyncProducer.scala:80)
- at kafka.producer.SyncProducer.kafka$producer$SyncProducer$$doSend(SyncProducer.scala:79)
- at kafka.producer.SyncProducer.send(SyncProducer.scala:124)
- at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:59)
- ... 3 more
2 使用localhost启动生产和消费进程:
1)启动生产者进程:
- [root@zzs kafka_2.11-0.10.0.0]# bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
- this is msg
- [2016-06-03 11:44:16,932] WARN Error while fetching metadata with correlation id 0 : {test=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
- [2016-06-03 11:44:18,255] WARN Error while fetching metadata with correlation id 1 : {test=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
- [2016-06-03 11:44:18,648] WARN Error while fetching metadata with correlation id 2 : {test=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
- [2016-06-03 11:44:18,801] WARN Error while fetching metadata with correlation id 3 : {test=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
- [2016-06-03 11:44:18,928] WARN Error while fetching metadata with correlation id 4 : {test=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
- [2016-06-03 11:44:19,035] WARN Error while fetching metadata with correlation id 5 : {test=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
- [2016-06-03 11:44:19,180] WARN Error while fetching metadata with correlation id 6 : {test=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
- [2016-06-03 11:44:19,308] WARN Error while fetching metadata with correlation id 7 : {test=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
2)启动消费者进程:
- [root@zzs kafka_2.11-0.10.0.0]# bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
- [2016-06-03 11:45:18,330] WARN Fetching topic metadata with correlation id 0 for topics [Set(test)] from broker [BrokerEndPoint(0,218.30.64.194,9092)] failed (kafka.client.ClientUtils$)
- java.nio.channels.ClosedChannelException
- at kafka.network.BlockingChannel.send(BlockingChannel.scala:110)
- at kafka.producer.SyncProducer.liftedTree1$1(SyncProducer.scala:80)
- at kafka.producer.SyncProducer.kafka$producer$SyncProducer$$doSend(SyncProducer.scala:79)
- at kafka.producer.SyncProducer.send(SyncProducer.scala:124)
- at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:59)
- at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:94)
- at kafka.consumer.ConsumerFetcherManager$LeaderFinderThread.doWork(ConsumerFetcherManager.scala:66)
- at kafka.utils.ShutdownableThread.run(ShutdownableThread.scala:63)
- [2016-06-03 11:45:18,541] WARN [console-consumer-42554_zzs-1464925496436-ae2ee9c7-leader-finder-thread], Failed to find leader for Set([test,0]) (kafka.consumer.ConsumerFetcherManager$LeaderFinderThread)
- kafka.common.KafkaException: fetching topic metadata for topics [Set(test)] from broker [ArrayBuffer(BrokerEndPoint(0,218.30.64.194,9092))] failed
- at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:73)
- at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:94)
- at kafka.consumer.ConsumerFetcherManager$LeaderFinderThread.doWork(ConsumerFetcherManager.scala:66)
- at kafka.utils.ShutdownableThread.run(ShutdownableThread.scala:63)
- Caused by: java.nio.channels.ClosedChannelException
- at kafka.network.BlockingChannel.send(BlockingChannel.scala:110)
- at kafka.producer.SyncProducer.liftedTree1$1(SyncProducer.scala:80)
- at kafka.producer.SyncProducer.kafka$producer$SyncProducer$$doSend(SyncProducer.scala:79)
- at kafka.producer.SyncProducer.send(SyncProducer.scala:124)
- at kafka.client.ClientUtils$.fetchTopicMetadata(ClientUtils.scala:59)
- ... 3 more
3.解决问题
1)查看Kafka的配置文件,cat config/server.properties
- zookeeper.connect=localhost:2181
连接的zookeeper的为localhost,所以需要用localhost启动生产和消费进程
2)查看kafka启动的日志,发现
- Registered broker 0 at path /brokers/ids/0 with addresses: PLAINTEXT -> EndPoint(218.30.64.194,9092,PLAINTEXT) (kafka.utils.ZkUtils)
为什么启动的broker的ip是 218.30.64.194
==> 没有绑定Kafka启动监听的host信息
vi config/server.properties
- listeners=PLAINTEXT://localhost:9092
3)重新启动zookeeper、kafka、consumer、producer
./bin/zookeeper-server-start.sh ./config/zookeeper.properties
./bin/kafka-server-start.sh ./config/server.properties
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
在producer中输入消息,可以在producer中消费:
- [root@zzs kafka_2.11-0.10.0.0]# bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
- this is msg
- this is msg2
- [root@zzs kafka_2.11-0.10.0.0]# bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
- this is msg
- this is msg2
- this is msg3
- this is mgs4
问题解决!