kafka伪分布下安装和使用

 2.1 伪分布式安装 生产消费数据案例

9092是kafka默认端口,在 kafka/conf/server.properties下可以看到

解压:
[root@h2single local]# tar -zxvf kafka_2.10-0.8.2.0.tgz 
重命名
[root@h2single local]# mv kafka_2.10-0.8.2.0/ kafka
使用自动zk 并启动zk:
启动ZK :
[root@h2single kafka]# bin/zookeeper-server-start.sh config/zookeeper.properties &  (&表示后台启动)
启动kafka服务
[root@h2single kafka]# bin/kafka-server-start.sh config/server.properties &

创建主题( 意味着有临时存放数据的地方了) 
[root@h2single kafka]# bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

命令解释如下:
--zookeeper localhost:2181 指定了zk 
--replication-factor 1 指定了副本数
--partitions 1 指定了分区数 
--topic test 主题名称


查看所有主题名称:
[root@h2single kafka]# bin/kafka-topics.sh --list --zookeeper localhost:2181
结果:
[2015-03-06 23:31:25,145] INFO Established session 0x14bf32190630002 with negotiated timeout 30000 for client /0:0:0:0:0:0:0:1:59839 (org.apache.zookeeper.server.ZooKeeperServer)
test ----> 显示刚创建的topic名称

查看指定主题名称的详细信息:
[root@h2single kafka]# bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test
Topic:test      PartitionCount:1        ReplicationFactor:1     Configs:
        Topic: test     Partition: 0  0表示标号  Leader: 0       Replicas: 0     Isr: 0


		
创建生产者    
[root@h2single kafka]# bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test  后输入如下 
hello me
you an me are ok

创建消费者 
[root@h2single kafka]# bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning   得到如下数据,表示生产的数据被消费到了
hello you
hhello me
you an me are ok

伪分布下制造多个broker,并指定副本个数下创建主题:

1 copy 配置文件 conf/server.properties
[root@h2single config]# cp server.properties  server1.properties
[root@h2single config]# cp server.properties  server2.properties

2 修改端口 port
vi  server1.properties
broker.id=1
port=9093
log.dirs=/tmp/kafka1-logs

vi  server2.properties
broker.id=2
port=9094
log.dirs=/tmp/kafka2-logs
 

3 启动另外两个broker:
bin/kafka-server-start.sh config/server1.properties &
bin/kafka-server-start.sh config/server2.properties &

4 jps查看,目前已经启动了三个kafka broker:

5 在创建主题的时候 设定副本个数
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic test1


创建生产者 bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test1 
创建消费者 bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test1 --from-beginning

猜你喜欢

转载自chengjianxiaoxue.iteye.com/blog/2190324