kafka单节点部署无法访问问题解决

时间:2020-12-20 21:32:34

场景:在笔记本安装了一台虚拟机, 在本地的虚拟机上部署了一个kafka服务:

写了一个测试程序,在笔记本上运行测试程序,访问虚拟机上的kafka,报如下异常:

2015-01-15 09:33:26 [kafka.producer.async.DefaultEventHandler]-[INFO] Back off for 100 ms before retrying send. Remaining retries = 1

2015-01-15 09:33:26 [kafka.client.ClientUtils$]-[INFO] Fetching metadata from broker id:0,host:192.168.108.136,port:9092 with correlation id 9 for 1 topic(s) Set(client-all-log-test)

2015-01-15 09:33:26 [kafka.producer.SyncProducer]-[INFO] Connected to 192.168.108.136:9092 for producing

2015-01-15 09:33:26 [kafka.producer.SyncProducer]-[INFO] Disconnecting from 192.168.108.136:9092

2015-01-15 09:33:47 [kafka.producer.SyncProducer]-[ERROR] Producer connection to ubuntu-machine:9092 unsuccessful

java.net.ConnectException : Connection timed out: connect

at sun.nio.ch.Net.connect0( Native Method)

at sun.nio.ch.Net.connect(Unknown Source)

at sun.nio.ch.Net.connect(Unknown Source)

at sun.nio.ch.SocketChannelImpl.connect(Unknown Source)

at kafka.network.BlockingChannel.connect(BlockingChannel.scala:57)

at kafka.producer.SyncProducer.connect(SyncProducer.scala:141)

at kafka.producer.SyncProducer.getOrMakeConnection(SyncProducer.scala:156)

at kafka.producer.SyncProducer.kafka$producer$SyncProducer$$doSend(SyncProducer.scala:68)

at kafka.producer.SyncProducer$$anonfun$send$1$$anonfun$apply$mcV$sp$1.apply$mcV$sp(SyncProducer.scala:102)

at kafka.producer.SyncProducer$$anonfun$send$1$$anonfun$apply$mcV$sp$1.apply(SyncProducer.scala:102)

at kafka.producer.SyncProducer$$anonfun$send$1$$anonfun$apply$mcV$sp$1.apply(SyncProducer.scala:102)

at kafka.metrics.KafkaTimer.time(KafkaTimer.scala:33)

at kafka.producer.SyncProducer$$anonfun$send$1.apply$mcV$sp(SyncProducer.scala:101)

at kafka.producer.SyncProducer$$anonfun$send$1.apply(SyncProducer.scala:101)

at kafka.producer.SyncProducer$$anonfun$send$1.apply(SyncProducer.scala:101)

at kafka.metrics.KafkaTimer.time(KafkaTimer.scala:33)

at kafka.producer.SyncProducer.send(SyncProducer.scala:100)

at kafka.producer.async.DefaultEventHandler.kafka$producer$async$DefaultEventHandler$$send(DefaultEventHandler.scala:255)

at kafka.producer.async.DefaultEventHandler$$anonfun$dispatchSerializedData$2.apply(DefaultEventHandler.scala:106)

at kafka.producer.async.DefaultEventHandler$$anonfun$dispatchSerializedData$2.apply(DefaultEventHandler.scala:100)

at scala.collection.mutable.HashMap$$anonfun$foreach$1.apply(HashMap.scala:95)

at scala.collection.mutable.HashMap$$anonfun$foreach$1.apply(HashMap.scala:95)

at scala.collection.Iterator$class.foreach(Iterator.scala:772)

at scala.collection.mutable.HashTable$$anon$1.foreach(HashTable.scala:157)

at scala.collection.mutable.HashTable$class.foreachEntry(HashTable.scala:190)

at scala.collection.mutable.HashMap.foreachEntry(HashMap.scala:45)

at scala.collection.mutable.HashMap.foreach(HashMap.scala:95)

at kafka.producer.async.DefaultEventHandler.dispatchSerializedData(DefaultEventHandler.scala:100)

at kafka.producer.async.DefaultEventHandler.handle(DefaultEventHandler.scala:72)

at kafka.producer.Producer.send(Producer.scala:76)

at kafka.javaapi.producer.Producer.send(Producer.scala:33)

at me.learn.kafka.demo.TestProducer.main( TestProducer.java:38)

Exception in thread "main" kafka.common.FailedToSendMessageException : Failed to send messages after 3 tries.

at kafka.producer.async.DefaultEventHandler.handle(DefaultEventHandler.scala:90)

at kafka.producer.Producer.send(Producer.scala:76)

at kafka.javaapi.producer.Producer.send(Producer.scala:33)

at me.learn.kafka.demo.TestProducer.main( TestProducer.java:38)

原因应该网络问题,但是检查了防火墙设置,没有限制,将程序部署的虚拟机所在的服务器上,就可以运行了,说明还是网络的限制。

需要改动config文件夹下的server.properties中的以下两个属性

zookeeper.connect=localhost:2181 #改成zookeeper.connect=10.0.30.221:2181
#去掉host.name的注释
#host.name=localhost改成host.name=10.0.30.221