From f961aa5c46a7d98748191ba6951b11933f64a294 Mon Sep 17 00:00:00 2001 From: jieyuu <645634619@qq.com> Date: Tue, 27 Aug 2024 21:39:29 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=9F=E5=88=97=E9=85=8D=E7=BD=AE=E5=8F=8A?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/jieyuu/config/RabbitMQConfig.java | 129 ++++++++++++++++++ .../src/main/resources/application.yml | 23 +++- .../net/jieyuu/biz/DemoApplicationTests.java | 26 ++++ 3 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 xdclass-coupon-service/src/main/java/net/jieyuu/config/RabbitMQConfig.java create mode 100644 xdclass-coupon-service/src/test/java/net/jieyuu/biz/DemoApplicationTests.java diff --git a/xdclass-coupon-service/src/main/java/net/jieyuu/config/RabbitMQConfig.java b/xdclass-coupon-service/src/main/java/net/jieyuu/config/RabbitMQConfig.java new file mode 100644 index 0000000..c6622ea --- /dev/null +++ b/xdclass-coupon-service/src/main/java/net/jieyuu/config/RabbitMQConfig.java @@ -0,0 +1,129 @@ +package net.jieyuu.config; + + +import lombok.Data; +import org.springframework.amqp.core.Binding; +import org.springframework.amqp.core.Exchange; +import org.springframework.amqp.core.Queue; +import org.springframework.amqp.core.TopicExchange; +import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; +import org.springframework.amqp.support.converter.MessageConverter; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.HashMap; +import java.util.Map; + +@Configuration +@Data +public class RabbitMQConfig { + + /** + * 交换机 + */ + @Value("${mqconfig.coupon_event_exchange}") + private String eventExchange; + + + /** + * 第一个队列延迟队列 + */ + @Value("${mqconfig.coupon_release_delay_queue}") + private String couponReleaseDelayQueue; + + /** + * 第一个队列的路由key + * 进入队列的路由key + */ + @Value("${mqconfig.coupon_release_delay_routing_key}") + private String couponReleaseDelayRoutingKey; + + + /** + * 第二个队列,被监听恢复库存的队列 + */ + @Value("${mqconfig.coupon_release_queue}") + private String couponReleaseQueue; + + /** + * 第二个队列的路由key + *

+ * 即进入死信队列的路由key + */ + @Value("${mqconfig.coupon_release_routing_key}") + private String couponReleaseRoutingKey; + + /** + * 过期时间 + */ + @Value("${mqconfig.ttl}") + private Integer ttl; + + /** + * 消息转换器 + * + * @return + */ + @Bean + public MessageConverter messageConverter() { + return new Jackson2JsonMessageConverter(); + } + + /** + * 自动创建交换机 Topic类型 也可dirct交换机 + * 一个微服务一个交换机 + * + * @return + */ + @Bean + public Exchange couponEventExchange() { + return new TopicExchange(eventExchange, true, false); + } + + /** + * 延迟队列 + * + * @return + */ + @Bean + public Queue couponReleaseDelayQueue() { + Map args = new HashMap<>(3); + args.put("x-message-ttl", ttl); + args.put("x-dead-letter-routing-key", couponReleaseRoutingKey); + args.put("x-dead-letter-exchange", eventExchange); + return new Queue(couponReleaseDelayQueue, true, false, false, args); + } + + /** + * 死信队列 普通队列,用于被监听 + * + * @return + */ + @Bean + public Queue couponReleaseQueue() { + return new Queue(couponReleaseQueue, true, false, false); + } + + /** + * 延迟队列建立绑定关系 + * + * @return + */ + @Bean + public Binding couponReleaseDelayBinding() { + return new Binding(couponReleaseDelayQueue, Binding.DestinationType.QUEUE, eventExchange, couponReleaseDelayRoutingKey, null); + } + + /** + * 死信队列建立绑定关系 + * + * @return + */ + @Bean + public Binding couponReleaseBinding() { + return new Binding(couponReleaseQueue, Binding.DestinationType.QUEUE, eventExchange, couponReleaseRoutingKey, null); + } + + +} diff --git a/xdclass-coupon-service/src/main/resources/application.yml b/xdclass-coupon-service/src/main/resources/application.yml index aba515e..7722e08 100644 --- a/xdclass-coupon-service/src/main/resources/application.yml +++ b/xdclass-coupon-service/src/main/resources/application.yml @@ -52,4 +52,25 @@ logging: # grouplist: # xdclass: 134.175.219.253:8091 # vgroup-mapping: -# xdclass-coupon-service-group: xdclass \ No newline at end of file +# xdclass-coupon-service-group: xdclass + + +#自定义消息队列配置,发送锁定库存消息->延迟exchange->lock.queue->死信exchange->release.queue +mqconfig: + #延迟队列,不能被监听消费 + coupon_release_delay_queue: coupon.release.delay.queue + + #延迟队列的消息过期后转发的队列 + coupon_release_queue: coupon.release.queue + + #交换机 + coupon_event_exchange: coupon.event.exchange + + #进入延迟队列的路由key + coupon_release_delay_routing_key: coupon.release.delay.routing.key + + #消息过期,进入释放死信队列的key + coupon_release_routing_key: coupon.release.routing.key + + #消息过期时间,毫秒,测试改为15秒 + ttl: 15000 \ No newline at end of file diff --git a/xdclass-coupon-service/src/test/java/net/jieyuu/biz/DemoApplicationTests.java b/xdclass-coupon-service/src/test/java/net/jieyuu/biz/DemoApplicationTests.java new file mode 100644 index 0000000..57cd427 --- /dev/null +++ b/xdclass-coupon-service/src/test/java/net/jieyuu/biz/DemoApplicationTests.java @@ -0,0 +1,26 @@ +package net.jieyuu.biz; + +import lombok.extern.slf4j.Slf4j; +import net.jieyuu.CouponApplication; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = CouponApplication.class) +@Slf4j +public class DemoApplicationTests { + + @Autowired + private RabbitTemplate rabbitTemplate; + + @Test + public void send() { + rabbitTemplate.convertAndSend("coupon.event.exchange", "coupon.release.delay.routing.key", "this is "); + + } + +} \ No newline at end of file