1. 程式人生 > >Kafka叢集無法外網訪問問題

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