Kafka学习之一 :安装启动

参考和转载:
Kafka
kafka安装和启动
Linux环境信息
kafka-topics.sh –describe显示结果解释
Java环境:
[hao973@bogon ~]$ java -version
java version “1.8.0_121”
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

Linux环境信息:
cat /proc/version
Linux version 2.6.32-431.el6.x86_64 ([email protected]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) ) #1 SMP Sun Nov 10 22:19:54 EST 2013
cat /etc/issue
Red Hat Enterprise Linux Server release 6.5 (Santiago)

假设现在没有Kafka和ZooKeeper环境。

Step 1: 下载代码
下载0.10.0.0版本并且解压它。Kafka下载地址

tar -xzf kafka_2.11-0.10.0.0.tgz 
cd kafka_2.11-0.10.0.0

Step 2: 启动服务

运行kafka需要使用Zookeeper,所以你需要先启动Zookeeper,如果你没有Zookeeper,你可以使用kafka自带打包和配置好的Zookeeper。

./bin/zookeeper-server-start.sh  ./config/zookeeper.properties 
[2017-12-13 22:49:40,427] INFO Reading configuration from: ./config/zookeeper.properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
......

现在启动kafka服务

./bin/kafka-server-start.sh ./config/server.properties &

......
[2017-12-13 23:34:33,284] INFO [KafkaServer id=0] started (kafka.server.KafkaServer)

启动kafka服务时出现错误:
java.net.UnknownHostException: bogon: bogon: Name or service not known

[2017-12-13 23:22:42,479] FATAL [KafkaServer id=0] Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer)
java.net.UnknownHostException: bogon: bogon: Name or service not known
    at java.net.InetAddress.getLocalHost(InetAddress.java:1505)
    ......
Caused by: java.net.UnknownHostException: bogon: Name or service not known
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    .....

解决办法:
修改 /etc/hosts, 在后面添加 bogon
修改前:
[root@bogon ~]# vim /etc/hosts
1 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
2 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
修改后:
[root@bogon ~]# vim /etc/hosts
1 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 bogon
2 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 bogon

Step 3: 创建一个主题(topic)

创建一个名为“test”的Topic,只有一个分区和一个备份:

./bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

Created topic "test".

创建好之后,可以通过运行以下命令,查看已创建的topic信息:

./bin/kafka-topics.sh --list --zookeeper localhost:2181

test

或者,除了手工创建topic外,你也可以配置你的broker,当发布一个不存在的topic时自动创建topic。

Step 4: 发送消息

Kafka提供了一个命令行的工具,可以从输入文件或者命令行中读取消息并发送给Kafka集群。每一行是一条消息。
运行producer(生产者),然后在控制台输入几条消息到服务器。

./bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test 
>this is a message
>this is a other messgae

Step 5: 消费消息

Kafka也提供了一个消费消息的命令行工具,将存储的信息输出出来。

./bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning

Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].

this is a message
this is a other messgae

如果你有2台不同的终端上运行上述命令,那么当你在运行生产者时,消费者就能消费到生产者发送的消息。
所有的命令行工具有很多的选项,你可以查看文档来了解更多的功能。

Step 6: 设置多个broker集群

到目前,我们只是单一的运行一个broker,,没什么意思。对于Kafka,一个broker仅仅只是一个集群的大小, 所有让我们多设几个broker.
首先为每个broker创建一个配置文件:

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.dir=/tmp/kafka-logs-1

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

broker.id是集群中每个节点的唯一且永久的名称,我们修改端口和日志分区是因为我们现在在同一台机器上运行,我们要防止broker在同一端口上注册和覆盖对方的数据。

我们已经运行了zookeeper和刚才的一个kafka节点,所有我们只需要在启动2个新的kafka节点。

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


[1] 8363
...
[2017-12-14 00:37:35,935] INFO [KafkaServer id=1] started (kafka.server.KafkaServer)

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


[2] 8665
...
[2017-12-14 00:39:48,446] INFO [KafkaServer id=2] started (kafka.server.KafkaServer)

现在,我们创建一个新topic,把备份设置为:3

./bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic

Created topic "my-replicated-topic".

好了,现在我们已经有了一个集群了,我们怎么知道每个集群在做什么呢?运行命令“describe topics”

./bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic my-replicated-topic

Topic:my-replicated-topic   PartitionCount:1    ReplicationFactor:3 Configs:
    Topic: my-replicated-topic  Partition: 0    Leader: 2   Replicas: 2,1,0 Isr: 2,1,0

这是一个解释输出,第一行是所有分区的摘要,每一行提供一个分区信息,因为我们只有一个分区,所以只有一行。

    "leader":该节点负责所有指定分区的读和写,每个节点的领导都是随机选择的。
    "replicas":备份的节点,无论该节点是否是leader或者目前是否还活着,只是显示。
    "isr":备份节点的集合,也就是活着的节点集合。

我们运行这个命令,看看一开始我们创建的那个节点:

./bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test

Topic:test  PartitionCount:1    ReplicationFactor:1 Configs:
    Topic: test Partition: 0    Leader: 0   Replicas: 0 Isr: 0

没有惊喜,刚才创建的topic(主题)没有Replicas,所以是0。

让我们来发布一些信息在新的topic上:

./bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-replicated-topic

>this is a message
>this is a other message
>

现在,消费这些消息。

./bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic my-replicated-topic

Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].

this is a message
this is a other message

猜你喜欢

转载自blog.csdn.net/feng973/article/details/78803575