一、概述
# 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
# 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
listeners就是主要用来定义Kafka Broker的Listener的配置项。
advertised.listeners参数的作用就是将Broker的Listener信息发布到Zookeeper中
二、试验
1、默认都不配置
zookeeper查看kafka地址
get /brokers/ids/0
"endpoint":["PLAINTEXT://k8s01:9002"]
kafka创建topic
bin/kafka-topics.sh --create --bootstrap-server localhost:9002 --replication-factor 1 --partitions 1 --topic test
bin/kafka-topics.sh --list --bootstrap-server localhost:9002
如果使用
bin/kafka-topics.sh --list --bootstrap-server 192.168.10.131:9002
需要提前添加主机hosts
192.168.10.131 k8s01
2、两者都设置为同一IP和端口
配置
listeners=PLAINTEXT://192.168.10.131:9092
advertised.listeners=PLAINTEXT://192.168.10.131:9092
zookeeper查看kafka地址
get /brokers/ids/0
"endpoint":["PLAINTEXT://192.168.10.131:9092"]
bin/kafka-topics.sh --list --bootstrap-server 192.168.10.131:9092
3、advertised改为随机主机名
配置
listeners=PLAINTEXT://192.168.10.131:9092
advertised.listeners=PLAINTEXT://test-kafka:9092
zookeeper查看kafka地址
get /brokers/ids/0
"endpoint":["PLAINTEXT://test-kafka:9092"]
4、listeners为127.0.0.1
配置
listeners=PLAINTEXT://127.0.0.1:9092
advertised.listeners=PLAINTEXT://192.168.10.131:9092
zookeeper查看kafka地址
get /brokers/ids/0
"endpoint":["PLAINTEXT://192.168.10.131:9092"]
5、内外网分流
listener.security.protocol.map=EXTERNAL:PLAINTEXT,INTERNAL:PLAINTEXT
listeners=EXTERNAL://192.168.10.131:9092,INTERNAL://192.168.10.131:9093
inter.broker.listener.name=INTERNAL
zookeeper查看kafka地址
get /brokers/ids/0
"endpoints":["EXTERNAL://192.168.10.131:9092","INTERNAL://192.168.10.131:9093"]
内网访问可以使用:192.168.10.131:9093
外网访问使用:192.168.10.131:9092
这在kafka部署到kubernetes时候就很有用了
6、总结
listeners就是主要用来定义Kafka Broker的Listener的配置项。标签:PLAINTEXT,10.131,192.168,kafka,--,listeners,advertised From: https://blog.51cto.com/u_13236892/5787379
advertised.listeners参数的作用就是将Broker的Listener信息发布到Zookeeper中
inter.broker.listener.name:专门用于Kafka集群中Broker之间的通信
listener.security.protocol.map
配置监听者的安全协议的,比如PLAINTEXT、SSL、SASL_PLAINTEXT、SASL_SSL
listeners
是kafka真正bind的地址
advertised.listeners
是暴露给外部的listeners,如果没有设置,会用listeners