队列配置及测试
This commit is contained in:
parent
9816b81bd3
commit
f961aa5c46
@ -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
|
||||||
|
* <p>
|
||||||
|
* 即进入死信队列的路由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<String, Object> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -53,3 +53,24 @@ logging:
|
|||||||
# xdclass: 134.175.219.253:8091
|
# xdclass: 134.175.219.253:8091
|
||||||
# vgroup-mapping:
|
# vgroup-mapping:
|
||||||
# xdclass-coupon-service-group: xdclass
|
# 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
|
@ -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 ");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user