Rocketmq源码分析(1)

复制代码
此次源码分析-rocketmq-spring-boot-starter,starter众所周知入口点就是AutoConfiguration.


RocketMQAutoConfiguration.class
java 复制代码
// 标识为配置类
@Configuration
//将RocketMQProperties识别为配置属性类,创建对象并注入到spring容器中
@EnableConfigurationProperties(RocketMQProperties.class)
// 当类路径中有MQAdmin.class 才启用本配置
@ConditionalOnClass({MQAdmin.class})
//条件启用配置,如果配置rocketmq.name-server不存在,默认会加载此配置,如果没有配置havingValue就意味着rocketmq.name-server 不是 false 就会加载此配置
@ConditionalOnProperty(prefix = "rocketmq", value = "name-server", matchIfMissing = true)
// 把MessageConverterConfiguration、ListenerContainerConfiguration、ExtProducerResetConfiguration、RocketMQTransactionConfiguration 当做配置类加载进来
@Import({MessageConverterConfiguration.class, ListenerContainerConfiguration.class, ExtProducerResetConfiguration.class, RocketMQTransactionConfiguration.class})
// 指定该配置类会在MessageConverterConfiguration配置类的后面加载
@AutoConfigureAfter({MessageConverterConfiguration.class})
// 指定该配置类会在RocketMQTransactionConfiguration配置类的前面加载
@AutoConfigureBefore({RocketMQTransactionConfiguration.class})

public class RocketMQAutoConfiguration {
    private static final Logger log = LoggerFactory.getLogger(RocketMQAutoConfiguration.class);

    public static final String ROCKETMQ_TEMPLATE_DEFAULT_GLOBAL_NAME =
        "rocketMQTemplate";

    @Autowired
    private Environment environment;

    @PostConstruct
    public void checkProperties() {
        String nameServer = environment.getProperty("rocketmq.name-server", String.class);
        log.debug("rocketmq.nameServer = {}", nameServer);
        if (nameServer == null) {
            log.warn("The necessary spring property 'rocketmq.name-server' is not defined, all rockertmq beans creation are skipped!");
        }
    }

    @Bean
    @ConditionalOnMissingBean(DefaultMQProducer.class)
    @ConditionalOnProperty(prefix = "rocketmq", value = {"name-server", "producer.group"})
    public DefaultMQProducer defaultMQProducer(RocketMQProperties rocketMQProperties) {
        RocketMQProperties.Producer producerConfig = rocketMQProperties.getProducer();
        String nameServer = rocketMQProperties.getNameServer();
        String groupName = producerConfig.getGroup();
        Assert.hasText(nameServer, "[rocketmq.name-server] must not be null");
        Assert.hasText(groupName, "[rocketmq.producer.group] must not be null");

        String accessChannel = rocketMQProperties.getAccessChannel();

        String ak = rocketMQProperties.getProducer().getAccessKey();
        String sk = rocketMQProperties.getProducer().getSecretKey();
        boolean isEnableMsgTrace = rocketMQProperties.getProducer().isEnableMsgTrace();
        String customizedTraceTopic = rocketMQProperties.getProducer().getCustomizedTraceTopic();

        DefaultMQProducer producer = RocketMQUtil.createDefaultMQProducer(groupName, ak, sk, isEnableMsgTrace, customizedTraceTopic);

        producer.setNamesrvAddr(nameServer);
        if (!StringUtils.isEmpty(accessChannel)) {
            producer.setAccessChannel(AccessChannel.valueOf(accessChannel));
        }
        producer.setSendMsgTimeout(producerConfig.getSendMessageTimeout());
        producer.setRetryTimesWhenSendFailed(producerConfig.getRetryTimesWhenSendFailed());
        producer.setRetryTimesWhenSendAsyncFailed(producerConfig.getRetryTimesWhenSendAsyncFailed());
        producer.setMaxMessageSize(producerConfig.getMaxMessageSize());
        producer.setCompressMsgBodyOverHowmuch(producerConfig.getCompressMessageBodyThreshold());
        producer.setRetryAnotherBrokerWhenNotStoreOK(producerConfig.isRetryNextServer());

        return producer;
    }

    @Bean(destroyMethod = "destroy")
    @ConditionalOnBean(DefaultMQProducer.class)
    @ConditionalOnMissingBean(name = ROCKETMQ_TEMPLATE_DEFAULT_GLOBAL_NAME)
    public RocketMQTemplate rocketMQTemplate(DefaultMQProducer mqProducer,
        RocketMQMessageConverter rocketMQMessageConverter) {
        RocketMQTemplate rocketMQTemplate = new RocketMQTemplate();
        rocketMQTemplate.setProducer(mqProducer);
        rocketMQTemplate.setMessageConverter(rocketMQMessageConverter.getMessageConverter());
        return rocketMQTemplate;
    }
}

重点 是import注解 引入的这几个配置类。 首先第一个RocketMQTransactionConfiguration配置类,它是来解析RocketMQMessageListener 注解修饰的业务逻辑类的。想要知道RocketMQMessageListener 具体实现方式,就要阅读此类的代码。

java 复制代码
@Configuration
// 此处实现ApplicationContextAware是为了获取容器上下文, 实现SmartInitializingSingleton是为了在单例bean全部初始化后的回调中做一些处理
public class ListenerContainerConfiguration implements ApplicationContextAware, SmartInitializingSingleton {
    ...

    public ListenerContainerConfiguration(RocketMQMessageConverter rocketMQMessageConverter,
        ...
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = (ConfigurableApplicationContext) applicationContext;
    }

    /**
    ** 此处是重点
    */
    @Override
    public void afterSingletonsInstantiated() {
        // 获取容器中所有RocketMQMessageListener注解修饰的bean,并且不是代理对象。
        Map<String, Object> beans = this.applicationContext.getBeansWithAnnotation(RocketMQMessageListener.class)
            .entrySet().stream().filter(entry -> !ScopedProxyUtils.isScopedTarget(entry.getKey()))
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        // 循环处理 
        beans.forEach(this::registerContainer);
    }

    private void registerContainer(String beanName, Object bean) {
        Class<?> clazz = AopProxyUtils.ultimateTargetClass(bean);
        ...
        // 获取注解
        RocketMQMessageListener annotation = clazz.getAnnotation(RocketMQMessageListener.class);

        // 解析占位符 获取最终的消费者组的名称
        String consumerGroup = this.environment.resolvePlaceholders(annotation.consumerGroup());
        String topic = this.environment.resolvePlaceholders(annotation.topic());

        // 获取消费者监听器的状态 默认为true
        boolean listenerEnabled =
            (boolean) rocketMQProperties.getConsumer().getListeners().getOrDefault(consumerGroup, Collections.EMPTY_MAP)
                .getOrDefault(topic, true);

        ...
        
        // bean的名字  加后缀区分
        String containerBeanName = String.format("%s_%s", DefaultRocketMQListenerContainer.class.getName(),
            counter.incrementAndGet());
        GenericApplicationContext genericApplicationContext = (GenericApplicationContext) applicationContext;
        // 注册监听器
        genericApplicationContext.registerBean(containerBeanName, DefaultRocketMQListenerContainer.class,
            () -> createRocketMQListenerContainer(containerBeanName, bean, annotation));
        DefaultRocketMQListenerContainer container = genericApplicationContext.getBean(containerBeanName,
            DefaultRocketMQListenerContainer.class);
        if (!container.isRunning()) {
            try {
                // 启动监听器
                container.start();
            } catch (Exception e) {
                log.error("Started container failed. {}", container, e);
                throw new RuntimeException(e);
            }
        }

        log.info("Register the listener to container, listenerBeanName:{}, containerBeanName:{}", beanName, containerBeanName);
    }

    ...

   
}

最后进入的start

java 复制代码
public synchronized void start() throws MQClientException {
        switch (this.serviceState) {
            //状态的处理
        }
        // 当订阅改变的时候 需要更新topic订阅信息
        this.updateTopicSubscribeInfoWhenSubscriptionChanged();
        // 检查客户端是否已经在 Broker 中注册
        this.mQClientFactory.checkClientInBroker();
        // 发送心跳包给所有的broker
        this.mQClientFactory.sendHeartbeatToAllBrokerWithLock();
        // 触发负载均衡消费者组
        this.mQClientFactory.rebalanceImmediately();
    }

总结上述源码,扫描所有mq的监听器注解,把注解修饰的类注册到容器中,并启动监听。再往深入的start()方法中看,就可以看到消费者、topic、tag相关信息的获取级处理过程,最后是启动。

相关推荐
RemainderTime5 天前
(七)Spring Cloud Alibaba 2023.x:RocketMQ 消息队列配置与实现
后端·spring cloud·微服务·rocketmq
你啊我啊你好6 天前
常用的消息中间件(ActiveMQ、RabbitMQ、RocketMQ、Kafka)面试精华
rabbitmq·rocketmq·activemq
Hello-Brand6 天前
RocketMQ系列3:核心技术介绍
消息队列·rabbitmq·rocketmq·mq·消息中间件·削峰填谷
_BugMan7 天前
【业务场景】订单超时取消的各种方案
redis·java-ee·rocketmq
Ch.yang11 天前
【架构】从 Socket 的角度认识非阻塞模型
tomcat·rocketmq
码农老起11 天前
从RocketMQ到Dubbo:自研中间件技术的崛起
中间件·dubbo·rocketmq
Steven_Mmm13 天前
关于消息队列性能是否能接收 5 万个 MQ
kafka·rocketmq
小范馆13 天前
RocketMQ面试题合集
java·rocketmq·java-rocketmq
菜菜-plus13 天前
Kafka | RabbitMQ | RocketMQ | ActiveMQ 的区别和入门案例
kafka·rabbitmq·rocketmq
Hello-Brand17 天前
RocketMQ系列2:领域模型和技术概念
消息队列·rocketmq·消息中间件