【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 # 每次只能获取一条消息,处理完成才能获取下一个消息

结果,如图:

相关推荐
xy34533 小时前
word技能--中文word文档通用格式标准化
word
扶风ff3 小时前
练题簿在线免费刷题:题目导入、章节管理、Excel 与 Word 导出,让题库更好用
学习·小程序·word·excel
yivifu3 小时前
在Word中快速实现同类内容应用“双行合一”格式
word
开开心心就好6 小时前
视频压缩工具推荐,画质效果很能打
智能手机·ffmpeg·ocr·word·排序算法·音视频·散列表
老陈说编程6 小时前
1. 鸿蒙 (HarmonyOS) 2012 至 2026 年的发展历程
分布式·华为·个人开发·harmonyos·鸿蒙·鸿蒙系统·程序员创富
xxwl5851 天前
RabbitMQ 学习笔记
笔记·学习·rabbitmq
ly76892 天前
Redis 分布式锁的边界条件:Redlock 争议、锁续期与客户端崩溃后的互斥失效
数据库·redis·分布式·分布式锁·watchdog·redlock
灯澜忆梦2 天前
【minio】#5 | MinIO 分布式部署 + HTTPS 部署
分布式·网络协议·https·对象存储·minio
谢亮_vipxieliang3 天前
Spring Cloud 服务治理入门:注册发现、配置中心、网关限流、熔断降级与分布式一致性方案
分布式·spring·spring cloud