Kafka 远程消费者读不到数据

问题描述

    服务器上使用脚本测试 producer & consumer 可生产 & 消费信息,但在使用 Java 代码远程作为消费者时,代码却卡在 comsumer.poll(long timeout) 不往下进行。

解决方式

    编辑 Kafka 目录下 config/server.properties,主要是添加advertised.listeners项:

# The address the socket server listens on. It will get the value returned from 
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
# 如下,经测均可使用
listeners=PLAINTEXT://:9092
# listeners = PLAINTEXT://0.0.0.0:9092
# listeners=PLAINTEXT://server1:9092
# listeners=PLAINTEXT://192.168.78.101: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
# 如下,经测hostname 和 ip 均可以
# advertised.listeners = PLAINTEXT://192.168.78.101:9092
advertised.listeners = PLAINTEXT://server1:9092

官方定义

 listeners

    Listener List - Comma-separated list of URIs we will listen on and the listener names. If the listener name is not a security protocol, listener.security.protocol.map must also be set. 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,SSL://:9091 CLIENT://0.0.0.0:9092,REPLICATION://localhost:9093

 advertised.listeners

    Listeners to publish to ZooKeeper for clients to use, if different than the listeners config property. 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. Unlike listeners it is not valid to advertise the 0.0.0.0 meta-address.

待填坑,需结合源码理解

其他

    1. 除 advertised.listeners 外,也可一同配置 advertised.host.nameadvertised.port,不过这两个配置项已废弃,仅在 advertised.listenerslisteners 均未设置时使用。
    2. 当未配置时,默认使用java.net.InetAddress.getCanonicalHostName(),写了个小文件在服务器上直接执行得到server1,即配置过的 hostname,这样的话远程是可以连通的,但却出现了此问题。

待填坑,需结合源码理解

参考

  1. Kafka Broker Configs
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值