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

使用spring-cloud-stream发送RabbitMQ消息无法指定RoutingKey问题咨询

问题原因

Spring Cloud Stream Rabbit Binder 3.x版本中,bindingRoutingKey属性的作用是队列绑定到交换机时使用的路由键,仅在绑定队列时生效,和发送消息时使用的路由键没有关联。
你配置了生产者端bindQueue: false,所以bindingRoutingKey配置完全不生效;而生产者发送消息的默认路由键取值为destination配置的值,也就是你设置的command_exchange_open,符合你遇到的现象。

解决方案

方案1:固定生产者发送的路由键(适配你的固定ORDER_PUSH场景)

将生产者配置中的bindingRoutingKey替换为routingKeyExpression,该配置用来指定发送消息时使用的路由键,值为SpEL表达式,固定值需要用单引号包裹。
修改后的完整配置如下:

spring:
  rabbitmq:
    addresses: amqp://sycx:sycx@192.168.1.204
  cloud:
    stream:
      rabbit:
        bindings:
          input-in-0:
            consumer:
              bindingRoutingKey: ORDER_PUSH
              exchangeType: direct
              queueNameGroupOnly: true
          output-out-0:
            producer:
              # 替换原有bindingRoutingKey配置,指定发送消息用的路由键
              routingKeyExpression: 'ORDER_PUSH'
              queueNameGroupOnly: true
              bindQueue: false
      bindings:
        input-in-0:
          destination: command_exchange_open
          group: ORDER_END
        output-out-0:
          destination: command_exchange_open
          # 生产者不需要配置group,可删除
          # group: ORDER_END
    function:
      definition: input;output

方案2:动态指定路由键(需要每条消息使用不同路由键时使用)

发送消息时,在消息头中添加amqp_routingKey属性,值为目标路由键即可,示例代码如下:

// 示例:构造带指定路由键的消息
Message<OrderDTO> message = MessageBuilder.withPayload(orderDTO)
    .setHeader("amqp_routingKey", "ORDER_PUSH")
    .build();
// 发送消息
output.send(message);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 05:45:03