从 Spring Boot 1.5.x 更新到 2.2.x RabbitMQ 不声明与列表的绑定

Update from Spring Boot 1.5.x to 2.2.x RabbitMQ doesn't declare bindings with a list

我曾经在我的一个配置文件中声明队列、交换和绑定,如下所示:

@EnableRabbit
@Configuration
public class MyRabbitConfiguration {

    @Bean
    public FanoutExchange firstExchange() {
        return new FanoutExchange("firstExchange");
    }

    @Bean
    public Queue firstQueue() {
        return new Queue("firstQueue", true, false, false);
    }

    @Bean
    public FanoutExchange secondExchange() {
        return new FanoutExchange("secondExchange");
    }

    @Bean
    public Queue secondQueue() {
        return new Queue("secondQueue", true, true, true);
    }

    @Bean
    public List<Declarable> bindings() {
        return Arrays.asList(
                BindingBuilder.bind(firstQueue()).to(firstExchange()),
                BindingBuilder.bind(secondQueue()).to(secondExchange()));
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        return rabbitTemplate;
    }
}

这曾经工作得很好。从 Spring Boot 1.5.x(显然也是初学者)更新到 Spring Boot 2.2.x 后,一切仍然有效,除了 Bindings .当我更换时:

@Bean
public List<Declarable> bindings() {
    return Arrays.asList(
            BindingBuilder.bind(firstQueue()).to(firstExchange()),
            BindingBuilder.bind(secondQueue()).to(secondExchange()));
}

与:

@Bean
public Binding firstBinding() {
    return BindingBuilder.bind(firstQueue()).to(firstExchange());
}

@Bean
public Binding secondBinding() {
    return BindingBuilder.bind(secondQueue()).to(secondExchange());
}

它开始像我预期的那样工作,我仍然有我的 Bindings。我怀疑它与 this 有关,但无法弄清楚。我做错了什么?

您需要改用 Declarables

查看文档:https://docs.spring.io/spring-amqp/docs/2.2.5.RELEASE/reference/html/#collection-declaration

注意重要区块:

In versions prior to 2.1, you could declare multiple Declarable instances by defining beans of type Collection<Declarable>. This can cause undesirable side effects in some cases, because the admin has to iterate over all Collection<?> beans. This feature is now disabled in favor of Declarables, as discussed earlier in this section. You can revert to the previous behavior by setting the RabbitAdmin property called declareCollections to true.