在使用kafka 本地消费远程服务器的时候遇到问题,
[2016-01-17 23:17:52,291] ERROR Unknown error when running consumer: (kafka.tools.ConsoleConsumer$)
java.net.UnknownHostException: localhost.9092, 127..0.01:9092 未知的名称或服务
at java.net.InetAddress.getLocalHost(InetAddress.java:1475)
at kafka.consumer.ZookeeperConsumerConnector.<init>(ZookeeperConsumerConnector.scala:122)
at kafka.consumer.ZookeeperConsumerConnector.<init>(ZookeeperConsumerConnector.scala:146)
at kafka.consumer.Consumer$.create(ConsumerConnector.scala:109)
at kafka.consumer.OldConsumer.<init>(BaseConsumer.scala:73)
at kafka.tools.ConsoleConsumer$.run(ConsoleConsumer.scala:63)
at kafka.tools.ConsoleConsumer$.main(ConsoleConsumer.scala:47)
at kafka.tools.ConsoleConsumer.main(ConsoleConsumer.scala)
Caused by: java.net.UnknownHostException: localhost.localdomain: 未知的名称或服务
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:901)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1295)
at java.net.InetAddress.getLocalHost(InetAddress.java:1471)
... 7 more
解决方案:
kafka服务器配置问题,咱们先看一段kafka的配置描述
# Hostname and port the broker will advertise to producers and consumers. If not set,
# it uses the value for "listeners" if configured. Otherwise, it will use the value
在我们使用默认配置时hostname和advertise.host.name是被注释掉的,所以系统会调用InetAddress.getCanonicalHostName()方法获取,这个值一般是localhost,如果kafka的消费者或者生产者那到这个localhost只能去本机kafka服务器消费或生产消息,所以当我们在本机使用时没问题,而一旦连的是远程的kafka服务器,使用localhost去消费本机的消息,这肯定是不可以的,感觉kafka这样设计又一点不太方便。所以我们在使用远程kafka服务器时应该把../kafka/config/service.properties 中的advertised.host.name=<远程kafka服务器的ip地址>。
编辑server.properties
host.name=localhost 改为ip地址