rabbitmq绑定不适用于spring-boot

时间:2019-03-05 07:46:17

标签: spring-boot rabbitmq spring-rabbitmq

使用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("*");
    }

}

罐子加注后,在RabbitMQ managementUI(15672)交换页面上没有错误消息,也没有主题交换。

但是,使用python代码,可以在Exchange detaile页面上看到主题交换显示和绑定。 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()

1 个答案:

答案 0 :(得分:1)

我只是复制了您的代码,所以效果很好。

注意在打开连接之前,不会声明队列/绑定,例如通过从队列中读取消息的侦听器容器(或使用[HttpPost] public JsonResult GetAllByName([FromBody]string Input) { JsonResult result = new JsonResult(null); result = this.Json(new { list = accountCodeData.GetAllByName(Input), }); return result; } 发送消息)

RabbitTemplate

容器必须具有@RabbitListener(queues = "bbb") public void listen(String in) { System.out.println(in); } (默认值)。