ps.本文所有源码都基于kafka-0.10.0.1
Kafka提供了Java版本的生产者实现--KafkaProducer,使用KafkaProducer的API可以轻松实现同步/异步发送消息、批量发送、超时重发等复杂的功能,KafkaProducer是线程安全的,多个线程之间可以共享实用同一个KafkaProducer对象。
下面先看一个使用上的小例子:
public static void main(String[] args) { boolean isAsync = args.length == 0 || !args[0].trim().equalsIgnoreCase("sync"); Properties properties = new Properties(); properties.put("bootstrap.servers", "localhost:9092"); properties.put("client.id", "DemoProducer"); properties.put("key.serializer", "org.apache.kafka.common.serialization.IntegerSerializer"); properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); KafkaProducer producer = new KafkaProducer<>(properties); String topic = "test"; int messageNo = 1; while(true) { String messageStr = "Message_" + messageNo; long startTime = System.currentTimeMillis(); if(isAsync) { //ProducerRecord可以当成一个消息,包含key,value,和topic //异步要有回调函数 producer.send(new ProducerRecord(topic, messageNo, messageStr), new DemoCallBack(startTime, messageNo, messageStr)); } else { //同步发送 try { //KafkaProducer.send()返回的类型是Future<RecordMetadata>

本文详细分析了KafkaProducer发送消息的流程,包括ProducerInterceptors的消息拦截、Serializer的序列化、Partitioner的选择、RecordAccumulator的批量发送、Sender的请求构造以及网络I/O过程。特别指出waitOnMetadata函数确保了目标topic的元数据可用,以便正确路由和发送消息到正确分区的leader副本。KafkaProducer维护了kafka集群的元数据,包含了topic的分区信息、副本分配和节点地址等。
最低0.47元/天 解锁文章
175

被折叠的 条评论
为什么被折叠?



