问题描述
服务器上使用脚本测试 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.name
和 advertised.port
,不过这两个配置项已废弃,仅在 advertised.listeners
和 listeners
均未设置时使用。
2. 当未配置时,默认使用java.net.InetAddress.getCanonicalHostName()
,写了个小文件在服务器上直接执行得到server1
,即配置过的 hostname
,这样的话远程是可以连通的,但却出现了此问题。
待填坑,需结合源码理解