java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot2.1.0+RocketMQ版本冲突

解决SpringBoot2.1.0+RocketMQ版本冲突问题

作者:万物~

这篇文章主要介绍了解决SpringBoot2.1.0+RocketMQ版本冲突问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

介绍

列一下主要的pom

<!--RocketMQ相关-->
        <dependency>
            <groupId>org.apache.rocketmq</groupId>
            <artifactId>rocketmq-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.rocketmq</groupId>
            <artifactId>rocketmq-client</artifactId>
            <version>4.4.0</version>
        </dependency>

application.properties配置

# RocketMQ相关配置
spring.rocketmq.nameServer=ip:9876
spring.rocketmq.producer.group=test

代码

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

     public void test(){
        try {
            // 使用rockMq发送消息
            HashMap<String, Object> msg = new HashMap<>(2);
            msg.put("id",user.getId());
            msg.put("mobile",mobile);
            msg.put("date",new Date());
            this.rocketMQTemplate.convertAndSend("login",msg);
        }catch (Exception e){
            LOGGER.error("发送消息出错",e);
        }
}

然后关键来了,项目启动后报错

Description:

Field rocketMQTemplate in com.tanhua.sso.service.UserService required a bean of type 'org.apache.rocketmq.spring.core.RocketMQTemplate' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

The following candidates were found but could not be injected:
    - Bean method 'rocketMQTemplate' in 'RocketMQAutoConfiguration' not loaded because @ConditionalOnProperty (rocketmq.name-server) did not find property 'name-server'


Action:

Consider revisiting the entries above or defining a bean of type 'org.apache.rocketmq.spring.core.RocketMQTemplate' in your configuration.

这里网上查了很多资料都没有解决,定位到问题的原因是因为org.apache.rocketmq.spring.autoconfigure.RocketMQAutoConfiguration类的问题

@Configuration
@EnableConfigurationProperties(RocketMQProperties.class)
@ConditionalOnClass({ MQAdmin.class, ObjectMapper.class })
@ConditionalOnProperty(prefix = "rocketmq", value = "name-server")
@Import({ JacksonFallbackConfiguration.class, ListenerContainerConfiguration.class })
@AutoConfigureAfter(JacksonAutoConfiguration.class)
public class RocketMQAutoConfiguration {

    @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");

        DefaultMQProducer producer;
        String ak = rocketMQProperties.getProducer().getAccessKey();
        String sk = rocketMQProperties.getProducer().getSecretKey();
        if (!StringUtils.isEmpty(ak) && !StringUtils.isEmpty(sk)) {
            producer = new DefaultMQProducer(groupName, new AclClientRPCHook(new SessionCredentials(ak, sk)),
                rocketMQProperties.getProducer().isEnableMsgTrace(),
                rocketMQProperties.getProducer().getCustomizedTraceTopic());
            producer.setVipChannelEnabled(false);
        } else {
            producer = new DefaultMQProducer(groupName, rocketMQProperties.getProducer().isEnableMsgTrace(),
                rocketMQProperties.getProducer().getCustomizedTraceTopic());
        }

        producer.setNamesrvAddr(nameServer);
        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;
    }

从报错信息发现可能是nameSever不匹配name-server导致,对应类中是@ConditionalOnProperty(prefix = “rocketmq”, value = “name-server”)这个注解

下面列一下@ConditionalOnProperty注解参数的意思

所以猜测配置文件要修改成对应的参数才行

将application中的配置改为

rocketmq.name-server=ip:9876
rocketmq.producer.group=test

改完后启动再次报错

Description:

An attempt was made to call the method org.apache.rocketmq.client.producer.DefaultMQProducer.<init>(Ljava/lang/String;ZLjava/lang/String;)V but it does not exist. Its class, org.apache.rocketmq.client.producer.DefaultMQProducer, is available from the following locations:

    jar:file:/D:/maven/yycg_repository/org/apache/rocketmq/rocketmq-client/4.3.2/rocketmq-client-4.3.2.jar!/org/apache/rocketmq/client/producer/DefaultMQProducer.class

It was loaded from the following location:

    file:/D:/maven/yycg_repository/org/apache/rocketmq/rocketmq-client/4.3.2/rocketmq-client-4.3.2.jar

这次的报错是版本冲突问题,经过我无数次的实验(流泪),最终将RocketMQ-client使用的版本是4.3.2改为4.4.0解决

<dependency>
            <groupId>org.apache.rocketmq</groupId>
            <artifactId>rocketmq-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.rocketmq</groupId>
            <artifactId>rocketmq-client</artifactId>
            <version>4.4.0</version>
        </dependency>

然后启动项目,没有报错,完美解决

这次这个问题费了我很多时间,记录一下。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文