前提: 已经装好 jdk 和 zookeeper
从官网下载: https://kafka.apache.org/downloads
cd /usr/local
mkdir kafka
curl -o kafka_2.12-3.3.1.tgz https://downloads.apache.org/kafka/3.3.1/kafka_2.12-3.3.1.tgz
tar -zxvf kafka_2.12-3.3.1.tgz
cd kafka_2.12-3.3.1
vim config/server.properties
host.name=yourhost
listeners=PLAINTEXT://your host:9092
advertised.listeners=PLAINTEXT://your host:9092
zookeeper.connect=localhost:2181
// bin/zookeeper-server-start.sh -daemon config/zookeeper.properties
bin/kafka-server-start.sh config/server.properties
查看日志
tail -f -n100 logs/server.log
tail -f -n100 logs/kafkaServer.out
查看端口是否启动
netstat -an|grep 9092
创建一个topic
bin/kafka-topics.sh --bootstrap-server yourhost:9092 --create --topic test
查看刚创建的topic
bin/kafka-topics.sh --bootstrap-server yourhost:9092 --list
监听
bin/kafka-console-consumer.sh --bootstrap-server yourhost:9092 --topic test --from-beginning
开启另一个窗口, 生产一些消息
bin/kafka-console-producer.sh --broker-list yourhost:9092 --topic test
> hello world
> hello kafka
由于自己买的服务器内存太小,不得不降低了启动初始内存, 默认为1G, 如果启动失败,可以关注此设置
bin/kafka-server-start.sh
伪集群
复制 config/server.properties 为 三份
cp config/server.properties server_01.properties
cp config/server.properties server_02.properties
cp config/server.properties server_03.properties
修改: server_01.properties
broker.id=0
log.dirs=/tmp/kafka01/kafka-logs
listeners=PLAINTEXT://:9092
advertised.listeners=PLAINTEXT://124.221.94.153:9092
修改: server_02.properties
broker.id=2
log.dirs=/tmp/kafka01/kafka-logs
listeners=PLAINTEXT://:9093
advertised.listeners=PLAINTEXT://124.221.94.153:9093
修改: server_03.properties
broker.id=3
log.dirs=/tmp/kafka01/kafka-logs
listeners=PLAINTEXT://:9094
advertised.listeners=PLAINTEXT://124.221.94.153:9094
启动:
/kafka-server-start.sh config/server_01.properties
/kafka-server-start.sh config/server_02.properties
/kafka-server-start.sh config/server_03.properties
查看端口是否已经在监听
netstat -an|grep 9092
netstat -an|grep 9093
netstat -an|grep 9094
创建topic
/kafka-topics.sh --bootstrap-server localhost:9092 localhost:9093 localhost:9094 --create --replication-factor 3 --partitions 3 --topic test
查看topic
./kafka-topics.sh --bootstrap-server localhost:9092 localhost:9093 localhost:9094 --list
查看topic详情
./kafka-topics.sh --bootstrap-server localhost:9092 localhost:9093 localhost:9094 --describe --topic test
生产
./kafka-console-producer.sh --broker-list localhost:9092 localhost:9093 localhost:9094 --topic test
消费
./kafka-console-consumer.sh --bootstrap-server localhost:9092 localhost:9093 localhost:9094 --topic test --from-beginning