天天看点

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:           
  • 1
  • 2
  • 3
  • 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:
zookeeper.connect=localhost:           
  • 1
  • 2
  • 3
  • 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配置。

http://blog.csdn.net/fengcai19/article/details/54695874?utm_source=itdadao&utm_medium=referral

大数据平台服务器处于两个网络中,其中内部网络用来进行数据交换和计算,配置万兆光纤网卡和光纤交换机;外部网络用来为其他部门提供服务、数据接口,这里使用的是千兆网络。Kafka的broker集群处于内部网络中,而外部网络需要订阅消费kafka中的留数据,就需要访问内网。这里我们需要添加接个配置以使kafka能够通过外网来访问:

我们先来看下Kafka几个参数的解释:

advertised.host.name Hostname to publish to ZooKeeper for clients to use. In IaaS environments, this may need to be different from the interface to which the broker binds. If this is not set, it will use the value for "host.name" if configured. Otherwise it will use the value returned from java.net.InetAddress.getCanonicalHostName().
host.name hostname of broker. If this is set, it will only bind to this address. If this is not set, it will bind to all interfaces
listeners Listener List – Comma-separated list of URIs we will listen on and their protocols. Specify hostname as 0.0.0.0 to bind to all interfaces. Leave hostname empty to bind to default interface. Examples of legal listener lists: PLAINTEXT://myhost:9092,TRACE://:9091 PLAINTEXT://0.0.0.0:9092, TRACE://localhost:9093

从官方的解释上,我们可以知道:

1.要使远程客户端和broker通信的时候指向正确的主机那么就需要指定advertised.host.name=bigdata3参数,这里bigdata3需要配置到hosts中

2.要使broker指向正确的网卡地址,那么我们就需要指定host.name=192.168.168.3,让broker之间通信使用内部网络

3.另外我们需要配置kafka监听所有的网卡:listeners=PLAINTEXT://0.0.0.0:9092

http://www.tuicool.com/articles/ye6Bfi