一、Kafka Producer
消息生产者
org.springframework.kafka.core.KafkaOperations 定义Kafka Producer发送消息操作接口
org.springframework.kafka.core.KafkaTemplate实现KafkaOperations接口,最终调用doSend方法发送消息
消息发送执行结果回调
org.springframework.kafka.support.ProducerListener 消息发送后回调,默认是LoggingProducerListener记录日志
org.springframework.kafka.support.LoggingProducerListener 发送失败记录日志
消息转换器
org.springframework.kafka.support.converter.MessageConverter
org.springframework.kafka.support.converter.RecordMessageConverter 将ConsumerRecord转换为Message,或者将Message转换为ProducerRecord
org.springframework.kafka.support.converter.MessagingMessageConverter 转换Kafka消息
/**
* Send the producer record.
* @param producerRecord the producer record.
* @return a Future for the {@link RecordMetadata}.
*/
protected ListenableFuture<SendResult<K, V>> doSend(final ProducerRecord<K, V> producerRecord) {
final Producer<K, V> producer = getTheProducer();
if (this.logger.isTraceEnabled()) {
this.logger.trace("Sending: " + producerRecord);
}
final SettableListenableFuture<SendResult<K, V>> future = new SettableListenableFuture<>();
producer.send(producerRecord, new Callback() {
@Override
public void onCompletion(RecordMetadata metadata, Exception exception) {
try {
if (exception == null) {
future.set(new SendResult<>(producerRecord, metadata));
if (KafkaTemplate.this.producerListener != null
&& KafkaTemplate.this.producerListener.isInterestedInSuccess()) {
KafkaTemplate.this.producerListener.onSuccess(producerRecord.topic(),
producerRecord.partition(), producerRecord.key(), producerRecord.value(), metadata);
}
}
else {
future.setException(new KafkaProducerException(producerRecord, "Failed to send", exception));
if (KafkaTemplate.this.producerListener != null) {
KafkaTemplate.this.producerListener.onError(producerRecord.topic(),
producerRecord.partition(),
producerRecord.key(),
producerRecord.value(),
exception);
}
}
}
finally {
producer.close();
}
}
});
if (this.autoFlush) {
flush();
}
if (this.logger.isTraceEnabled()) {
this.logger.trace("Sent: " + producerRecord);
}
return future;
}
消息生产者工厂
org.springframework.kafka.core.ProducerFactory 定义创建发送消息Producer接口
org.springframework.kafka.core.DefaultKafkaProducerFactory实现ProducerFactory 接口
@Override
public Producer<K, V> createProducer() {
if (this.producer == null) {
synchronized (this) {
if (this.producer == null) {
this.producer = new CloseSafeProducer<K, V>(createKafkaProducer());
}
}
}
return this.producer;
}
使用Kafka客户端API创建KafkaProducer
protected KafkaProducer<K, V> createKafkaProducer() {
return new KafkaProducer<K, V>(this.configs, this.keySerializer, this.valueSerializer);
}
操作委托给KafkaProducer
private static class CloseSafeProducer<K, V> implements Producer<K, V> {
private final Producer<K, V> delegate;
CloseSafeProducer(Producer<K, V> delegate) {
this.delegate = delegate;
}
@Override
public Future<RecordMetadata> send(ProducerRecord<K, V> record) {
return this.delegate.send(record);
}
@Override
public Future<RecordMetadata> send(ProducerRecord<K, V> record, Callback callback) {
return this.delegate.send(record, callback);
}
@Override
public void flush() {
this.delegate.flush();
}
@Override
public List<PartitionInfo> partitionsFor(String topic) {
return this.delegate.partitionsFor(topic);
}
@Override
public Map<MetricName, ? extends Metric> metrics() {
return this.delegate.metrics();
}
@Override
public void close() {
}
@Override
public void close(long timeout, TimeUnit unit) {
}
}
二、Kafka Consumer
创建消息消费者工厂
org.springframework.kafka.core.ConsumerFactory
org.springframework.kafka.core.DefaultKafkaConsumerFactory
使用Kafka客户端API创建KafkaConsumer
protected KafkaConsumer<K, V> createKafkaConsumer(Map<String, Object> configs) {
return new KafkaConsumer<K, V>(configs, this.keyDeserializer, this.valueDeserializer);
}
org.springframework.kafka.annotation.KafkaListenerAnnotationBeanPostProcessor根据注解KafkaListener
org.springframework.kafka.listener.KafkaMessageListenerContainer
org.springframework.kafka.listener.ConcurrentMessageListenerContainer