1 前置条件
相关依赖包:下载rocketmq-spring项目
将rocketmq-spring安装到本地仓库
mvn install -Dmaven.skip.test=true
2 消息生产者
创建空项目shop-project
在空项目shop-project下创建空的maven模块springboot-rocketmq-producer
2.1 添加依赖
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.0.1.RELEASEversion>
- parent>
-
- <properties>
- <rocketmq-spring-boot-starter-version>2.0.3rocketmq-spring-boot-starter-version>
- properties>
-
- <dependencies>
- <dependency>
- <groupId>org.apache.rocketmqgroupId>
- <artifactId>rocketmq-spring-boot-starterartifactId>
- <version>${rocketmq-spring-boot-starter-version}version>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>1.18.6version>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
-
- dependencies>
2.2 配置文件
- # application.properties
- rocketmq.name-server=192.168.115.15:9876;192.168.115.16:9876
- rocketmq.producer.group=my-group
2.3 启动类
- package com.irun2u;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- /**
- * @Author: haifei
- * @Date: 2022/11/8 14:53
- */
- @SpringBootApplication
- public class MQProducerApplication {
- public static void main(String[] args) {
- SpringApplication.run(MQProducerApplication.class);
- }
- }
2.4 测试类
- package com.irun2u;
-
- import lombok.extern.slf4j.Slf4j;
- import org.apache.rocketmq.spring.core.RocketMQTemplate;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
-
- /**
- * @Author: haifei
- * @Date: 2022/11/8 14:57
- */
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes = {MQProducerApplication.class})
- @Slf4j
- public class ProducerTest {
-
- @Autowired
- private RocketMQTemplate rocketMQTemplate;
-
- @Test
- public void testSendMsg(){
- rocketMQTemplate.convertAndSend("springboot-rocketmq", "hello springboot and rocketmq");
- //主题、消息
- log.info("消息发送成功");
- }
- }
参考HM-RocketMQ1.3【RocketMQ集群搭建】 - yub4by - 博客园 (cnblogs.com)启动rocketmq-console集群监控平台
运行上述测试方法
查看平台中信息
3 消息消费者
在空项目shop-project下创建空的maven模块springboot-rocketmq-consumer
3.1 添加依赖
同上
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
3.2 配置文件
同上
改
rocketmq.consumer.group=my-group
3.3 启动类
- package com.irun2u;
-
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- /**
- * @Author: haifei
- * @Date: 2022/11/8 16:24
- */
- @Slf4j
- @SpringBootApplication
- public class MQConsumerApplication {
- public static void main(String[] args) {
- SpringApplication.run(MQConsumerApplication.class);
- log.info("消费者启动成功");
- }
- }
3.4 消息监听器
- package com.irun2u.listener;
-
- import org.apache.rocketmq.spring.annotation.ConsumeMode;
- import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
- import org.apache.rocketmq.spring.core.RocketMQListener;
- import org.springframework.stereotype.Component;
-
- /**
- * @Author: haifei
- * @Date: 2022/11/8 16:31
- */
- @Component
- @RocketMQMessageListener(
- topic = "springboot-rocketmq",
- //consumeMode = ConsumeMode.ORDERLY, //广播CONCURRENTLY;负载均衡ORDERLY(默认)
- consumerGroup = "${rocketmq.consumer.group}"
- )
- public class Consumer implements RocketMQListener
{ - @Override
- public void onMessage(String s) {
- System.out.println("接收到消息:" + s);
- }
- }