在 Ubuntu 上配置 Kafka 生产者,通常分三步:安装 Kafka → 启动服务 → 配置并使用生产者。下面以 Kafka 3.x(KRaft 或 Zookeeper 模式均可) 为例说明。
sudo apt update
sudo apt install openjdk-17-jdk -y
java -version
wget https://downloads.apache.org/kafka/3.7.0/kafka_2.13-3.7.0.tgz
tar -xzf kafka_2.13-3.7.0.tgz
cd kafka_2.13-3.7.0
KAFKA_CLUSTER_ID=$(bin/kafka-storage.sh random-uuid)
bin/kafka-storage.sh format -t $KAFKA_CLUSTER_ID -c config/kraft/server.properties
bin/kafka-server-start.sh config/kraft/server.properties
如果用 Zookeeper:
bin/zookeeper-server-start.sh config/zookeeper.properties
bin/kafka-server-start.sh config/server.properties
bin/kafka-topics.sh \
--create \
--topic test-topic \
--bootstrap-server localhost:9092 \
--partitions 1 \
--replication-factor 1
查看 Topic:
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
bin/kafka-console-producer.sh \
--topic test-topic \
--bootstrap-server localhost:9092
然后直接输入消息:
hello kafka
this is a test
Kafka 自带示例配置:
config/producer.properties
示例配置内容:
bootstrap.servers=localhost:9092
key.serializer=org.apache.kafka.common.serialization.StringSerializer
value.serializer=org.apache.kafka.common.serialization.StringSerializer
acks=all
retries=3
linger.ms=10
batch.size=16384
使用配置文件启动:
bin/kafka-console-producer.sh \
--topic test-topic \
--bootstrap-server localhost:9092 \
--producer.config config/producer.properties
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>3.7.0</version>
</dependency>
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
props.put("acks", "all");
props.put("retries", 3);
KafkaProducer<String, String> producer =
new KafkaProducer<>(props);
ProducerRecord<String, String> record =
new ProducerRecord<>("test-topic", "key1", "hello kafka");
producer.send(record);
producer.close();
| 配置项 | 说明 |
|---|---|
| bootstrap.servers | Kafka 地址 |
| acks | 0 / 1 / all(可靠性) |
| retries | 发送失败重试次数 |
| linger.ms | 批量等待时间 |
| batch.size | 批量大小 |
| compression.type | 压缩(gzip/snappy/lz4) |
消费者查看消息:
bin/kafka-console-consumer.sh \
--topic test-topic \
--bootstrap-server localhost:9092 \
--from-beginning
如果你需要:
可以直接告诉我你的使用场景。