深入kafka(二).单机与伪分布式配置实战

1.下载安装

  • 到https://www.apache.org上下载
    wget http://apache.fayea.com/kafka/0.10.0.0/kafka_2.11-0.10.0.0.tgz
  • 安装:
    拷贝到用户目录 cp kafka_2.11-0.10.0.0.tgz /usr/local
    解压:tar -zxvf kafka_2.11-0.10.0.0.tgz
    重命名: mv kafka_2.11-0.10.0.0 kafka

2.单机版配置

  • 使用kafka自带的zookeeper:默认config中配置的zookeeper地址为zookeeper.connect=localhost:2181
    bin/zookeeper-server-start.sh config/zookeeper.properties &
    bin/kafka-server-start.sh config/server.properties &
  • 不适用自带的zookeeper,则需要修改config.properties中的zookeeper.connect为zookeeper地址
    bin/kafka-server-start.sh config/server.properties &
  • &表示后台启动进程
  • 停止kafka集群:bin/kafka-server-stop.sh

3.命令行测试:

3.1创建topic

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

3.2列出所有topic

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

3.3启动生产者

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

输入一些信息,比如hello,并且Ctrl+z退出shell

3.4启动消费者comsumer

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

稍等片刻就能看到你生产的消息 hello

4.伪分布式搭建:

4.1配置

每个broker代表一个kafka server端,每个broker对应一个server.properties的配置文件,我们搭建运行3个broker的伪分布式环境。
配置文件分别为config/server.properties、config/server1.properties、config/server2.properties。

config/server.properties:

broker.id=0
hostname = localhost
port = 9092
log.dirs=/usr/local/kafka/logs/kafka-log-0
zookeeper.connect=localhost:2181
zookeeper.connection.timeout.ms=6000   

config/server-1.properties:

broker.id=1
hostname = localhost
port = 9093
log.dirs=/usr/local/kafka/logs/kafka-log-1
zookeeper.connect=localhost:2181
zookeeper.connection.timeout.ms=6000   

config/server-2.properties:

broker.id=2
hostname = localhost
port = 9094
log.dirs=/usr/local/kafka/logs/kafka-log-2
zookeeper.connect=localhost:2181
zookeeper.connection.timeout.ms=6000   

4.2启动kafka集群:

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

lsof -i tcp:9092 lsof -i tcp:9093 lsof -i tcp:9094分别观察端口情况

4.3 测试集群

  • 创建一个拥有3个副本的集群
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic
  • 查看每个节点信息
bin/kafka-topics.sh --describe  --zookeeper localhost:2181 --topic my-replicated-topic
  • 生产信息:
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-replicated-topic
  • 消费消息:
bin/kafka-console-consumer.sh --zookeeper localhost:2181  --topic my-replicated-topic --from-beginning
  • 停止kafka集群:bin/kafka-server-stop.sh

欢迎大家扫码关注我的微信公众号,与大家一起分享技术与成长中的故事。


我的微信公众号.jpg

你可能感兴趣的:(深入kafka(二).单机与伪分布式配置实战)