Pulsar源码解析-客户端-单个消费者ConsumerImpl创建底层实现

pulsar消费者有多种
ConsumerImpl:一个消费者客户端连接
MultiTopicsConsumerImpl:多个消费者,topic配置有多个,如果开启重试队列也是当前实现,具体看MultiTopicsConsumerImpl源码解析
PatternMultiTopicsConsumerImpl:配置正则表达式的topic,本质也是MultiTopicsConsumerImpl,主要实现了topic的自动发现。
ZeroQueueConsumerImpl:没有缓冲队列的消费者 即receiverQueueSize=0

本篇介绍的是相对最好理解的ConsumerImpl,创建方式:

Consumer subscribe = PulsarClient.builder()
             	.serviceUrl("pulsar://127.0.0.1:6650")
             	.build()
              	.newConsumer()
                .topic("public/default/topic")
                .enableRetry(false)
                ...

重点是:topic(...)只配一个,enableRetry(fasle)

之前介绍过生产者的创建,入口是PulsarClientImpl,消费者也是这里。

消费者的介绍跟以前一样,省略参数校验,日志等代码

一、客户端ConsumerImpl创建入口

public class PulsarClientImpl {
   
   

public <T> CompletableFuture<Consumer<T>> subscribeAsync(ConsumerConfigurationData<T> conf, Schema<T> schema, ConsumerInterceptors<T> interceptors) {
   
   
        // 配置的正则
        if (conf.getTopicsPattern() != null) {
   
   
            return patternTopicSubscribeAsync(conf, schema, interceptors);
        } 
      	// 单个消费者
		else if (conf.getTopicNames().size() == 1) {
   
   
            return singleTopicSubscribeAsync(conf, schema, interceptors);
        } 
        // 多个
        else {
   
   
            return multiTopicSubscribeAsync(conf, schema, interceptors);
        }
    }
}

singleTopicSubscribeAsync(...)中先处理schema,然后调用了doSingleTopicSubscribeAsync(...)
直接看 doSingleTopicSubscribeAsync(...)

public class PulsarClientImpl {
   
   

    private <T> CompletableFuture<Consumer<T>> doSingleTopicSubscribeAsync(ConsumerConfigurationData<T> conf, Schema<T> schema, ConsumerInterceptors<T> interceptors) {
   
   
    
        CompletableFuture<Consumer<T>> consumerSubscribedFuture = new CompletableFuture<>();

        String topic = conf.getSingleTopic();
        // 获取分区数
        getPartitionedTopicMetadata(topic).thenAccept(metadata -> {
   
   
            ConsumerBase<T> consumer;
			// 如果是分区topic
            if (metadata.partitions > 0) {
   
   
            	// 多个分区创建MultiTopicsConsumerImpl
            	// 当前方法doSingle命名是不是不太准确...我也想不出更好的命名了
                consumer = MultiTopicsConsumerImpl.createPartitionedConsumer(PulsarClientImpl.this, conf,
                        externalExecutorProvider, consumerSubscribedFuture, metadata.partitions, schema, interceptors);
            } else {
   
   
            	// 非分区topic,partitionIndex是-1
                int partitionIndex = TopicName.getPartitionIndex(topic);
                consumer = ConsumerImpl.newConsumerImpl(PulsarClientImpl.this, topic, conf, externalExecutorProvider,
                        partitionIndex, false, consumerSubscribedFuture, null, schema, interceptors,
                        true);
            }
            consumers.add(consumer);
        }).exceptionally(ex -> {
   
   
            return null;
        });

        return consumerSubscribedFuture;
    }
}

继续ConsumerImpl.newConsumerImpl(...)

二、ConsumerImpl构造

public 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值