You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Cloud Stream RabbitMQ交换器自动绑定失败问题求助

Spring Cloud Stream整合RabbitMQ:交换器重复创建且无绑定问题排查

当前学习Spring Cloud Stream整合RabbitMQ时遇到问题:存在两个服务ServiceA(生产者)和ServiceB(消费者),但运行后生成了两个独立的交换器,其中生产者指定的"new-price"交换器无绑定关系,需要手动绑定。以下是服务配置及代码,附解决线索:

生产者ServiceA

配置文件

spring:
    cloud:
        stream:
            default-binder: rabbit
            bindings:
                new-price-out:
                    destination: "new-price"
                    content-type: "application/json"
            rabbit:
                binder:
                    host: "localhost"
                    port: 5672
                    username: "guest"
                    password: "guest"

代码实现

@Component
public class RabbitMqAdapter implements MessageQueuePort {

    private final StreamBridge streamBridge;
    private final String CREATE_PRICE_BINDING = "new-price-out";

    public RabbitMqAdapter(StreamBridge streamBridge){
        this.streamBridge = streamBridge;
    }

    @Override
    public void sendPriceCreationMessage(QuickPriceAnalysisRequest request) {
        streamBridge.send(CREATE_PRICE_BINDING, request);
    }
}

消费者ServiceB

配置文件

spring:
  cloud:
    stream:
      default-binder: rabbit
      bindings:
        new-price-in:
          destination: "new-price" # 与生产者使用相同交换器名称
          content-type: "application/json"
          group: price-consumers
      rabbit:
        bindings:
          consumer:
            declareExchange: false
        binder:
            host: "localhost"
            port: 5672
            username: "guest"
            password: "guest"

代码实现

@Component
@RequiredArgsConstructor
public class RabbitInAdapter implements PriceCreationEventPort{

    private final NewPriceUseCase newPriceUseCase;

    @Bean
    @Override
    public Consumer<Message<CreatePriceCommand>> newPrice() {
        return message -> {
            CreatePriceCommand payload = message.getPayload();
        };
    }
}

解决线索

  • 修正消费者Rabbit绑定配置层级:当前消费者配置中rabbit.bindings.consumer的层级错误,未关联到具体的绑定名称new-price-in。正确配置应改为:
    rabbit:
      bindings:
        new-price-in: # 与bindings中的消费者绑定名保持一致
          consumer:
            declareExchange: false
    
    错误的层级会导致消费者无法正确关联到生产者创建的new-price交换器,进而可能创建额外的交换器。
  • 统一绑定名称与Bean注解:消费者的@Bean方法名为newPrice,Spring Cloud Stream默认会将其转为new-price作为绑定名,但配置中使用的是new-price-in,名称不匹配导致绑定失败。可以二选一修改:
    1. 将配置中的new-price-in改为new-price;
    2. 给@Bean添加@Input("new-price-in")注解,明确指定绑定名称:
      @Bean
      @Input("new-price-in")
      @Override
      public Consumer<Message<CreatePriceCommand>> newPrice() {
          // ...
      }
      
  • 检查消息类型一致性:生产者发送的是QuickPriceAnalysisRequest,消费者接收的是CreatePriceCommand,类型不匹配会导致消息无法正常路由,也可能间接表现为交换器无绑定。需确保两端消息类型一致,或配置正确的消息转换器。
  • 暂时移除declareExchange: false:在绑定配置未完全正确前,先去掉该配置,让消费者自动关联并确认交换器绑定情况,排查完成后再根据需求添加。

内容的提问来源于stack exchange,提问作者adi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 20:55:20