乞丐版rabbitmq

1.总结:

生产者只指定了exchange, routekey, 没指定消费者。

2个消费者分别用exchange, routekey绑定自己定义的不同的queue

结果两个消费者都能一模一样的消费到message

 

2.工程结构:

生产者

 

 

 消费者1

 

 

 消费者2

 

 

 

3.代码:

生产者:

@RestController
public class Producer1 {

    @Autowired
    private RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        //将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
        return "ok";
    }
}

消费者1:

@Configuration
public class DirectRabbitConfig {

    //队列 起名:TestDirectQueue
    @Bean
    public Queue TestDirectQueue() {
        return new Queue("TestDirectQueue1", true);
    }

    //Direct交换机 起名:TestDirectExchange
    @Bean
    DirectExchange TestDirectExchange() {
        return new DirectExchange("TestDirectExchange", true, false);
    }

    //绑定  将队列和交换机绑定, 并设置用于匹配键:TestDirectRouting
    @Bean
    Binding bindingDirect() {
        return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting");
    }

    @Bean
    DirectExchange lonelyDirectExchange() {
        return new DirectExchange("lonelyDirectExchange");
    }
}
@Component
@RabbitListener(queues = "TestDirectQueue1")//监听的队列名称 TestDirectQueue
public class Consumer1 {
    @RabbitHandler
    public void process(Map testMessage) {
        System.out.println("consumer1  : " + testMessage.toString());
    }
}

消费者2:

@Configuration
public class DirectRabbitConfig2 {

    //队列 起名:TestDirectQueue
    @Bean
    public Queue TestDirectQueue() {
        return new Queue("TestDirectQueue2", true);
    }

    //Direct交换机 起名:TestDirectExchange
    @Bean
    DirectExchange TestDirectExchange() {
        return new DirectExchange("TestDirectExchange", true, false);
    }

    //绑定  将队列和交换机绑定, 并设置用于匹配键:TestDirectRouting
    @Bean
    Binding bindingDirect() {
        return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting");
    }

    @Bean
    DirectExchange lonelyDirectExchange() {
        return new DirectExchange("lonelyDirectExchange");
    }
}
@Component
@RabbitListener(queues = "TestDirectQueue2")//监听的队列名称 TestDirectQueue
public class Consumer2 {
    @RabbitHandler
    public void process(Map testMessage) {
        System.out.println("consumer2 : " + testMessage.toString());
    }
}

 

4.测试:

生产者调用接口

 

 消费者1:

 

消费者2:

两个消费者通过绑定不同的队列都消费到全部的数据

 

 

5.上面是两个消费者都要消费到消息,以下是两个消费者轮流消费:

改动:两个消费者都绑定同一个队列

消费者2代码修改如下:

@Configuration
public class DirectRabbitConfig2 {

    //队列 起名:TestDirectQueue
    @Bean
    public Queue TestDirectQueue() {
        return new Queue("TestDirectQueue1", true);
    }

    //Direct交换机 起名:TestDirectExchange
    @Bean
    DirectExchange TestDirectExchange() {
        return new DirectExchange("TestDirectExchange", true, false);
    }

    //绑定  将队列和交换机绑定, 并设置用于匹配键:TestDirectRouting
    @Bean
    Binding bindingDirect() {
        return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting");
    }

    @Bean
    DirectExchange lonelyDirectExchange() {
        return new DirectExchange("lonelyDirectExchange");
    }
}
@Component
@RabbitListener(queues = "TestDirectQueue1")//监听的队列名称 TestDirectQueue
public class Consumer2 {
    @RabbitHandler
    public void process(Map testMessage) {
        System.out.println("consumer2 : " + testMessage.toString());
    }
}

测试:

生产者:

 

 消费者1:

消费者2:

生产者再发一次消息:

 消费者1:

 

消费者2:

 

由此可见多个消费者绑定相同的queue就会轮流消费。 

 

posted @ 2021-09-28 00:20  圣金巫灵  阅读(51)  评论(0编辑  收藏  举报