(1)本地安装RocketMq
https://blog.csdn.net/bbc2005/article/details/85218497
(2)rocket-console代码克隆好之后,分支切换到release-rocketmq-console-1.0.0
(3)根据连接里的教程修改配置文件,重新maven打包启动项目(如果报错的话,就把本地仓库相关的包删了重新下一遍),正确启动是这个样子。
(4)在springboot项目中yml文件中添加rocketMQ配置
rocketmq:
name-server: 127.0.0.1:9876
producer:
group: my-group
(5)启动的时候报了一个错,查了一下是依赖版本冲突了,原先的rocketmq-spring-boot-starter用的是2.1.1改成了2.1.0就可以正常启动了。
<!-- https://mvnrepository.com/artifact/org.apache.rocketmq/rocketmq-spring-boot-starter -->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.rocketmq/rocketmq-client -->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-common</artifactId>
<version>4.5.1</version>
</dependency>
(6) 编写一个简单的生产者和消费者demo
/*
* @Description :生产者产生消息
* @URL :
* @param key
* @return : com.example.sbdemo.pojo.quartz.QrtzCronTriggers
* @Author : zouyuxian
* @Date : 2022/4/8 17:05
**/
@Override
public QrtzCronTriggers pushMessage(QrtzCronTriggersKey key) {
QrtzCronTriggers qrtzCronTriggers = qrtzCronTriggersDao.selectByPrimaryKey(key);
rocketMQTemplate.convertAndSend("first-topic","你好,Java旅途" + qrtzCronTriggers.getCronExpression());
return qrtzCronTriggers;
}
/**
* @author issuser
* @title: Consumer
* @projectName sbdemo
* @description: 消费消息
* @date 2022/4/817:05
*/
@Component
@RocketMQMessageListener(topic="first-topic",consumerGroup="my-group")
public class Consumer implements RocketMQListener {
@Override
public void onMessage(Object o) {
System.out.println(o);
}
}
(6)此时启动项目报错,rocketmq-client 4.3.2中没有AccessChannel这个文件,猜测是这个版本没有这个方法,升级到rocketmq-client 4.5.2可以正常启动了。
nested exception is java.lang.NoClassDefFoundError: org/apache/rocketmq/client/AccessChannel
(7)调用之后可以在控制台查询