【问题标题】:How to set Binders for Spring Cloud Stream Bindings on different RabbitMQ vhosts如何在不同的 RabbitMQ 虚拟主机上为 Spring Cloud Stream Bindings 设置 Binder
【发布时间】:2019-06-05 14:34:34
【问题描述】:

我正在尝试使用 Spring Cloud Stream 支持设置 RabbitMQ

我有几个消费者和生产者。其中一个生产者应该向同一 RabbitMQ 实例上的单独虚拟主机生成消息(以后可能是不同的物理实例)。

application.yaml

spring:
  cloud:
    stream:
      binders:
        binder1:
          type: rabbit
            defaultCandidate: false
            inheritEnvironment: false
          environment:
            spring:
              rabbitmq:
                host: localhost
                port: 5672
                virtual-host: virtual-host-1
                username: guest
                password: guest
        binder2:
          type: rabbit
            defaultCandidate: false
            inheritEnvironment: false
          environment:
            spring:
              rabbitmq:
                host: localhost
                port: 5672
                virtual-host: virtual-host-2
                username: guest
                password: guest
      bindings:
        test:
          binder: binder1
        coordinates:
          destination: coordinates
          binder: binder1
        events:
          destination: events
          binder: binder1
        events_output:
          destination: events
          binder: binder1
        tasks:
          destination: tasks
          binder: binder2

目标是绑定tasks 应该使用vhost virtual-host-2。其他绑定应使用 vhost virtual-host-1

但是binder 的值似乎被忽略了,默认的rabbit binder 在应用程序启动时被默认设置考虑在内。

我在调试运行时时注意到了这一点:

每个绑定上的 binder 值为 NULL。尽管该值在属性中明确提供。

如果我将任何活页夹的defaultCandidate 设置为true,则该活页夹设置将用作默认设置的替代。

是否配置错误?

【问题讨论】:

    标签: java spring rabbitmq spring-cloud-stream spring-rabbit


    【解决方案1】:

    这也是我不喜欢 yaml 的原因之一。很难跟踪可能配置错误的内容。无论如何,这是我刚刚尝试过的工作示例。

    spring:
      cloud:
        stream:
          bindings:
            input:
              binder: rabbit1
              group: vhost1-group
              destination: vhost1-queue
            output:
              binder: rabbit2
              destination: vhost2-queue
          binders:
            rabbit1:
              type: rabbit
              environment:
                spring:
                  rabbitmq:
                    host: localhost
                    virtual-host: vhost1
            rabbit2:
              type: rabbit
              environment:
                spring:
                  rabbitmq:
                    host: localhost
                    virtual-host: vhost2
    

    【讨论】:

      【解决方案2】:

      我刚刚复制/粘贴了您的配置;修复了一些缩进,对我来说效果很好......

      spring:
        cloud:
          stream:
            binders:
              binder1:
                type: rabbit
                defaultCandidate: false
                inheritEnvironment: false
                environment:
                  spring:
                    rabbitmq:
                      host: localhost
                      port: 5672
                      virtual-host: virtual-host-1
                      username: guest
                      password: guest
              binder2:
                type: rabbit
                defaultCandidate: false
                inheritEnvironment: false
                environment:
                  spring:
                    rabbitmq:
                      host: localhost
                      port: 5672
                      virtual-host: virtual-host-2
                      username: guest
                      password: guest
            bindings:
              test:
                binder: binder1
              tasks:
                destination: tasks
                binder: binder2
      
      @SpringBootApplication
      @EnableBinding(Foo.class)
      public class So56462671Application {
      
          public static void main(String[] args) {
              SpringApplication.run(So56462671Application.class, args);
          }
      
      }
      
      interface Foo {
      
          @Input
          MessageChannel test();
      
          @Input
          MessageChannel tasks();
      
      }
      

            defaultCandidate: false
            inheritEnvironment: false
      

      缩进不正确,但出现 YAML 解析错误。

      【讨论】:

        猜你喜欢
        • 2021-10-23
        • 2010-09-21
        • 2020-07-17
        • 2019-04-19
        • 1970-01-01
        • 2018-01-24
        • 2020-07-29
        • 1970-01-01
        • 2018-12-16
        相关资源
        最近更新 更多