diff --git a/04-coupon/pom.xml b/04-coupon/pom.xml index 518f003..287458a 100644 --- a/04-coupon/pom.xml +++ b/04-coupon/pom.xml @@ -21,5 +21,9 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery + + org.springframework.boot + spring-boot-starter-amqp + \ No newline at end of file diff --git a/04-coupon/src/main/java/com/mashibing/config/RabbitMQConfig.java b/04-coupon/src/main/java/com/mashibing/config/RabbitMQConfig.java new file mode 100644 index 0000000..d71e9df --- /dev/null +++ b/04-coupon/src/main/java/com/mashibing/config/RabbitMQConfig.java @@ -0,0 +1,52 @@ +package com.mashibing.config; + +import org.springframework.amqp.core.*; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author zjw + * @description + */ +@Configuration +public class RabbitMQConfig { + + // 下单服务的交换机 + public static final String PLACE_ORDER_EXCHANGE = "place_order_exchange"; + // 三个服务的Queue + public static final String COUPON_QUEUE = "coupon_queue"; + public static final String USER_POINTS_QUEUE = "user_points_queue"; + public static final String BUSINESS_QUEUE = "business_queue"; + + + @Bean + public Exchange placeOrderExchange(){ + return ExchangeBuilder.fanoutExchange(PLACE_ORDER_EXCHANGE).build(); + } + + @Bean + public Queue couponQueue(){ + return QueueBuilder.durable(COUPON_QUEUE).build(); + } + @Bean + public Queue userPointsQueue(){ + return QueueBuilder.durable(USER_POINTS_QUEUE).build(); + } + @Bean + public Queue businessQueue(){ + return QueueBuilder.durable(BUSINESS_QUEUE).build(); + } + + @Bean + public Binding couponBinding(Exchange placeOrderExchange,Queue couponQueue){ + return BindingBuilder.bind(couponQueue).to(placeOrderExchange).with("").noargs(); + } + @Bean + public Binding userPointsBinding(Exchange placeOrderExchange,Queue userPointsQueue){ + return BindingBuilder.bind(userPointsQueue).to(placeOrderExchange).with("").noargs(); + } + @Bean + public Binding businessBinding(Exchange placeOrderExchange,Queue businessQueue){ + return BindingBuilder.bind(businessQueue).to(placeOrderExchange).with("").noargs(); + } +} diff --git a/04-coupon/src/main/java/com/mashibing/listener/CouponListener.java b/04-coupon/src/main/java/com/mashibing/listener/CouponListener.java new file mode 100644 index 0000000..d869065 --- /dev/null +++ b/04-coupon/src/main/java/com/mashibing/listener/CouponListener.java @@ -0,0 +1,27 @@ +package com.mashibing.listener; + +import com.mashibing.config.RabbitMQConfig; +import com.rabbitmq.client.Channel; +import org.springframework.amqp.core.Message; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +/** + * @author zjw + * @description + */ +@Component +public class CouponListener { + + @RabbitListener(queues = {RabbitMQConfig.COUPON_QUEUE}) + public void consume(String msg, Channel channel, Message message) throws InterruptedException, IOException { + // 预扣除优惠券 + Thread.sleep(400); + System.out.println("优惠券预扣除成功!" + msg); + // 手动ACK + channel.basicAck(message.getMessageProperties().getDeliveryTag(),false); + } + +} diff --git a/04-coupon/src/main/resources/application.yml b/04-coupon/src/main/resources/application.yml index 4088ece..45ca452 100644 --- a/04-coupon/src/main/resources/application.yml +++ b/04-coupon/src/main/resources/application.yml @@ -7,4 +7,13 @@ spring: cloud: nacos: discovery: - server-addr: 114.116.226.76:8848 \ No newline at end of file + server-addr: 114.116.226.76:8848 + rabbitmq: + host: 114.116.226.76 + port: 5672 + username: rabbitmq + password: rabbitmq + virtual-host: rabbitmq + listener: + simple: + acknowledge-mode: manual \ No newline at end of file