• Rabbitmq入门与应用(四)-RabbitMQ常见模式


    RabbitMQ常见Queue模式

    image-20230722102545586 image-20230722102605912 image-20230722102641436

    简单模式

    点对点模式,一个生产者一个消费者

    生产者将消息发送到队列,消费者从队列中获取消息,队列是存储消息的缓冲区

    image-20230617192522553

    查看管理端效果
    序列化解决方案
    • 基于java序列化
    • 基于Json
    @Bean
    public MessageConverter messageConverter() {
    	return new Jackson2JsonMessageConverter();
    }
    
    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(factory);
        rabbitTemplate.setMessageConverter(messageConverter());
        :
        :
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    基于json

    image-20230722180149630

    Work queues

    工作队列模式:

    一个生产者,多个消费者

    • C1,C2 是否都收到
    • 假设只有一个收到,谁收到(先)

    image-20230617192808675 image-20231031093805938

    结果: 只有一个消费者消费,轮训(负载均衡)的方式进行消费

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    @Bean
    public Queue workQueue() {
        return new Queue(RabbitMQConstants.WORK_QUEUE, DURABLE);
    }
    
    • 1
    • 2
    • 3
    • 4
    image-20230725100649432

    发布订阅/fanout模式

    生产者通过fanout扇出交换机群发消息给消费者,同一条消息每一个消费者都可以收到。

    image-20230617193442071

    @Bean
    public Binding bindingFanoutA(){
        return BindingBuilder
                .bind(fanoutQueueA())
                .to(fanoutExchange());
    }
    
    /**
    
     * Fanout交换机和队列B绑定
     * @return
       */
       @Bean
       public Binding bindingFanoutB(){
       return BindingBuilder
               .bind(fanoutQueueB())
               .to(fanoutExchange());
       }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    image-20230725100900868

    routing模式

    image-20230725103347564
    配置
        @Bean
        public Queue routingYellowQueue(){
            return new Queue(RabbitMQConstants.ROUTING_YELLOW_QUEUE,DURABLE);
        }
        @Bean
        public Queue routingBlueQueue(){
            return new Queue(RabbitMQConstants.ROUTING_BLUE_QUEUE,DURABLE);
        }
    
        @Bean
        public DirectExchange routingExchange(){
            return new DirectExchange(RabbitMQConstants.ROUTING_EXCHANGE,DURABLE,AUTO_DELETE);
        }
    
    
        @Bean
        public Binding routingYellowBinding(){
            return BindingBuilder
                    .bind(routingYellowQueue())
                    .to(routingExchange())
                    .with(RabbitMQConstants.ROUTING_YELLOW_KEY);
        }
    
        @Bean
        public Binding routingBlueBinding(){
            return BindingBuilder
                    .bind(routingBlueQueue())
                    .to(routingExchange())
                    .with(RabbitMQConstants.ROUTING_BLUE_KEY);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    生产者
      @Override
        public void routingOrder() {
    
            Order order= new Order();
            order.setBook(41L);
            order.setStatus("未支付");
            order.setTotal(5);
            order.setTime(new Date());
            
            rabbitTemplate.convertAndSend(
                    RabbitMQConstants.ROUTING_EXCHANGE,     
                    RabbitMQConstants.ROUTING_YELLOW_KEY,  //routing到yellow线路
                    order);
            log.debug("[routing工作模式: ] 产生一个订单-->{}",order);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    topic模式

    image-20231031121640085

    * 任意个任意一字符串

    # 0或一个任意字符串

    cba.topic.abc

    topic.* —只有一个字符串

    • topic.orange

    topic.# ----> 0个字符串或者多个字符串(用户点号分开的)

    • topic
    • topic.abc
    • topic.abc.bananaimage-20230725140907058
    image-20230725140727896

    image-20231218145310089

        @Bean
        public Binding topicOrangeBinding(){
            return BindingBuilder
                    .bind(topicOrangeQueue())
                    .to(topicExchange())
                    .with("*.fruit.*");  //*.fruit.*
        }
    
        @Bean
        public Binding topicBananaBinding(){
            return BindingBuilder
                    .bind(topicBananaQueue())
                    .to(topicExchange())
                    .with("#.fruit.*");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    Spring Cloud Alibaba(四)
    基于开源模型搭建实时人脸识别系统(六):人脸识别(人脸特征提取)
    视频截取gif动画怎么操作?轻松一键快速视频转gif
    【HCIE】04.网络安全技术
    JVM面试核心点
    linux 实时调度实现
    flip-flop with VHDL (dataflow, structure, behavior)
    前端list列表自定义图标并设置大小
    spring boot集成pg
    软件工程第三周
  • 原文地址:https://blog.csdn.net/qq_36115196/article/details/136167387