Kafka集群无法外网访问问题解决攻略

来源:互联网 发布:手机app数据库设计 编辑:程序博客网 时间:2024/05/06 00:12

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:9092zookeeper.connect=localhost:2181

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

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

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

#listeners=PLAINTEXT://:9092advertised.listeners=PLAINTEXT://x.x.x.x:9092zookeeper.connect=localhost:2181

成功通过测试,完美解决问题。

关于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配置。

0 0