Kafka连接服务器出现:Connection to node 1 (localhost/127.0.0.1:9092) could not be established.

今天遇到个很奇怪的问题,是关于外网连接Kafka的。

在服务器本地可以使用命令行参数连接并且进行发布订阅操作,但是在外网环境就不可以了,SpringBoot连接报错:

Connection to node 1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.

首先一点点来排查错误。

从错误信息可以看到,SpringBoot开启后连接的是127.0.0.1,也就是本地的Kafka broker,但是配置文件中配置的是服务器的IP地址。我本地也并没有安装Kafka和Zookeeper服务。

1.安全组是否开放&防火墙是否拦截请求

一般数据库或Redis连接不可用,都有可能是安全组没有开放或者防火墙拦截了外来连接导致。登陆阿里云服务器管理台后发现,9092端口和2181端口都是开放的,说明阿里云这边并没有关闭了Kafka和Zookeeper的外部连接权限,那么尝试下端口扫描,发现这两个端口依然可以被外部访问:

在这里插入图片描述

2.登陆服务器查看Kafka broker是否可用

xx@xx:~$ sudo lsof -i:9092
COMMAND   PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
java    11049 root  164u  IPv4 1349268      0t0  TCP *:9092 (LISTEN)
java    11049 root  180u  IPv4 1350151      0t0  TCP localhost:33640->localhost:9092 (ESTABLISHED)
java    11049 root  181u  IPv4 1349272      0t0  TCP localhost:9092->localhost:33640 (ESTABLISHED)

通过查询Kafka broker默认端口9092发现,服务运行状态良好,排除掉服务下线的可能性。

3.查看Kafka运行日志

xx@xx~/kafka_2.11-2.3.1/$ sudo tail -f logs/server.log
[2019-12-09 00:49:13,507] INFO [GroupMetadataManager brokerId=1] Finished loading offsets and group metadata from __consumer_offsets-24 in 1 milliseconds. (kafka.coordinator.group.GroupMetadataManager)
[2019-12-09 00:49:13,507] INFO [GroupMetadataManager brokerId=1] Finished loading offsets and group metadata from __consumer_offsets-27 in 0 milliseconds. (kafka.coordinator.group.GroupMetadataManager)
[2019-12-09 00:49:13,507] INFO [GroupMetadataManager brokerId=1] Finished loading offsets and group metadata from __consumer_offsets-30 in 0 milliseconds. (kafka.coordinator.group.GroupMetadataManager)
[2019-12-09 00:49:13,508] INFO [GroupMetadataManager brokerId=1] Finished loading offsets and group metadata from __consumer_offsets-33 in 1 milliseconds. (kafka.coordinator.group.GroupMetadataManager)
[2019-12-09 00:49:13,508] INFO [GroupMetadataManager brokerId=1] Finished loading offsets and group metadata from __consumer_offsets-36 in 0 milliseconds. (kafka.coordinator.group.GroupMetadataManager)
[2019-12-09 00:49:13,508] INFO [GroupMetadataManager brokerId=1] Finished loading offsets and group metadata from __consumer_offsets-39 in 0 milliseconds. (kafka.coordinator.group.GroupMetadataManager)
[2019-12-09 00:49:13,509] INFO [GroupMetadataManager brokerId=1] Finished loading offsets and group metadata from __consumer_offsets-42 in 1 milliseconds. (kafka.coordinator.group.GroupMetadataManager)
[2019-12-09 00:49:13,509] INFO [GroupMetadataManager brokerId=1] Finished loading offsets and group metadata from __consumer_offsets-45 in 0 milliseconds. (kafka.coordinator.group.GroupMetadataManager)
[2019-12-09 00:49:13,509] INFO [GroupMetadataManager brokerId=1] Finished loading offsets and group metadata from __consumer_offsets-48 in 0 milliseconds. (kafka.coordinator.group.GroupMetadataManager)
[2019-12-09 00:49:13,509] INFO [GroupMetadataManager brokerId=1] Finished loading offsets and group metadata from __consumer_offsets-3 in 0 milliseconds. (kafka.coordinator.group.GroupMetadataManager)

运行状态良好,没有出现WARNERROR信息,从日志看不出问题。

4.在网上找资料

既然自己菜,没能力找到问题所在,那么问问google,总有一些解决问题的方式,虽然并不一定能直接解决问题,但是在查找问题的过程中还是能学到很多,例如这篇博客也提到了Kafka外网连接问题,但按照这个方式修改后并不能解决我的问题:

kafka连接问题

这里面提到了一个很重要的概念:

kafka启动后会在zookeeper的/brokers/ids下注册监听协议,包括IP和端口号,客户端连接的时候,会取得这个IP和端口号。
后来查看了kafka的配置,原来我忽视了listeners和advertised.listeners的区别,advertised.listeners才是真正暴露给外部使用的连接地址,会写入到zookeeper节点中的。于是再次进行修改,把IP配置到advertised.listeners中,问题再一次解决。

advertised.listeners才是真正的对外代理地址!
那么listeners的作用就不是对外提供服务代理,而是监听!

5.解决问题

修改server.properties的两行默认配置,即可通过外网连接服务器Kafka,问题解决:

# 允许外部端口连接                                            
listeners=PLAINTEXT://0.0.0.0:9092  
# 外部代理地址                                                
advertised.listeners=PLAINTEXT://121.201.64.12:9092

你可能感兴趣的:(Kafka连接服务器出现:Connection to node 1 (localhost/127.0.0.1:9092) could not be established.)