Kafka笔记--使用ubuntu为bocker(服务器)windows做producer和comsumer(客户端)
原文连接:http://www.cnblogs.com/davidwang456/p/4201875.html
程序仍然使用之前的一篇博文中的例子 :http://www.cnblogs.com/gnivor/p/4934265.html
这里是将producer和consumer与bocker分离
如何搭建Kafka集群见: http://www.cnblogs.com/gnivor/p/4934073.html
注意:不同的地方
需要改动config文件夹下的server.properties中的以下两个属性
zookeeper.connect=localhost:2181改成zookeeper.connect=192.168.1.164 (IP地址):2181
以及默认注释掉的
#host.name=localhost改成host.name=192.168.1.164 (IP地址) 注意:每个节点这一项要填自己的IP地址。
host.name不更改会造成客户端报如下的错误
Exception in thread "Thread-0" kafka.common.FailedToSendMessageException: Failed to send messages after 3 tries. at kafka.producer.async.DefaultEventHandler.handle(DefaultEventHandler.scala:90) at kafka.producer.Producer.send(Producer.scala:76) at kafka.javaapi.producer.Producer.send(Producer.scala:33) at its.kafka.Producer.run(Producer.java:46)
修改配置之后,启动zookeeper和kafka,然后分别在另外两台主机上运行producer/consumer
接口 KafkaProperties.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public interface KafkaProperties { final static String zkConnect = "192.168.1.160:2181"; final static String groupId = "group1"; final static String topic = "topic1"; // final static String kafkaServerURL = "192.168.1.160"; // final static int kafkaServerPort = 9092; // final static int kafkaProducerBufferSize = 64 * 1024; // final static int connectionTimeOut = 20000; // final static int reconnectInterval = 10000; // final static String topic2 = "topic2"; // final static String topic3 = "topic3"; // final static String clientId = "SimpleConsumerDemoClient"; }
生产者 KafkaProducer.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class KafkaProducer extends Thread { private final kafka.javaapi.producer.Producer<Integer, String> producer; private final String topic; private final Properties props = new Properties(); public KafkaProducer(String topic) { props.put("serializer.class", "kafka.serializer.StringEncoder"); props.put("metadata.broker.list", "192.168.1.160:9092"); // 配置kafka端口 producer = new kafka.javaapi.producer.Producer<Integer, String>(new ProducerConfig(props)); this.topic = topic; } @Override public void run() { int messageNo = 1; while (true) { String messageStr = new String("This is a message, number: " + messageNo); System.out.println("Send:" + messageStr); producer.send(new KeyedMessage<Integer, String>(topic, messageStr)); messageNo++; try { sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
消费者 KafkaConsumer.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.util.Properties; import kafka.consumer.ConsumerConfig; import kafka.consumer.ConsumerIterator; import kafka.consumer.KafkaStream; import kafka.javaapi.consumer.ConsumerConnector; public class KafkaConsumer extends Thread { private final ConsumerConnector consumer; private final String topic; public KafkaConsumer(String topic) { consumer = kafka.consumer.Consumer.createJavaConsumerConnector(createConsumerConfig()); this.topic = topic; } private static ConsumerConfig createConsumerConfig() { Properties props = new Properties(); props.put("zookeeper.connect", KafkaProperties.zkConnect); // zookeeper的地址 props.put("group.id", KafkaProperties.groupId); // 组ID //zk连接超时 props.put("zookeeper.session.timeout.ms", "40000"); props.put("zookeeper.sync.time.ms", "200"); props.put("auto.commit.interval.ms", "1000"); return new ConsumerConfig(props); } @Override public void run() { Map<String, Integer> topicCountMap = new HashMap<String, Integer>(); topicCountMap.put(topic, new Integer(1)); Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap); KafkaStream<byte[], byte[]> stream = consumerMap.get(topic).get(0); ConsumerIterator<byte[], byte[]> it = stream.iterator(); while (it.hasNext()) { System.out.println("receive:" + new String(it.next().message())); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
执行函数 KafkaConsumerProducerDemo.java(注意 producer和consumer可以分开在不同的主机执行)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class KafkaConsumerProducerDemo { public static void main(String[] args) { // KafkaProducer producerThread = new KafkaProducer(KafkaProperties.topic); //这里的topic为“topic1” // producerThread.start(); // // KafkaProducer producerThread2 = new KafkaProducer("topic2"); // producerThread2.start(); KafkaConsumer consumerThread = new KafkaConsumer(KafkaProperties.topic); consumerThread.start(); KafkaConsumer consumerThread2 = new KafkaConsumer("topic2"); consumerThread2.start(); //两对生产者消费者,一对发送和接收topic1,一对发送和接收topic2 } }
producer结果
Send topic1:This is a message, number: 1 Send topic2:This is a message, number: 1 Send topic2:This is a message, number: 2 Send topic1:This is a message, number: 2 Send topic1:This is a message, number: 3 Send topic2:This is a message, number: 3 Send topic1:This is a message, number: 4 Send topic2:This is a message, number: 4 Send topic1:This is a message, number: 5 Send topic2:This is a message, number: 5 Send topic1:This is a message, number: 6 Send topic2:This is a message, number: 6 Send topic2:This is a message, number: 7 Send topic1:This is a message, number: 7
consumer结果
receive topic1:This is a message, number: 1 receive topic2:This is a message, number: 1 receive topic1:This is a message, number: 2 receive topic2:This is a message, number: 2 receive topic1:This is a message, number: 3 receive topic2:This is a message, number: 3 receive topic1:This is a message, number: 4 receive topic2:This is a message, number: 4 receive topic1:This is a message, number: 5 receive topic2:This is a message, number: 5 receive topic1:This is a message, number: 6 receive topic2:This is a message, number: 6 receive topic1:This is a message, number: 7 receive topic2:This is a message, number: 7