Kafka集群无法外网访问问题

本地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配置。

猜你喜欢

转载自blog.csdn.net/xiaofanren1111/article/details/80944005