kafka2.11-0.9.0.0实现Producer发送消息时,无法发送,报错信息:org.apache.kafka.common.errors.TimeoutException: Batch Expired

最开始在本机搭建了kafka伪集群,本地 producer 客户端成功发布消息至 broker。随后在服务器(虚拟机)上搭建了 kafka 集群,在本机连接该集群,producer 却无法发布消息到 broker(奇怪也没有抛错)。最开始怀疑是 iptables 没开放,于是开放端口,结果还不行(又开始怀疑是代码问题、版本问题等等,倒腾了很久)。后来打印了返回信息,发现报了异常org.apache.kafka.common.errors.TimeoutException: Batch Expired

参考了网上的一些解决方案,最终锁定修改如下配置信息。

# The address the socket server listens on. It will get the value returned from 
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = security_protocol://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
listeners=PLAINTEXT://:9092
 # 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

以上说的就是 advertised.listeners 是 broker 给 producer 和 consumer 连接使用的,如果没有设置,就使用 listeners,而如果 host_name 没有设置的话,就使用 java.net.InetAddress.getCanonicalHostName() 方法返回的主机名。

修改方法:

修改server.properties 如下配置

修改前:

listeners=PLAINTEXT://:9092

修改后

listeners=PLAINTEXT://192.168.x.100:9092
advertised.listeners=PLAINTEXT://10.10.xx.252:9092

其中192.168.x.100是虚拟机内网地址

10.10.xx.252是安装虚拟机的服务器映射的外网地址

如果是虚拟机还需要配置虚拟机与主机的端口映射以及主机的入栈、出栈协议。

修改后重启kafka服务。

猜你喜欢

转载自www.cnblogs.com/copykakaxi/p/12524293.html