可以看到報錯第一句顯示:Connection with localhost/127.0.0.1 disconnected
但是我明明在application.yml中配置了我的Kafka Server的地址是:192.168.52.131:9092,而在實際連接kafka服務器時卻使用的localhost/127.0.0.1這個地址,所以導致無法連接kafka Server。
經過百度,得知,在設置Kafka的時候,需要設置advertised.listeners這個屬性。
該屬性在config/server.properties中的描述如下:
# Hostname and port the broker will advertise to producers and consumers. If not set,
# it uses the value for "listeners" if configured. Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
# advertised.listeners=PLAINTEXT://:your.host.name:9092
翻譯過來就是hostname和端口是用來建議給生產者和消費者使用的,如果沒有設置,將會使用listeners的配置,如果listeners也沒有配置,將使用java.net.InetAddress.getCanonicalHostName()來獲取這個hostname和port,對於ipv4,基本就是localhost了。
"PLAINTEXT"表示協議,可選的值有PLAINTEXT和SSL,hostname可以指定IP地址,也可以用"0.0.0.0"表示對所有的網絡接口有效,如果hostname爲空表示只對默認的網絡接口有效
也就是說如果你沒有配置advertised.listeners,就使用listeners的配置通告給消息的生產者和消費者,這個過程是在生產者和消費者獲取源數據(metadata)。
因此重新設置advertised.listeners爲如下:
advertised.listeners=PLAINTEXT://192.168.52.131:9092