添加依赖
<!--消息发送-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置
-
spring: rabbitmq: host: 192.168.150.101 # 你的虚拟机IP port: 5672 # 端口 virtual-host: /hmall # 虚拟主机 username: hmall # 用户名 password: 123 # 密码
发送者
注入
private final RabbitTemplate rabbitTemplate;
try {
rabbitTemplate.convertAndSend(
"pay.topic" //交换机
, "pay.success" //key
, po.getBizOrderNo()); //消息体
} catch (Exception e) {
log.error("支付成功的消息发送失败,支付单id:{}, 交易单id:{}", po.getId(), po.getBizOrderNo(), e);
}
消费者
-
@Component @RequiredArgsConstructor public class PayStatusListener { private final IOrderService orderService; @RabbitListener(bindings = @QueueBinding( value = @Queue(name = "mark.order.pay.queue", durable = "true"), exchange = @Exchange(name = "pay.topic", type = ExchangeTypes.TOPIC), key = "pay.success" )) public void listenPaySuccess(Long orderId){ orderService.markOrderPaySuccess(orderId); } }