rabbitmq 绑定不适用于 spring-boot

rabbitmq binding not work with spring-boot

with spring boot 1.5.9 RELEASE,代码如下

@Configuration
@EnableRabbit
public class RabbitmqConfig {
    @Autowired
    ConnectionFactory connectionFactory;

    @Bean//with or without this bean, neither works
    public AmqpAdmin amqpAdmin() {
        return new RabbitAdmin(connectionFactory);
    }

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

    @Bean
    public TopicExchange requestExchange() {
        return new TopicExchange("request");
    }

    @Bean
    public Binding bbbBinding() {
        return BindingBuilder.bind(bbbQueue())
                .to(requestExchange())
                .with("*");
    }

}

jar stars后,RabbitMQ managementUI(15672)交流页面没有报错,也没有话题交流显示

但是,使用 python 代码,主题交换显示和绑定可以在交换详细信息页面上看到。 python代码如下

 connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.189.134.47'))
 channel = connection.channel()

 channel.exchange_declare(exchange='request', exchange_type='topic', durable=True)

 result = channel.queue_declare(queue='aaa', durable=True)
 queue_name = result.method.queue

 channel.queue_bind(exchange='aaa', routing_key='*',
                           queue=queue_name)

 print(' [*] Waiting for logs. To exit press CTRL+C')

 def callback(ch, method, properties, body):
        print(" [x] %r" % body)

 channel.basic_consume(callback, queue=queue_name, no_ack=True)

 channel.start_consuming()

我刚刚复制了你的代码,它工作正常。

注意 queue/binding 在连接打开之前不会被声明,例如通过从队列中读取的侦听器容器(或使用RabbitTemplate).

@RabbitListener(queues = "bbb")
public void listen(String in) {
    System.out.println(in);
}

容器必须有autoStartup=true(默认)。