kafka启动命令_Kafka入门

3bef9970c9663f3bb12e0963937fae4a.png

Kafka是一个高性能分布式消息系统,今天就简单介绍下基础入门吧。

一、下载安装

可以访问https://www.apache.org/dyn/closer.cgi?path=/kafka/2.2.0/kafka_2.12-2.2.0.tgz下载安装文件。

> tar -xzf kafka_2.12-2.2.0.tgz > cd kafka_2.12-2.2.0

二、启动服务器

Kafka使用zookeeper,所以我们要先启动zookeeper,可以使用安装文件里面的启动命令启动一个单节点的zookeeper。

> bin/zookeeper-server-start.sh config/zookeeper.properties
[2013-04-22 15:01:37,495] INFO Reading configuration from: config/zookeeper.properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
...

然后就可以启动Kafka服务器了

> bin/kafka-server-start.sh config/server.properties
[2013-04-22 15:01:47,028] INFO Verifying properties (kafka.utils.VerifiableProperties)
[2013-04-22 15:01:47,051] INFO Property socket.send.buffer.bytes is overridden to 1048576 (kafka.utils.VerifiableProperties)
...

三、创建一个主题(Topic)

我们使用命令创建一个名叫"test"的主题。

> bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test

我们可以使用命令行查询主题

> bin/kafka-topics.sh --list --bootstrap-server localhost:9092
test

四、发送消息

我们接下来使用命令行里的发送命令来往"test"主题里面发送消息。

> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
This is a message
This is another message

五、接收消息

同样我们使用命令行来接收"test"主题里的消息。

> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
This is a message
This is another message

可以看到,我们接收到了刚才我们发出的消息。

六、多节点Kafka

上面我们只创建了一个kafka实例,我们将它扩展成3个节点。

首先我们复制两份原有节点的配置文件:

> cp config/server.properties config/server-1.properties
> cp config/server.properties config/server-2.properties

编辑如下内容:

config/server-1.properties:
    broker.id=1
    listeners=PLAINTEXT://:9093
    log.dirs=/tmp/kafka-logs-1
 
config/server-2.properties:
    broker.id=2
    listeners=PLAINTEXT://:9094
    log.dirs=/tmp/kafka-logs-2

注意,broker.id必须是唯一的,并且我们把端口和日志文件路径都修改了以便我们在单机上启动多节点。

然后我们试着启动这两个节点:

> bin/kafka-server-start.sh config/server-1.properties &
> bin/kafka-server-start.sh config/server-2.properties &

接着我们创建一个三个副本的主题:

> bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 3 --partitions 1 --topic my-replicated-topic

我们可以通过命令查询这个主题的信息:

> bin/kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic my-replicated-topic
Topic:my-replicated-topic   PartitionCount:1    ReplicationFactor:3 Configs:
Topic: my-replicated-topic  Partition: 0    Leader: 1   Replicas: 1,2,0 Isr: 1,2,0

接着我们往这个topic发送消息:

> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-replicated-topic
my test message 1
my test message 2

我们来运行消费者来消费这些消息:

> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic my-replicated-topic
my test message 1
my test message 2

接下来我们来看看高可用,上面我们看到Leader节点是1,所以我们停止掉1,再运行查看命令

> bin/kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic my-replicated-topic
Topic:my-replicated-topic   PartitionCount:1    ReplicationFactor:3 Configs:
Topic: my-replicated-topic  Partition: 0    Leader: 2   Replicas: 1,2,0 Isr: 2,0

我们继续运行消费者:

> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic my-replicated-topicmy test message 1 my test message 2 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值