springboot使用RabbitMQ的fanout廣播模式消費者死活接收不到訊息

蛙仔.發表於2020-12-02
  • 1.application.yml檔案
server:
  port: 8002
spring:
  application:
    name: producer
  rabbitmq:
    host: 192.168.1.5
    port: 5672
    virtual-host: rabbitmq-test
    username: hang_yang
    password: hang_yang
  • 2.生產者
@Test
	public void sendMessageFanout(){
		rabbitTemplate.convertAndSend("fanout","","這是一條fanout廣播訊息");
	}
  • 3.配置類
 @Configuration
public class RabbitConfig {
    /**
     * fanout廣播模型配置
     */
    //1.配置交換機
    @Bean
    public FanoutExchange fanoutExchange(){
//        FanoutExchange fanoutExchange = new FanoutExchange("fanout");
        return new FanoutExchange("fanout");
    }

    //2.配置佇列
    @Bean
    public Queue fanoutQueue1() {
        return new Queue("fanoutQueue1");
    }

    @Bean
    public Queue fanoutQueue2() {
        return new Queue("fanoutQueue2");
    }

    //3.佇列繫結交換機
    @Bean
    public Binding bindFanout1(){
//        Binding binding = BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());
        return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());
    }

    @Bean
    public Binding bindFanout2(){
//        Binding binding = BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());
        return BindingBuilder.bind(fanoutQueue2()).to(fanoutExchange());
    }
}
  • 4.消費者
	@Component
public class FanouConsumer {

    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = "fanoutQueue1"),exchange = @Exchange(name = "fanout",type = "fanout"))
    })
    public void getMessage1(String body){
        System.out.println("消費者[1]接收到訊息:" + body);

    }

    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = "fanoutQueue2"),exchange = @Exchange(name = "fanout",type = "fanout"))
    })
    public void getMessage2(String body){
        System.out.println("消費者[3]接收到訊息:" + body);
    }

}
  • 5.RabbitMQ交換機詳情
  • 在這裡插入圖片描述
    消費者一直接收不到生產者的訊息,各位大佬有沒有幫忙解決一下,萬分感謝!

相關文章