linux下安装kafka-3.0.0
由于kafka3.0.0出来了,尝试使用下其新特性,此次记录单机安装并使用其自带zookeeper启动。
下载并解压
执行下列命令下载
wget https://dlcdn.apache.org/kafka/3.0.0/kafka_2.12-3.0.0.tgz --no-check-certificate
方式一:通过kafka自带zk启动
修改配置文件
修改config目录下server.properties,关注以下三点
broker.id=0
listeners=PLAINTEXT://localhost:9092
zookeeper.connect=localhost:2181
log.retention.hours=3 过期时间
log.dirs=/tmp/kafka-logs 存储目录
启动自带zk和kafka
在bin目录下执行自带zk启动命令
./zookeeper-server-start.sh -daemon ../config/zookeeper.properties &
在bin目录下执行kafka启动命令
./kafka-server-start.sh -daemon ../config/server.properties &
此时执行ps -ef|grep kafka
可以看到两个进程已经存在
简单测试
在bin目录下执行创建名为LOGTAIL-FLUSHER-KAFKA的topic的命令
./kafka-topics.sh --bootstrap-server localhost:9092 --create --replication-factor 1 --partitions 8 --topic LOGTAIL-FLUSHER-KAFKA
再执行./kafka-topics.sh --bootstrap-server localhost:9092 --list
查看是否创建成功
方式二:通过kafka3.0中kraft启动
Apache Kafka 的内置共识机制将取代 Apache ZooKeepe。虽然 KRaft 尚未被推荐用于生产(已知差距列表),但对 KRaft 元数据和 API 进行了许多改进。Exactly-once 和分区重新分配支持值得强调。鼓励大家查看 KRaft 的新功能并在开发环境中试用它。
在kafka3.0源码下的介绍文档有如何使用,大家也可以看看。
Generate a cluster ID 生成集群id
The first step is to generate an ID for your new cluster, using the kafka-storage tool:
第一步通过kafka-storage tool生产集群id:
$ ./bin/kafka-storage.sh random-uuid
0ibxKrJFT7O19OyiOc-9Tw
Format Storage Directories格式化存储目录
The next step is to format your storage directories. If you are running in single-node mode, you can do this with one command:
下一步是格式化存储目录。 如果您在单节点模式下运行,您可以使用以下命令来执行此操作:
$ ./bin/kafka-storage.sh format -t <uuid> -c ./config/kraft/server.properties
Formatting /tmp/kraft-combined-logs
如果您使用多个节点,那么您应该在每个节点上运行 format 命令。 确保为每个集群使用相同的集群 ID。
If you are using multiple nodes, then you should run the format command on each node. Be sure to use the same cluster ID for each one.
Start the Kafka Server
Finally, you are ready to start the Kafka server on each node.
最后,您已准备好在每个节点上启动 Kafka 服务器。
$ ./bin/kafka-server-start.sh ./config/kraft/server.properties
[2021-02-26 15:37:11,071] INFO Registered kafka:type=kafka.Log4jController MBean (kafka.utils.Log4jControllerRegistration$)
[2021-02-26 15:37:11,294] INFO Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation (org.apache.zookeeper.common.X509Util)
[2021-02-26 15:37:11,466] INFO [Log partition=__cluster_metadata-0, dir=/tmp/kraft-combined-logs] Loading producer state till offset 0 with message format version 2 (kafka.log.Log)
[2021-02-26 15:37:11,509] INFO [raft-expiration-reaper]: Starting (kafka.raft.TimingWheelExpirationService$ExpiredOperationReaper)
[2021-02-26 15:37:11,640] INFO [RaftManager nodeId=1] Completed transition to Unattached(epoch=0, voters=[1], electionTimeoutMs=9037) (org.apache.kafka.raft.QuorumState)
...
Just like with a ZooKeeper based broker, you can connect to port 9092 (or whatever port you configured) to perform administrative operations or produce or consume data.
就像使用基于 ZooKeeper 的代理一样,您可以连接到端口 9092(或您配置的任何端口)来执行管理操作或生产或使用数据。
$ ./bin/kafka-topics.sh --create --topic foo --partitions 1 --replication-factor 1 --bootstrap-server localhost:9092
Created topic foo.
直接按照上面操作即可启动成功,十分方便,大家最好按照下面守护进程方式启动。
./bin/kafka-server-start.sh -daemon ./config/kraft/server.properties &
查看集群 ID配置文件方式:
cat /tmp/kraft-combined-logs/meta.properties
之后就是同样的方法使用了