Kafka-0.8.2.x Consumer API
摘要
本文主要说下此版本消费者API重要的一些代码含义。
0x01 完整例子
1.1 Consumer消费代码
class ConsumerTest implements Runnable {
private KafkaStream m_stream;
private int m_threadNumber;
public ConsumerTest(KafkaStream a_stream, int a_threadNumber) {
m_threadNumber = a_threadNumber;
m_stream = a_stream;
}
public void run() {
// Consumer线程只需要使用iterator遍历该消息,进行业务逻辑处理即可
ConsumerIterator<byte[], byte[]> it = m_stream.iterator();
while (it.hasNext())
System.out.println("Thread " + m_threadNumber + ": " + new String(it.next().message()));
System.out.println("Shutting down Thread: " + m_threadNumber);
}
}
1.2 构建消费者、main函数等代码
package com.test.groups;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ConsumerGroupExample {
private final ConsumerConnector consumer;
private final String topic;
private ExecutorService executor;
public ConsumerGroupExample(String a_zookeeper, String a_groupId, String a_topic) {
// 通过配置构建一个KafkaConsumer实例
consumer = kafka.consumer.Consumer.createJavaConsumerConnector(
createConsumerConfig(a_zookeeper, a_groupId));
this.topic = a_topic;
}
public void shutdown() {
if (consumer != null) consumer.shutdown();
if (executor != null) executor.shutdown();
try {
if (!executor.awaitTermination(5000, TimeUnit.MILLISECONDS)) {
System.out.println("Timed out waiting for consumer threads to shut down, exiting uncleanly");
}
} catch (InterruptedException e) {
System.out.println("Interrupted during shutdown, exiting uncleanly");
}
}
public void run(int a_numThreads) {
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
// 确定一个Consumer实例对于指定的topic拥有几个线程来处理
topicCountMap.put(topic, new Integer(a_numThreads));
// 通过topicCountMap构建消息流
Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
// 对指定topic构建消息流list
List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic);
// 创建一个指定线程数量的线程池
executor = Executors.newFixedThreadPool(a_numThreads);
// 遍历消息流,每个都创建一个消费者线程提交到线程池进行处理
int threadNumber = 0;
for (final KafkaStream stream : streams) {
executor.submit(new ConsumerTest(stream, threadNumber));
threadNumber++;
}
}
/**
* 构建Consumer基础配置
*/
private static ConsumerConfig createConsumerConfig(String a_zookeeper, String a_groupId) {
Properties props = new Properties();
props.put("zookeeper.connect", a_zookeeper);
props.put("group.id", a_groupId);
props.put("zookeeper.session.timeout.ms", "400");
props.put("zookeeper.sync.time.ms", "200");
props.put("auto.commit.interval.ms", "1000");
return new ConsumerConfig(props);
}
public static void main(String[] args) {
String zooKeeper = args[0];
String groupId = args[1];
String topic = args[2];
// 消费者线程数
int threads = Integer.parseInt(args[3]);
ConsumerGroupExample example = new ConsumerGroupExample(zooKeeper, groupId, topic);
example.run(threads);
try {
Thread.sleep(10000);
} catch (InterruptedException ie) {
}
example.shutdown();
}
}
0x02 一些需要注意的点
2.1 消费者线程数配置
topicCountMap.put(topic, new Integer(a_numThreads));
- 使用topicCountMap配置topic线程数的时候,这个是针对同一个组的一个消费者实例的。如果总的线程数大于了partition数量 ,那就会导致一些线程不会消费任何数据。
- 反过来,如果partitions数大于线程数,那么一些线程会同时接收多个partition数据。注意此时仍然只能保证一个partition内部有序。
- 当添加更多Consumer实例或线程时会导致Kafka再平衡,可能影响parition分配给线程的策略。