首页 > 网络 > 云计算 >

kafka集群搭建和使用Java写kafka生产者消费者

2016-12-26

kafka集群搭建和使用Java写kafka生产者消费者

1 kafka集群搭建

1.zookeeper集群搭建在110,111,112

2.kafka使用3个节点110,111,112

修改配置文件config/server.properties

broker.id=110

host.name=192.168.1.110

log.dirs=/usr/local/kafka_2.10-0.8.2.0/logs

复制到其他两个节点,然后修改对应节点上的config/server.pro

3.启动,在三个节点分别执行

bin/kafka-server-start.shconfig/server.properties>/dev/null2>&1&

4创建主题

bin/kafka-topics.sh--create--zookeeperlocalhost:2181--replication-factor3--partitions3--topictest

5查看主题详细

bin/kafka-topics.sh--describe--zookeeperlocalhost:2181--topictest

--topictest

Topic:testPartitionCount:3ReplicationFactor:3Configs:

Topic:testPartition:0Leader:110Replicas:110,111,112Isr:110,111,112

Topic:testPartition:1Leader:111Replicas:111,112,110Isr:111,112,110

Topic:testPartition:2Leader:112Replicas:112,110,111Isr:112,110,111

6去zk上看kafka集群

[zk:localhost:2181(CONNECTED)5]ls/

[admin,zookeeper,consumers,config,controller,zk-fifo,storm,brokers,controller_epoch]

[zk:localhost:2181(CONNECTED)6]ls/brokers---->查看注册在zk内的kafka

[topics,ids]

[zk:localhost:2181(CONNECTED)7]ls/brokers/ids

[112,110,111]

[zk:localhost:2181(CONNECTED)8]ls/brokers/ids/112

[]

[zk:localhost:2181(CONNECTED)9]ls/brokers/topics

[test]

[zk:localhost:2181(CONNECTED)10]ls/brokers/topics/test

[partitions]

[zk:localhost:2181(CONNECTED)11]ls/brokers/topics/test/partitions

[2,1,0]

[zk:localhost:2181(CONNECTED)12]

2 kafka java调用:

2.1 java端生产数据, kafka集群消费数据:

Java代码

收藏代码

1创建maven工程,pom.xml中增加如下:

org.apache.kafka

kafka_2.10

0.8.2.0

2java代码:向主题test内写入数据

importjava.util.Properties;

importjava.util.concurrent.TimeUnit;

importkafka.javaapi.producer.Producer;

importkafka.producer.KeyedMessage;

importkafka.producer.ProducerConfig;

importkafka.serializer.StringEncoder;

publicclasskafkaProducerextendsThread{

privateStringtopic;

publickafkaProducer(Stringtopic){

super();

this.topic=topic;

}

@Override

publicvoidrun(){

Producerproducer=createProducer();

inti=0;

while(true){

producer.send(newKeyedMessage(topic,"message:"+i++));

try{

TimeUnit.SECONDS.sleep(1);

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

privateProducercreateProducer(){

Propertiesproperties=newProperties();

properties.put("zookeeper.connect","192.168.1.110:2181,192.168.1.111:2181,192.168.1.112:2181");//声明zk

properties.put("serializer.class",StringEncoder.class.getName());

properties.put("metadata.broker.list","192.168.1.110:9092,192.168.1.111:9093,192.168.1.112:9094");//声明kafkabroker

returnnewProducer(newProducerConfig(properties));

}

publicstaticvoidmain(String[]args){

newkafkaProducer("test").start();//使用kafka集群中创建好的主题test

}

}

3kafka集群中消费主题test的数据:

[root@h2masterkafka]#bin/kafka-console-consumer.sh--zookeeperlocalhost:2181--topictest--from-beginnin

4启动java代码,然后在看集群消费的数据如下:

message:0

message:1

message:2

message:3

message:4

message:5

message:6

message:7

message:8

message:9

message:10

message:11

message:12

message:13

message:14

message:15

message:16

message:17

message:18

message:19

message:20

message:21

3 kafka 使用Java写消费者,这样 先运行kafkaProducer ,在运行kafkaConsumer,即可得到生产者的数据:

Java代码

收藏代码

importjava.util.HashMap;

importjava.util.List;

importjava.util.Map;

importjava.util.Properties;

importkafka.consumer.Consumer;

importkafka.consumer.ConsumerConfig;

importkafka.consumer.ConsumerIterator;

importkafka.consumer.KafkaStream;

importkafka.javaapi.consumer.ConsumerConnector;

/**

*接收数据

*接收到:message:10

接收到:message:11

接收到:message:12

接收到:message:13

接收到:message:14

*@authorzm

*

*/

publicclasskafkaConsumerextendsThread{

privateStringtopic;

publickafkaConsumer(Stringtopic){

super();

this.topic=topic;

}

@Override

publicvoidrun(){

ConsumerConnectorconsumer=createConsumer();

MaptopicCountMap=newHashMap();

topicCountMap.put(topic,1);//一次从主题中获取一个数据

Map>>messageStreams=consumer.createMessageStreams(topicCountMap);

KafkaStreamstream=messageStreams.get(topic).get(0);//获取每次接收到的这个数据

ConsumerIteratoriterator=stream.iterator();

while(iterator.hasNext()){

Stringmessage=newString(iterator.next().message());

System.out.println("接收到:"+message);

}

}

privateConsumerConnectorcreateConsumer(){

Propertiesproperties=newProperties();

properties.put("zookeeper.connect","192.168.1.110:2181,192.168.1.111:2181,192.168.1.112:2181");//声明zk

properties.put("group.id","group1");//必须要使用别的组名称,如果生产者和消费者都在同一组,则不能访问同一组内的topic数据

returnConsumer.createJavaConsumerConnector(newConsumerConfig(properties));

}

publicstaticvoidmain(String[]args){

newkafkaConsumer("test").start();//使用kafka集群中创建好的主题test

}

}

相关文章
最新文章
热点推荐