【RabbitMQ】使用SpringAMQP的消息队列(Hello Word)和工作队列(Work Queue)

SpringAMQP

SpringAMQP中文文档

Hello Word

**案例1:**利用SpringAMQP实现HelloWord中的集成消息队列功能

项目结构,如图:

1.引入AMQP依赖(父工程中)

xml 复制代码
<!--AMQP依赖,包含RabbitMQ-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2.publisher包的application.yml中添加MQ连接信息

yml 复制代码
logging:
  pattern:
    dateformat: MM-dd HH:mm:ss:SSS
spring:
  rabbitmq:
    host: 192.168.174.129 # rabbitMQ的ip地址(我是在虚拟机上用docker安装的RabbitMQ)
    port: 5672 # 端口
    virtual-host: / # 虚拟主机
    username: itcast # 用户名
    password: 123321 # 密码

3.在消息发送者(publisher包)中新建一个测试类SpringAmqpTest,编写测试方法

java 复制代码
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringAmqpTest {
    @Autowired
    private RabbitTemplate rabbitTemplate
	@Test
	public void testSimpleQueue(){
		String queueName ="simple.queue"; //队列名称
		String message ="hello, spring amgp!"; //消息
		rabbitTemplate.convertAndSend(queueName,message);
    }
}

4.运行,结果

5.在consumer中编写消费逻辑,监听simple.queue

5.1consumer包的application.yml中添加MQ连接信息
yml 复制代码
logging:
  pattern:
    dateformat: MM-dd HH:mm:ss:SSS
spring:
  rabbitmq:
    host: 192.168.174.129 # rabbitMQ的ip地址(我是在虚拟机上用docker安装的RabbitMQ)
    port: 5672 # 端口
    virtual-host: / # 虚拟主机
    username: itcast # 用户名
    password: 123321 # 密码
5.2在消费者(consumer包)新建一个类SpringRabbitListener,编写消费逻辑
java 复制代码
@Component
public class SpringRabbitListener {
    
    @RabbitListener(queues="simple.queue")
	public void listenSimple0ueueMessage(String msg) throws InterruptedException {
		System.out.println("spring 消费者接收到消息 :【"+ msq +"】");
    }
}
5.3运行,结果

simple.queue队列中的消息也为空了

Work Queue

案例2:WorkQueue工作队列,实现一个队列绑定多个消费者

Work queue,工作队列,可以提高消息处理速度,避免队列消息堆积

前提:在案例1的基础上

1.在SpringAmqpTest添加新的发布

java 复制代码
//消息发送50次 
@Test
    public void testSendMessage2WorkQueue() throws InterruptedException {
        String queueName = "simple.queue";
        String message = "hello, message__";
        for (int i = 1; i <= 50; i++) {
            rabbitTemplate.convertAndSend(queueName, message + i);
            Thread.sleep(20); //休眠20秒
        }
    }

2.注释掉SpringRabbitListener上原有方法,添加新的消费

java 复制代码
 @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1接收到消息:【" + msg + "】" + LocalTime.now());
        Thread.sleep(20);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2........接收到消息:【" + msg + "】" + LocalTime.now());
        Thread.sleep(200);
    }

3.运行,结果

结果为消费,平均分配

4.消费预取限制,能者多劳---在消费者(consumer包)的application.yml修改

yml 复制代码
logging:
  pattern:
    dateformat: MM-dd HH:mm:ss:SSS
spring:
  rabbitmq:
    host: 192.168.174.129 # rabbitMQ的ip地址
    port: 5672 # 端口
    username: itcast
    password: 123321
    virtual-host: /
    listener:
      simple:
        prefetch: 1 # 每次只能获取一条消息,处理完成才能获取下一个消息

结果,如图:

相关推荐
weisian15111 小时前
Java并发编程--45-分布式一致性协议入门:Raft、Paxos与ZAB的核心思想
java·分布式·raft·paxos·zab
juniperhan13 小时前
Flink 系列第17篇:Flink Table&SQL 核心概念、原理与实战详解
大数据·数据仓库·分布式·sql·flink
卢傢蕊14 小时前
FastDFS 分布式存储
分布式·fastdfs
菜鸟小码15 小时前
Hadoop大数据时代的底座和基石
大数据·hadoop·分布式
珠海西格电力16 小时前
零碳园区管理系统如何守护能源与数据安全?
大数据·人工智能·分布式·架构·能源
weisian15117 小时前
Java并发编程--44-分布式限流:令牌桶与漏桶算法在网关层的落地
java·分布式·令牌桶算法·漏桶算法·固定窗口算法·滑动窗口算法
蒋胜山19 小时前
Word 练习题(3)
word
想你依然心痛21 小时前
HarmonyOS 6(API 23)分布式实战:基于悬浮导航与沉浸光感的“光影协创“跨设备白板系统
分布式·wpf·harmonyos·悬浮导航·沉浸光感
立莹Sir1 天前
商品中台架构设计与技术落地实践——基于Spring Cloud微服务体系的完整解决方案
分布式·后端·spring cloud·docker·容器·架构·kubernetes
人道领域1 天前
【Redis实战篇】初步基于Redis实现的分布式锁---基于黑马点评
java·数据库·redis·分布式·缓存