1.maven中引入rabbitmq的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.application.yml中进行rabbitmq相关配置:
# rabbitmq空一格写,内嵌在spring里边
rabbitmq:
host: 192.168.56.10
port: 5672
virtual-host: /
#发送端确认机制 correlated:发布消息成功到交换器后会触发回调方法
publisher-confirm-type: correlated
template:
# 只要抵达队列,以异步发送优先回调以异步发送优先回调我们这个returnconfirm
mandatory: true
# 开启发送消息抵达队列的确认
publisher-returns: true
在项目启动类中添加开启rabbitmq的注解@EnableRabbit
3.创建交换机,队列,并将队列绑定到指定交换机:
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class MyMQConfig {
@Bean
public Queue orderDelayQueue(){
/* * 构造一个死信队列
* x-dead-letter-exchange: order-event-exchange
* x-dead-letter-routing-key: order.release.order
* x-message-ttl: 60000
* @create 2025/1/1
**/
Map<String,Object>arguments=new HashMap<>();
arguments.put("x-dead-letter-exchange","order-event-exchange");
arguments.put("x-dead-letter-routing-key","order.release.order");
arguments.put("x-message-ttl",60000);
Queue queue = new Queue("order.delay.queue", true, false, false,arguments);
return queue;
}
@Bean
public Queue orderReleaseOrderQueue(){
Queue queue = new Queue("order.release.order.queue", true, false, false);
return queue;
}
@Bean
public Exchange orderEventExchange(){
return new TopicExchange("order-event-exchange",true,false);
}
@Bean
public Binding orderCreateOrderBinding(){
return new Binding("order.delay.queue", Binding.DestinationType.QUEUE,"order-event-exchange","order.create.order",null);
}
@Bean
public Binding orderReleaseOrderBinding(){
return new Binding("order.release.order.queue", Binding.DestinationType.QUEUE,"order-event-exchange","order.release.order",null);
}
}
在上面的代码中,创建了2个队列:order.delay.queue和order.release.order.queue,其中order.delay.queue是延时队列,为了方便演示,将延时时间设置为1分钟(60000ms)。order.release.order.queue是普通队列。创建了1个交换机:order-event-exchange。order.delay.queue队列通过路由键order.create.order绑定到order-event-exchange交换机,order.release.order.queue队列通过路由键order.release.order也绑定到order-event-exchange交换机。在交换机的选择上,考虑到要绑定到不同的队列和路由键,支持模糊匹配,这里使用Topic交换机。
4.在MyMQConfig 创建一个方法监听队列:
@RabbitListener(queues = "order.release.order.queue")
public void listener(OrderEntity order, Channel channel, Message message) throws IOException {
System.out.println("收到过期的订单信息,准备关闭订单"+order.getOrderSn());
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
}
添加上面的方法后,当我们的服务连上rabbitmq,rabbitmq就会创建上面@Bean标注的交换机和队列了。
5.写一个创建订单的接口,简单模拟下这个过程:
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/test/createOrder")
@ResponseBody
public String creatOrder(){
OrderEntity order=new OrderEntity();
order.setOrderSn(UUID.randomUUID().toString());
order.setCreateTime(new Date());
rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",order);
return "ok";
}
使用rabbitTemplate将订单数据通过交换机order-event-exchange和路由键order.create.order将订单数据order发送给队列order.delay.queue。启动服务调用接口,来观察rabbitmq后台队列的变化:
可以看到 order.delay.queue队列中有1条消息等待消费,由于rabbitmq后台每隔5秒刷新一次,过1分钟再去看,队列中已有消息了。