• HM-RocketMQ2.2【SpringBoot整合RocketMQ】


    1 前置条件

    相关依赖包:下载rocketmq-spring项目

    rocketmq-spring安装到本地仓库

    mvn install -Dmaven.skip.test=true

    image-20221108105844657

    image-20221108105917387

    image-20221108142938910

    image-20221108143252047

    2 消息生产者

    创建空项目shop-project

    在空项目shop-project下创建空的maven模块springboot-rocketmq-producer

    2.1 添加依赖

    1. <parent>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-parentartifactId>
    4. <version>2.0.1.RELEASEversion>
    5. parent>
    6. <properties>
    7. <rocketmq-spring-boot-starter-version>2.0.3rocketmq-spring-boot-starter-version>
    8. properties>
    9. <dependencies>
    10. <dependency>
    11. <groupId>org.apache.rocketmqgroupId>
    12. <artifactId>rocketmq-spring-boot-starterartifactId>
    13. <version>${rocketmq-spring-boot-starter-version}version>
    14. dependency>
    15. <dependency>
    16. <groupId>org.projectlombokgroupId>
    17. <artifactId>lombokartifactId>
    18. <version>1.18.6version>
    19. dependency>
    20. <dependency>
    21. <groupId>org.springframework.bootgroupId>
    22. <artifactId>spring-boot-starter-testartifactId>
    23. <scope>testscope>
    24. dependency>
    25. dependencies>

    2.2 配置文件

    1. # application.properties
    2. rocketmq.name-server=192.168.115.15:9876;192.168.115.16:9876
    3. rocketmq.producer.group=my-group

    2.3 启动类

    1. package com.irun2u;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. /**
    5. * @Author: haifei
    6. * @Date: 2022/11/8 14:53
    7. */
    8. @SpringBootApplication
    9. public class MQProducerApplication {
    10. public static void main(String[] args) {
    11. SpringApplication.run(MQProducerApplication.class);
    12. }
    13. }

    2.4 测试类

    1. package com.irun2u;
    2. import lombok.extern.slf4j.Slf4j;
    3. import org.apache.rocketmq.spring.core.RocketMQTemplate;
    4. import org.junit.Test;
    5. import org.junit.runner.RunWith;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.boot.test.context.SpringBootTest;
    8. import org.springframework.test.context.junit4.SpringRunner;
    9. /**
    10. * @Author: haifei
    11. * @Date: 2022/11/8 14:57
    12. */
    13. @RunWith(SpringRunner.class)
    14. @SpringBootTest(classes = {MQProducerApplication.class})
    15. @Slf4j
    16. public class ProducerTest {
    17. @Autowired
    18. private RocketMQTemplate rocketMQTemplate;
    19. @Test
    20. public void testSendMsg(){
    21. rocketMQTemplate.convertAndSend("springboot-rocketmq", "hello springboot and rocketmq");
    22. //主题、消息
    23. log.info("消息发送成功");
    24. }
    25. }

    参考HM-RocketMQ1.3【RocketMQ集群搭建】 - yub4by - 博客园 (cnblogs.com)启动rocketmq-console集群监控平台

    运行上述测试方法

    image-20221108152352464

    查看平台中信息

    image-20221108152550527

    image-20221108152605890

    3 消息消费者

    在空项目shop-project下创建空的maven模块springboot-rocketmq-consumer

    3.1 添加依赖

    同上

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-webartifactId>
    4. dependency>

    3.2 配置文件

    同上

    rocketmq.consumer.group=my-group

    3.3 启动类

    1. package com.irun2u;
    2. import lombok.extern.slf4j.Slf4j;
    3. import org.springframework.boot.SpringApplication;
    4. import org.springframework.boot.autoconfigure.SpringBootApplication;
    5. /**
    6. * @Author: haifei
    7. * @Date: 2022/11/8 16:24
    8. */
    9. @Slf4j
    10. @SpringBootApplication
    11. public class MQConsumerApplication {
    12. public static void main(String[] args) {
    13. SpringApplication.run(MQConsumerApplication.class);
    14. log.info("消费者启动成功");
    15. }
    16. }

    3.4 消息监听器

    1. package com.irun2u.listener;
    2. import org.apache.rocketmq.spring.annotation.ConsumeMode;
    3. import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
    4. import org.apache.rocketmq.spring.core.RocketMQListener;
    5. import org.springframework.stereotype.Component;
    6. /**
    7. * @Author: haifei
    8. * @Date: 2022/11/8 16:31
    9. */
    10. @Component
    11. @RocketMQMessageListener(
    12. topic = "springboot-rocketmq",
    13. //consumeMode = ConsumeMode.ORDERLY, //广播CONCURRENTLY;负载均衡ORDERLY(默认)
    14. consumerGroup = "${rocketmq.consumer.group}"
    15. )
    16. public class Consumer implements RocketMQListener {
    17. @Override
    18. public void onMessage(String s) {
    19. System.out.println("接收到消息:" + s);
    20. }
    21. }

    image-20221108163906258

    image-20221108164053283

    image-20221108164245170

  • 相关阅读:
    如何设计线程安全的 HashMap?
    怎么做口碑营销?口碑营销有哪些方式?
    【C进阶】指针笔试题解析
    低代码:时代的选择
    ssm和springboot整合
    java计算机毕业设计新生报到管理系统源程序+mysql+系统+lw文档+远程调试
    【力扣算法简单五十题】06.X的平方根
    CAM350 - 打印装配图 for openpnp
    瑞吉外卖(26)- 添加购物车功能开发
    Spring Security配置
  • 原文地址:https://blog.csdn.net/HAIFEI666/article/details/127756064