1. docker部署
拉取镜像:docker pull rabbitmq:3-management
启动容器:
docker run \
-e RABBITMQ_DEFAULT_USER=itcast \
-e RABBITMQ_DEFAULT_PASS=123321 \
--name mq \
--hostname mq1 \
-p 15672:15672 \
-p 5672:5672 \
-d \
rabbitmq:3-management
2. springboot添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
3. 配置文件application.yml
spring:
rabbitmq:
host: 192.168.200.128
username: root
password: 123456
virtual-host: /
port: 5672
listener:
simple:
#消费者最小数量
concurrency: 10
#消费者最大数量
max-concurrency: 10
#限制消费者每次只处理一条消息,处理完再下一跳
prefetch: 1
#启动时是否默认启动容器,默认true
auto-startup: true
#被拒绝时重新进入队列
default-requeue-rejected: true
template:
retry:
#发布重试
enabled: true
#重试时间
initial-interval: 1000ms
#最大重试数量
max-attempts: 3
#重试最大间隔时间
max-interval: 10000ms
#重试的间隙乘数,比如2时,第一次等10s,第二次20s,第三次40s
multiplier: 1
4. 添加RabbitMQConfig类配置队列
@Configuration
public class RabbitMQConfig {
@Bean
public Queue queue(){
return new Queue("queue",true);
}
}
5. 创建生产者类MQSender
@Service
@Slf4j
public class MQSender {
@Autowired
private RabbitTemplate rabbitTemplate;
public void send(Object msg){
log.info("发送消息:"+msg);
rabbitTemplate.convertAndSend("queue",msg);
}
}
6. 创建消费者类MQReveiver
@Service
@Slf4j
public class MQReceiver {
@RabbitListener(queues = "queue")
public void receive(Object msg){
log.info("接收消息:"+msg);
}
}
7. 测试
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private MQSender mqSender;
@RequestMapping("/mq")
@ResponseBody
public void mq(){
mqSender.send("hallo,mq");
}
}
8. fanout模式(广播)
①:创建队列,交换机并且绑定
@Configuration
public class RabbitMQConfig {
@Bean
public Queue queue01(){
return new Queue("queue01");
}
@Bean
public Queue queue02(){
return new Queue("queue02");
}
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange("fanout_exchange");
}
@Bean
public Binding binding01(){
return BindingBuilder.bind(queue01()).to(fanoutExchange());
}
@Bean
public Binding binding02(){
return BindingBuilder.bind(queue02()).to(fanoutExchange());
}
}
②:生产者
@Service
@Slf4j
public class MQSender {
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendfanout(Object msg){
log.info("发送消息:"+msg);
rabbitTemplate.convertAndSend("fanout_exchange","",msg);
}
}
③:消费者
@Service
@Slf4j
public class MQReceiver {
@RabbitListener(queues = "queue01")
public void receivefanout01(Object msg){
log.info("queue01接收消息:"+msg);
}
@RabbitListener(queues = "queue02")
public void receivefanout02(Object msg){
log.info("queue02接收消息:"+msg);
}
}
④:测试
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private MQSender mqSender;
@RequestMapping("/mq/fanout")
@ResponseBody
public void mqfanout(){
mqSender.sendfanout("hallo,mq");
}
}
9. direct模式
● 其会根据,RoutingKey路由键,去决定交换机把消息发送到哪个(些)队列上去;
● Producer在发送消息时,如果使用direct模式;那么,我们在发送消息的时候,就需要指定一个Routing Key(否则交换机,将不知道把消息发送到哪个(些)队列上去);
● 然后Consumer在接收消息的时候,需要创建一个队列;然后,Consumer要想使用队列接收到某个消息,那么在通过队列接受消息时,就需要设置对应的Routing Key;
①:创建队列,交换机,并且绑定
@Configuration
public class RabbitMQConfig {
@Bean
public Queue queue03(){
return new Queue("queue03");
}
@Bean
public Queue queue04(){
return new Queue("queue04");
}
@Bean
public DirectExchange directExchange(){
return new DirectExchange("direct_exchange");
}
@Bean
public Binding binding03(){
return BindingBuilder.bind(queue03()).to(directExchange()).with("routingksy01");
}
@Bean
public Binding binding04(){
return BindingBuilder.bind(queue03()).to(directExchange()).with("routingksy02");
}
}
②:生产者
@Service
@Slf4j
public class MQSender {
@Autowired
private RabbitTemplate rabbitTemplate;
public void senddirect01(Object msg){
log.info("发送red消息:"+msg);
rabbitTemplate.convertAndSend("direct_exchange","routingksy01",msg);
}
public void senddirect02(Object msg){
log.info("发送blue消息:"+msg);
rabbitTemplate.convertAndSend("direct_exchange","routingksy02",msg);
}
}
③:消费者
@Service
@Slf4j
public class MQReceiver {
@RabbitListener(queues = "queue03")
public void receivedirect01(Object msg){
log.info("queue02接收red消息:"+msg);
}
@RabbitListener(queues = "queue04")
public void receivedirect02(Object msg){
log.info("queue02接收blue消息:"+msg);
}
}
④: 测试
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private MQSender mqSender;
@RequestMapping("/mq/direct")
@ResponseBody
public void mqdirect(){
mqSender.senddirect01("这是red消息!!!");
mqSender.senddirect01("这是blue消息!!!");
}
}
10. topic模式
● Topic模式可以认为是direct模式的一个威力加强版;
● Topic之所以,可以应对“多个条件”的复杂逻辑;主要是其可以进行模糊匹配;
● 其中,*可以匹配任何一个单词(不包含"."的任意字符串);例如,item.*可以匹配item.pikapika,或者item.&gdygj。#可以匹配0至多个单词;例如:item.# 能够匹配 item.insert.abc 或者 item.insert;
● 关于,topic模式,可以看下下面的案例;
①:配置队列,交换机
@Configuration
public class RabbitMQConfig {
@Bean
public Queue queue05(){
return new Queue("topic_queue01");
}
@Bean
public Queue queue06(){
return new Queue("topic_queue02");
}
@Bean
public TopicExchange topicExchange(){
return new TopicExchange("topic_exchange");
}
@Bean
public Binding binding05(){
return BindingBuilder.bind(queue05()).to(topicExchange()).with("#.queue.#");
}
@Bean
public Binding binding06(){
return BindingBuilder.bind(queue06()).to(topicExchange()).with("*.queue.#");
}
}
②:生产者
@Service
@Slf4j
public class MQSender {
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendtopic01(Object msg){
log.info("发送queue.red消息:"+msg);
rabbitTemplate.convertAndSend("topic_exchange","queue.red",msg);
}
public void sendtopic02(Object msg){
log.info("发送blue.queue.red消息:"+msg);
rabbitTemplate.convertAndSend("topic_exchange","blue.queue.red",msg);
}
}
③: 消费者
@Service
@Slf4j
public class MQReceiver {
@RabbitListener(queues = "topic_queue01")
public void receivedtopic01(Object msg){
log.info("queue01接收red消息:"+msg);
}
@RabbitListener(queues = "topic_queue02")
public void receivetopic02(Object msg){
log.info("queue02接收blue消息:"+msg);
}
}
④: 测试
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private MQSender mqSender;
@RequestMapping("/mq/direct")
@ResponseBody
public void mqdirect(){
mqSender.senddirect01("这是red消息!!!");
mqSender.senddirect02("这是blue消息!!!");
}
@RequestMapping("/mq/topic")
@ResponseBody
public void mqtopic(){
mqSender.sendtopic01("这是queue.red消息");
mqSender.sendtopic02("这是blue.queue.red消息!!!");
}
}