概念
工作队列模式(Work Queue)是 RabbitMQ 中常见的一种消息传递模式,也称为任务队列模式。在这种模式中,多个消费者共享一个队列来处理消息,每条消息只有一个消费者可以接收,但是消息可以被多个消费者中的一个处理,以实现任务的分发和负载均衡。
工作流程
-
生产者发送消息: 生产者将消息发送到一个队列,而不是直接发送给消费者。这样做是为了解耦生产者和消费者,使得生产者不需要知道消息是由哪个消费者处理的。
-
多个消费者监听队列: 多个消费者监听同一个队列,当队列中有消息时,RabbitMQ 将消息分发给其中一个消费者。
-
消息处理: 消费者接收到消息后,进行相应的处理。处理的时间可能会有所不同,一些消息可能需要更多的时间来处理,而另一些消息可能处理得很快。
-
负载均衡: RabbitMQ 会尽可能地平均地将消息分配给各个消费者,从而实现负载均衡。当有多个消费者时,消息将按照轮询的方式分发给它们,以确保每个消费者都能处理大致相同数量的消息。
优点
- 负载均衡:多个消费者共享一个队列,可以均衡地分配消息处理的负载。
- 并行处理:可以通过增加消费者来提高消息处理的并行性,从而提高整体处理能力。
- 消息持久化:可以将消息持久化到队列中,确保即使消费者宕机,消息也不会丢失。
工作队列模式适用于需要对消息进行分发和并行处理的场景,例如任务队列、后台处理等。
Springboot集成
1.配置队列名称
2.创建队列
在RabbitmqConfig配置文件中注入到spring容器中,前面是用的@Queue注解创建
package com.model.config;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author: Haiven
* @Time: 2024/4/18 17:28
* @Description: TODO
*/
@Configuration
public class RabbitmqConfig {
@Value("${rabbitmq.work.queue}")
private String workQueue;
@Bean(name = "workQueue")
public Queue getWorkQueue(){
return QueueBuilder.durable(workQueue).build();
}
}
设置消费者
WorkConsumer
package com.model.listener;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @Author: Haiven
* @Time: 2024/4/19 11:51
* @Description: TODO
*/
@Component
public class WorkConsumer {
@RabbitListener(queues = {"work"})
public void consumer01(String msg){
System.out.println("消费者 -01- 接收消息:" + msg );
}
@RabbitListener(queues = {"work"})
public void consumer02(String msg){
System.out.println("消费者 -02- 接收消息:" + msg );
}
}
这里一个队列可以被两个消费者绑定,所以创建两个消费者;
@RabbitListener用在方法上,表示该方法作为监听队列的回调方法;
名为work的队列,在RabbitmqConfig配置文件中已经创建
编写生产者接口
package com.model.controller;
import com.code.domain.Response;
import com.model.service.RabbitService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @Author: Haiven
* @Time: 2024/4/19 9:46
* @Description: TODO
*/
@RestController
@RequestMapping("/producer")
public class ProducerController {
@Resource
private RabbitService rabbitService;
@GetMapping("/simple")
public Response<Void> simple(String msg){
boolean res = rabbitService.simple(msg);
return res ? Response.success() : Response.fail();
}
@GetMapping("/work")
public Response<Void> work(String msg){
boolean res = rabbitService.work(msg);
return res ? Response.success() : Response.fail();
}
}
package com.model.service.impl;
import com.model.service.RabbitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @Author: Haiven
* @Time: 2024/4/19 10:51
* @Description: TODO
*/
@Service
@Slf4j
public class RabbitServiceImpl implements RabbitService {
@Resource
private RabbitTemplate rabbitTemplate;
@Value("${rabbitmq.simple.queue}")
private String simpleQueue;
@Value("${rabbitmq.work.queue}")
private String workQueue;
@Override
public boolean simple(String msg) {
try {
rabbitTemplate.convertAndSend(simpleQueue, msg);
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
@Override
public boolean work(String msg) {
try {
rabbitTemplate.convertAndSend(workQueue, msg);
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
}
调用接口,模拟生产者发送6条消息
发送完毕
消息是以轮训的方式分发到两个消费者中,且每个消息只会被消费一次