Kafka之使用
windows下的管理工具:
kafka-tool: https://www.kafkatool.com/download.html
[Windows]
常用命令:
# 查看topic 列表 ./bin/kafka-topics.sh --bootstrap-server 127.0.0.1:9092 --list --bootstrap-server 指定kafka的服务地址 # 查看某个topic详情 ./bin/kafka-topics.sh --bootstrap-server 127.0.0.1:9092 --topic ORGANIZATION_CHANGE --describe # 创建topic kafka-topics.sh --create --zookeeper node1:2181 --replication-factor 1 --partitions 1 --topic my-kafka-topic # 消费者 ./bin/kafka-console-consumer.sh --bootstrap-server 127.0.0.1:9092 --topic test --from-beginning 参数说明: --from-beginning: 从开始进行数据消费
属性列表:
参数 | 值类型 | 说明 | 有效值 | |
--bootstrap-server | string | 要连接的服务器 | host1:port1,host2:port2 | |
--topic | string | 接收消息的主题名称 | ||
--batch-size | int | 单个批处理中发送的消息数 | 默认:200 | |
--compression-codec | string | 压缩编码器 | none、gzip(默认值) snappy、lz4、zstd |
|
--max-block-ms | Long | 在发送请求期间,生产者将阻止的最长时间 | 60000(默认值) | |
--max-memory-bytes | Long | 生产者用来缓冲等待发送到服务器的总内存 | 33554432(默认值) | |
--max-partition-memory-bytes | Long | 为分区分配的缓冲区大小 | 16384 | |
--message-send-max-retries | Integer | 最大的重试发送次数 | 3 | |
--metadata-expiry-ms | Long | 强制更新元数据的时间阈值(ms) | 300000 | |
--producer-property | String |
|
形如:key=value | |
--producer.config | String | 生产者配置属性文件 [--producer-property]优先于此配置 |
配置文件完整路径 | |
--property | String | 自定义消息读取器 | parse.key=true|false key.separator=<key.separator> ignore.error=true|false |
|
--request-required-acks | String | 生产者请求的确认方式 | 0、1(默认值)、all | |
--request-timeout-ms | Integer | 生产者请求的确认超时时间 | 1500(默认值) | |
--retry-backoff-ms | Integer | 生产者重试前,刷新元数据的等待时间阈值 | 100(默认值) | |
--socket-buffer-size | Integer | TCP接收缓冲大小 | 102400(默认值) | |
--timeout | Integer | 消息排队异步等待处理的时间阈值 | 1000(默认值) | |
--sync | 同步发送消息 | |||
--version | 显示 Kafka 版本 不配合其他参数时,显示为本地Kafka版本 |
常见问题:
1.使用代码进行连接的时候,发生报错:producer closed kafka: client has run out of available brokers to talk to: dial tcp 127.0.0.1:9092: connectex: No connection could be made because the target machine actively refused it.
解决:
需要再kafka的配置服务配置文件中开启注释 # The address the socket server listens on. If not configured, the host name will be equal to the value of # java.net.InetAddress.getCanonicalHostName(), with PLAINTEXT listener name, and port 9092. # FORMAT: # listeners = listener_name://host_name:port # EXAMPLE: # listeners = PLAINTEXT://your.host.name:9092 listeners=PLAINTEXT://0.0.0.0:9092 # Listener name, hostname and port the broker will advertise to clients. # If not set, it uses the value for "listeners". advertised.listeners=PLAINTEXT://172.29.97.140:9092 这里的IP需要暴露给客户端使用的, 我这里是设置的本机的局域网IP