1. 安装java

参考: Centos7 安装 Java 和 Maven

2. 安装kafka

Kafka Binary downloads

kafka.jpg

wget http://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.4.0/kafka_2.12-2.4.0.tgz
tar -zxf kafka_2.12-2.4.0.tgz 
mv kafka_2.12-2.4.0 /usr/local/kafka
cd /usr/local/kafka/
mkdir -p /data/kafka/logs-1
vim /usr/local/kafka/config/server.properties
…………
broker.id=1
log.dir=/data/kafka/logs-1
…………

3. 启动zookeeper

bin/zookeeper-server-start.sh -daemon config/zookeeper.properties

4. 启动kafka

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

5. 创建Topic

[root@ogenes02 kafka]# bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic ogenes02
Created topic ogenes02.
[root@ogenes02 kafka]# bin/kafka-topics.sh --list --zookeeper localhost:2181
__consumer_offsets
ogenes
ogenes02
test
[root@ogenes02 kafka]# 

6. 生产消息

[root@ogenes02 kafka]# bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
>hello kafka
>

7. 消费消息

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

8. 远程访问

vim /usr/local/kafka/config/server.properties
…………
listeners=PLAINTEXT://192.168.56.101:9092
advertised.listeners=PLAINTEXT://192.168.56.101:9092
…………

生产消息
bin/kafka-console-producer.sh --broker-list 192.168.56.101:9092 --topic test

消费消息
bin/kafka-console-consumer.sh --bootstrap-server 192.168.56.101:9092 --topic test --from-beginning

9. 单机多broker集群配置

[root@ogenes02 kafka]# cp config/server.properties config/server-2.properties
[root@ogenes02 kafka]# cp config/server.properties config/server-3.properties
[root@ogenes02 kafka]# mkdir /data/kafka/logs-2
[root@ogenes02 kafka]# mkdir /data/kafka/logs-3
[root@ogenes02 kafka]# vim config/server-2.properties 

…………

# The id of the broker. This must be set to a unique integer for each broker.
broker.id=2

############################# Socket Server Settings #############################

# The address the socket server listens on. It will get the value returned from 
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
#listeners=PLAINTEXT://:9092
listeners=PLAINTEXT://192.168.56.101:9093

# Hostname and port the broker will advertise to producers and consumers. If not set, 
# it uses the value for "listeners" if configured.  Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
#advertised.listeners=PLAINTEXT://your.host.name:9092
advertised.listeners=PLAINTEXT://192.168.56.101:9093
…………
log.dirs=/data/kafka/logs-2
…………


[root@ogenes02 kafka]# vim config/server-3.properties 

…………

# The id of the broker. This must be set to a unique integer for each broker.
broker.id=3

############################# Socket Server Settings #############################

# The address the socket server listens on. It will get the value returned from 
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
#listeners=PLAINTEXT://:9092
listeners=PLAINTEXT://192.168.56.101:9094

# Hostname and port the broker will advertise to producers and consumers. If not set, 
# it uses the value for "listeners" if configured.  Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
#advertised.listeners=PLAINTEXT://your.host.name:9092
advertised.listeners=PLAINTEXT://192.168.56.101:9094
…………
log.dirs=/data/kafka/logs-3
…………

[root@ogenes02 kafka]# bin/kafka-server-start.sh config/server.properties &
[root@ogenes02 kafka]# bin/kafka-server-start.sh config/server-2.properties &
[root@ogenes02 kafka]# bin/kafka-server-start.sh config/server-3.properties &
[root@ogenes02 kafka]#  bin/kafka-console-producer.sh --broker-list 192.168.56.101:9093 --topic ogenes001
>窝窝头
>^C^[[A[root@ogenes02 kafka]#  bin/kafka-console-producer.sh --broker-list 192.168.56.101:9092 --topic ogenes001 
>一块钱四个
>^C[root@ogenes02 kafka]#  bin/kafka-console-producer.sh --broker-list 192.168.56.101:9094 --topic ogenes001 
>嘿嘿
>^C[root@ogenes02 kafka]# 


[root@ogenes02 kafka]#  bin/kafka-console-consumer.sh --bootstrap-server 192.168.56.101:9093 192.168.56.101:9093 192.168.56.101:9094 --topic ogenes001 --from-beginning
窝窝头
一块钱四个
嘿嘿

10. 多机多broker集群配置

标签: none

添加新评论