解決SpringBoot2.1.0+RocketMQ版本沖突問題
介紹
- 我的項(xiàng)目使用的是springboot2.1.0
- rocketmq-spring-boot-starter 使用的是2.0.2
- RocketMQ-client使用的版本是4.3.2
- RocketMQ使用阿里云的docker搭建
列一下主要的pom
<!--RocketMQ相關(guān)--> <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相關(guān)配置 spring.rocketmq.nameServer=ip:9876 spring.rocketmq.producer.group=test
代碼
@Autowired private RocketMQTemplate rocketMQTemplate; public void test(){ try { // 使用rockMq發(fā)送消息 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("發(fā)送消息出錯(cuò)",e); } }
然后關(guān)鍵來了,項(xiàng)目啟動(dòng)后報(bào)錯(cuò)
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.
這里網(wǎng)上查了很多資料都沒有解決,定位到問題的原因是因?yàn)閛rg.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; }
從報(bào)錯(cuò)信息發(fā)現(xiàn)可能是nameSever不匹配name-server導(dǎo)致,對(duì)應(yīng)類中是@ConditionalOnProperty(prefix = “rocketmq”, value = “name-server”)這個(gè)注解
下面列一下@ConditionalOnProperty注解參數(shù)的意思
- prefix application.properties配置的前綴
- name 屬性是從application.properties配置文件中讀取屬性值
所以猜測(cè)配置文件要修改成對(duì)應(yīng)的參數(shù)才行
將application中的配置改為
rocketmq.name-server=ip:9876 rocketmq.producer.group=test
改完后啟動(dòng)再次報(bào)錯(cuò)
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
這次的報(bào)錯(cuò)是版本沖突問題,經(jīng)過我無數(shù)次的實(shí)驗(yàn)(流淚),最終將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>
然后啟動(dòng)項(xiàng)目,沒有報(bào)錯(cuò),完美解決
這次這個(gè)問題費(fèi)了我很多時(shí)間,記錄一下。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?Boot應(yīng)用中如何動(dòng)態(tài)指定數(shù)據(jù)庫實(shí)現(xiàn)不同用戶不同數(shù)據(jù)庫的問題
讓我們創(chuàng)建一個(gè) Spring Boot 項(xiàng)目首先設(shè)置一個(gè)具有必要依賴項(xiàng)的新 Spring Boot項(xiàng)目,在項(xiàng)目配置中包括 Spring Web、Spring Data JPA 和關(guān)于數(shù)據(jù)庫的依賴項(xiàng),接下來介紹Spring?Boot應(yīng)用中如何動(dòng)態(tài)指定數(shù)據(jù)庫,實(shí)現(xiàn)不同用戶不同數(shù)據(jù)庫的場(chǎng)景?,需要的朋友可以參考下2024-04-04postman?如何實(shí)現(xiàn)傳遞?ArrayList?給后臺(tái)
這篇文章主要介紹了postman?如何實(shí)現(xiàn)傳遞?ArrayList給后臺(tái),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Spring中Bean創(chuàng)建完后打印語句的兩種方法
這篇文章主要介紹了Spring中Bean創(chuàng)建完后打印語句的兩種方法,一個(gè)是實(shí)現(xiàn)InitializingBean接口,另一個(gè)使用@Bean注解和initMethod屬性,通過代碼示例介紹的非常詳細(xì),感興趣的小伙伴可以參考閱讀2023-07-07Mybatis 查詢語句條件為枚舉類型時(shí)報(bào)錯(cuò)的解決
這篇文章主要介紹了Mybatis 查詢語句條件為枚舉類型時(shí)報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01詳細(xì)介紹Java關(guān)鍵字throw?throws?Throwable的用法與區(qū)別
這篇文章主要介紹了java中throws與throw及Throwable的用法和區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04微信APP支付(IOS手機(jī)端+java后臺(tái))版
這篇文章主要為大家詳細(xì)介紹了微信APP支付(IOS手機(jī)端+java后臺(tái))版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05Java類中this關(guān)鍵字與static關(guān)鍵字的用法解析
這篇文章主要介紹了Java類中this關(guān)鍵字與static關(guān)鍵字的用法解析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09