一对一视频app开发,RabbitMQ数据隔离详解
一、自动创建影子队列
因为SpringAMQP中的
中的关键方法是私有的,无法通过继承的方式进行实现对以配置好的队列进行扩展,所以需要自定义该类,来实现对自动创建影子队列,并和交换器进行绑定
代码实现
改造RabbitListenerAnnotationBeanPostProcessor类来实现创建MQ影子队列以及将影子Key绑定到影子队列。
public class WormholeRabbitListenerAnnotationBeanPostProcessor extends RabbitListenerAnnotationBeanPostProcessor { @Resource private WormholeIsolationConfiguration wormholeIsolationConfiguration; /** * routingKey 前置处理器 * * @param queueName * @param routingKey * @return */ @Override public String preProcessingRoutingKey(String queueName, String routingKey) { //如果是影子队列就将routingKey转换为 影子routingKey if (wormholeIsolationConfiguration.checkIsolation(queueName) && !wormholeIsolationConfiguration.checkIsolation(routingKey)) { return wormholeIsolationConfiguration.generateIsolationKey(routingKey); } return routingKey; } /** * 处理队列问题,如果来了一个队列就生成一个shadow的队列 * * @param queues * @return */ @Override public List<String> handelQueues(List<String> queues) { List<String> isolationQueues = new ArrayList<>(); if (null != queues && !queues.isEmpty()) { for (String queue : queues) { //添加shadow队列 isolationQueues.add(wormholeIsolationConfiguration.generateIsolationKey(queue)); } queues.addAll(isolationQueues); } return queues; } }
二、传递染色标识
因为MQ是异步通讯,为了传递染色标识,会在发送MQ的时候将染色标识传递过来,MQ接收到之后放进一对一视频app开发的当前线程的ThreadLocal里面,这个需要扩展Spring的SimpleRabbitListenerContainerFactory来实现
代码实现
public class WormholeSimpleRabbitListenerContainerFactory extends SimpleRabbitListenerContainerFactory { @Override protected SimpleMessageListenerContainer createContainerInstance() { SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer(); simpleMessageListenerContainer.setAfterReceivePostProcessors(message -> { //防止线程复用 销毁ThreadLocal WormholeContextHolder.invalidContext(); //获取消息属性标识 String wormholeRequestContext = message.getMessageProperties().getHeader(WormholeContextHolder.WORMHOLE_REQUEST_MARK); if (StringUtils.isNotEmpty(wormholeRequestContext)) { WormholeContextHolder.setContext(wormholeRequestContext); } return message; }); return simpleMessageListenerContainer; } }
三、发送MQ消息处理
同上,需要传递染色标识,就通过继承RabbitTemplate重写convertAndSend方法来实现传递染色标识。
public class ShadowRabbitTemplate extends RabbitTemplate { public ShadowRabbitTemplate(ConnectionFactory connectionFactory) { super(connectionFactory); } @Autowired private WormholeIsolationConfiguration isolationConfiguration; @Override public void send(final String exchange, final String routingKey, final Message message, @Nullable final CorrelationData correlationData) throws AmqpException { WormholeContext wormholeContext = WormholeContextHolder.getContext(); if (null == wormholeContext) { super.send(exchange, routingKey, message, correlationData); } else { message.getMessageProperties().setHeader(WormholeContextHolder.WORMHOLE_REQUEST_MARK, wormholeContext.toString()); //生成Rabbit 隔离Key String wormholeRoutingKey = isolationConfiguration.generateIsolationKey(routingKey); //调用父类进行发送 super.send(exchange, wormholeRoutingKey, message, correlationData); } } }
以上就是一对一视频app开发,RabbitMQ数据隔离步骤详解, 更多内容欢迎关注之后的文章
标签:return,routingKey,队列,app,RabbitMQ,public,详解,queues,String From: https://www.cnblogs.com/yunbaomengnan/p/18199025