之前一直有项目用到,不过我并不负责这一块,所以了解不多,这次趁机会学习下。
之前对kafka的了解其实仅限于知道它是一个分布式消息系统,这次详细了解了下,知道了一些关键概念(topic主题、broker服务、producers消息发布者、consumer消息订阅者消费者),具体网上一大堆,这里不赘述,直接开始代码。
<dependency>
<groupId>org.apache.kafkagroupId>
<artifactId>kafka_2.10artifactId>
<version>0.10.0.0version>
dependency>
实际上我倒不是以上面方式引入的,因为使用kafka还是为了后面跟spark steaming集成,所以我是引入的spark-streaming-kafka,依赖包自然会被引入,需求相同的话可以像下面这样引入。
<dependency>
<groupId>org.apache.sparkgroupId>
<artifactId>spark-streaming-kafka-0-10_2.11artifactId>
<version>2.0.0version>
dependency>
这里使用KafkaProducer类,官方已经不建议使用Producer类,实现一个线程类,进行消息发布,实际的代码其实很简单,不过本来也就是要一个demo。
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
public class UserKafkaProducer extends Thread
{
private final KafkaProducer producer;
private final String topic;
private final Properties props = new Properties();
public UserKafkaProducer(String topic)
{
props.put("metadata.broker.list", "master2:6667");
props.put("bootstrap.servers", "master2:6667");
props.put("acks", "all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("linger.ms", 1);
props.put("buffer.memory", 33554432);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
producer = new KafkaProducer(props);
this.topic = topic;
}
@Override
public void run() {
int messageNo = 1;
while (true)
{
String messageStr = new String("Message_" + messageNo);
System.out.println("Send:" + messageStr);
producer.send(new ProducerRecord(topic, messageStr));
messageNo++;
try {
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import kafka.consumer.ConsumerConfig;
import kafka.javaapi.consumer.ConsumerConnector;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
public class UserKafkaConsumer extends Thread
{
private final ConsumerConnector consumer;
private final String topic;
public UserKafkaConsumer(String topic)
{
consumer = kafka.consumer.Consumer.createJavaConsumerConnector(
createConsumerConfig());
this.topic = topic;
}
private static ConsumerConfig createConsumerConfig()
{
Properties props = new Properties();
props.put("zookeeper.connect", "master1:2181,master2:2181");
props.put("group.id", "group1");
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 topicCountMap = new HashMap();
topicCountMap.put(topic, new Integer(1));
Mapbyte[], 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(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args)
{
UserKafkaProducer producerThread = new UserKafkaProducer(KafkaProperties.topic);
producerThread.start();
UserKafkaConsumer consumerThread = new UserKafkaConsumer(KafkaProperties.topic);
consumerThread.start();
}
运行即可。