Centos7安装kafka集群

kafka单机集群3个节点,端口分别是9092、9093、9094

安装kafka前先安装zookeeper


1、文件安装到/usr/local/kafka下(自己下载tar包或wget安装)

[root@localhost ~]# mkdir /usr/local/kafkasoft
[root@localhost ~]# cd /usr/local/kafkasoft/
[root@localhost kafkasoft]# ll
总用量 62500
-rw-r--r--. 1 root root 63999924 10月 11 21:25 kafka_2.11-2.2.0.tgz

解压

[root@localhost kafkasoft]# tar -zxvf kafka_2.11-2.2.0.tgz

创建kafka目录及节点目录

[root@localhost kafkasoft]# mkdir -p /usr/local/kafka/909{2,3,4}/logs
[root@localhost kafkasoft]# cp -r kafka_2.11-2.2.0 /usr/local/kafka/9092
[root@localhost kafkasoft]# cp -r kafka_2.11-2.2.0 /usr/local/kafka/9093
[root@localhost kafkasoft]# cp -r kafka_2.11-2.2.0 /usr/local/kafka/9094
[root@localhost kafkasoft]# cd ../kafka
[root@localhost kafka]# ll
总用量 0
drwxr-xr-x. 4 root root 42 10月 11 21:34 9092
drwxr-xr-x. 4 root root 42 10月 11 21:34 9093
drwxr-xr-x. 4 root root 42 10月 11 21:34 9094
[root@localhost kafka]# tree -L 2
.
├── 9092
│   ├── kafka_2.11-2.2.0
│   └── logs
├── 9093
│   ├── kafka_2.11-2.2.0
│   └── logs
└── 9094
    ├── kafka_2.11-2.2.0
    └── logs

2、配置server.properties

修改每个节点下kafka_2.12-2.2.0/config/server.properties配置文件

节点1

[root@localhost kafka]# vim 9092/kafka_2.11-2.2.0/config/server.properties 

broker.id=1
listeners=PLAINTEXT://127.0.0.1:9092
advertised.listeners=PLAINTEXT://127.0.0.1:9092
log.dirs=/usr/local/kafka/9092/logs
zookeeper.connect=127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183

节点2

[root@localhost kafka]# vim 9093/kafka_2.11-2.2.0/config/server.properties 

broker.id=2
listeners=PLAINTEXT://127.0.0.1:9093
advertised.listeners=PLAINTEXT://127.0.0.1:9093
log.dirs=/usr/local/kafka/9093/logs
zookeeper.connect=127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183

节点3

[root@localhost kafka]# vim 9094/kafka_2.11-2.2.0/config/server.properties 

broker.id=3
listeners=PLAINTEXT://127.0.0.1:9094
advertised.listeners=PLAINTEXT://127.0.0.1:9094
log.dirs=/usr/local/kafka/9094/logs
zookeeper.connect=127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183


3、配置consumer.properties

修改每个节点下的配置文件consumer.properties设置
bootstrap.servers=127.0.0.1:9092,127.0.0.1:9093,127.0.0.1:9094
group.id与java项目中的保持一致(无java代码可忽略)

[root@localhost kafka]# vim 9092/kafka_2.11-2.2.0/config/consumer.properties 
[root@localhost kafka]# vim 9093/kafka_2.11-2.2.0/config/consumer.properties 
[root@localhost kafka]# vim 9094/kafka_2.11-2.2.0/config/consumer.properties

4、启动kafka集群

[root@localhost kafka]# cd /usr/local/kafka/9092/kafka_2.11-2.2.0/bin/
[root@localhost bin]# ./kafka-server-start.sh -daemon ../config/server.properties
[root@localhost bin]# cd /usr/local/kafka/9093/kafka_2.11-2.2.0/bin/
[root@localhost bin]# ./kafka-server-start.sh -daemon ../config/server.properties
[root@localhost bin]# cd /usr/local/kafka/9094/kafka_2.11-2.2.0/bin/
[root@localhost bin]# ./kafka-server-start.sh -daemon ../config/server.properties
[root@localhost bin]# jps | grep Kafka
5681 Kafka
5330 Kafka
6031 Kafka

5、创建主题

[root@localhost bin]# ./kafka-topics.sh --create --zookeeper 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 --replication-factor 3 --partitions 3 --topic MQ_IPS_BAS_STR
WARNING: Due to limitations in metric names, topics with a period ('.') or underscore ('_') could collide. To avoid issues it is best to use either, but not both.
Created topic MQ_IPS_BAS_STR.

6、观察生产者和消费者

生产者

[root@localhost bin]# ./kafka-console-producer.sh --broker-list > 127.0.0.1:9092,127.0.0.1:9093,127.0.0.1:9094 --topic MQ_IPS_BAS_STR
>1
>

消费者

[root@localhost bin]# ./kafka-console-consumer.sh --bootstrap-server 127.0.0.1:9092 --topic MQ_IPS_BAS_STR test --from-beginning
1

7、防火墙添加端口

firewall-cmd --zone=public --add-port=9092/tcp --permanent
firewall-cmd --zone=public --add-port=9093/tcp --permanent
firewall-cmd --zone=public --add-port=9094/tcp --permanent
firewall-cmd --reload

你可能感兴趣的:(Centos7安装kafka集群)