无法在Rabbitmq上发布消息以使用路由密钥与Spring Cloud进行直接交换

时间:2019-08-16 06:34:48

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

我有一个Rabbitmq交换[test-exchange],这是一个直接交换,它使用路由键[test-routing-key]绑定到队列[test-queue]。

我正在尝试使用Spring Cloud流将消息发布到此交换,但消息未到达队列

我对pom.xml的依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>

使用的春季云是Edgware.RELEASE

我的源文件代码是

public interface DemoSource {

    @Output("demoChannel")
    MessageChannel userChannel();

}

我的应用文件代码为

@SpringBootApplication
@EnableBinding(DemoSource.class)
public class PublisherApplication implements CommandLineRunner{

    @Autowired
    DemoSource demoSource;

    public static void main(String[] args) {
        SpringApplication sp = new SpringApplication(PublisherApplication.class);
        sp.run(args);

    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Sending message...");
        User user = new User();
        user.setUserId("testId");
        user.setUserName("testName");
        demoSource.userChannel().send(MessageBuilder.withPayload(user).build());

    }

}

我的application.properties文件是

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

spring.cloud.stream.bindings.demoChannel.destination=test-exchange
spring.cloud.stream.bindings.demoChannel.group=test-routing-key
spring.cloud.stream.bindings.demoChannel.producer.bindingRoutingKey=test-routing-key
spring.cloud.stream.bindings.demoChannel.producer.routing-key-expression= 'test-routing-key'
spring.cloud.stream.rabbit.bindings.demoChannel.producer.exchange-type=direct
spring.cloud.stream.rabbit.bindings.demoChannel.producer.declare-exchange=false
spring.cloud.stream.default.contentType=application/json

1 个答案:

答案 0 :(得分:1)

我参加这个聚会超级迟到,但您的 @Output("demoChannel") 已明确命名,因此当您想发布时,您需要

demoSource.demoChannel().send(MessageBuilder.withPayload(user).build());

而不是你当前的

demoSource.userChannel().send(MessageBuilder.withPayload(user).build());