Springboot和MQ的服务安装这里就不多介绍了
1、pom文件添加依赖
<!-- rocketmq依赖 -->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.2.3</version>
</dependency>
2、配置yml信息(注意行缩进格式)
rocketmq:
name-server: localhost:9876 # mq地址
producer:
group: Dev_Group # 必须指定group
send-message-timeout: 3000 # 消息发送超时时长,默认3s
retry-times-when-send-failed: 3 # 同步发送消息失败重试次数,默认2
retry-times-when-send-async-failed: 3 # 异步发送消息失败重试次数,默认2
customized-trace-topic: TEST__TOPIC
3、启动遇到问题
依赖和yml配置都写好之后,启动项目,看到报错信息:

***************************
APPLICATION FAILED TO START
***************************
Description:
Field rocketMQTemplate in com.spt.message.service.MqProducerService 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)
Action:
Consider defining a bean of type 'org.apache.rocketmq.spring.core.RocketMQTemplate' in your configuration.
======================解决方案==================
尝试了很多办法都没有解决这个问题,又不想降低SpringBoot的版本。各方查找资料。
最终在github中RocketMQ的官方项目中找到一篇issue解决了这个问题。
https://github.com/apache/rocketmq-spring/pull/541
导致这个问题的原因是:
Springboot-3.0已经放弃了spring.plants自动装配,它被/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports所取代,添加这个文件是为了兼容。
所以,在resources下创建META-INF,然后在META-INF下创建文件:org.springframework.boot.autoconfigure.AutoConfiguration.imports

在org.springframework.boot.autoconfigure.AutoConfiguration.imports文件中加入内容:
org.apache.rocketmq.spring.autoconfigure.RocketMQAutoConfiguration
问题得以解决!
文章讲述了在Springboot应用中集成RocketMQ时遇到的Bean注入问题,具体表现为缺少RocketMQTemplate类型的Bean。问题根源在于Springboot3.0放弃了spring.plants自动装配。解决方案是在资源目录下创建META-INF文件,并在AutoConfiguration.imports文件中添加RocketMQ的自动配置类,从而解决兼容性问题。

1747

被折叠的 条评论
为什么被折叠?



