Spring之Kafka

一、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 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值