本地Consumer和Producer无法使用远程Kafka服务器的处理办法

服务搭建好kafka服务后,本机测试ok,外面机器却无法访问,很是怪异。

kafka服务器: 
阿里云VPC网络服务器,内网ip:10.10.10.10,绑定的弹性ip(外网ip)x.x.x.x,是单机测试环境,zk和kafka都在一台机器上,使用默认端口,kakfa是9092,zookeeper是2181。kafka版本:kafka_2.11-0.10.1.0

kafka是默认配置,没有修改:

#listeners=PLAINTEXT://:9092
#advertised.listeners=PLAINTEXT://your.host.name:9092
zookeeper.connect=localhost:2181
  • 1
  • 2
  • 3

测试发现本机,可以正常发布消息、消费消息,但是公司机器不可以。

看log发现是hostname无法识别,所以最简单的方案就是: 
1,本机绑定host,即修改/etc/hosts,添加10.10.10.10 hostname到hosts文件。

有没有不需要绑定hosts,更高大上的方案呢?有! 
2,经各种测试后发现,修改kafka的advertised.listeners配置即可:

#listeners=PLAINTEXT://:9092
advertised.listeners=PLAINTEXT://x.x.x.x:9092
zookeeper.connect=localhost:2181
  • 1
  • 2
  • 3

advertised.listeners官网有解释: 
Listeners to publish to ZooKeeper for clients to use, if different than the listeners above. In IaaS environments, this may need to be different from the interface to which the broker binds. If this is not set, the value for listeners will be used. 
详情:http://kafka.apache.org/documentation/#configuration

亲测,有了这个配置,kafka就会忽略listeners配置。

Logo

Kafka开源项目指南提供详尽教程,助开发者掌握其架构、配置和使用,实现高效数据流管理和实时处理。它高性能、可扩展,适合日志收集和实时数据处理,通过持久化保障数据安全,是企业大数据生态系统的核心。

更多推荐