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

