我正在尝试使用RabbitMQ配置一个简单的Spring Cloud Stream应用程序 . 我使用的代码主要取自spring-cloud-stream-samples . 我有一个切入点:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
和示例中的简单消息生成器:
@EnableBinding(Source.class)
public class SourceModuleDefinition {
private String format = "yyyy-MM-dd HH:mm:ss";
@Bean
@InboundChannelAdapter(value = Source.OUTPUT, poller = @Poller(fixedDelay = "${fixedDelay}", maxMessagesPerPoll = "1"))
public MessageSource<String> timerMessageSource() {
return () -> new GenericMessage<>(new SimpleDateFormat(this.format).format(new Date()));
}
}
另外,这是application.yml配置:
fixedDelay: 5000
spring:
cloud:
stream:
bindings:
output:
destination: test
当我运行该示例时,它连接到Rabbit并创建一个名为test的交换 . 但我的问题是,它不会自动创建队列和绑定 . 我可以看到兔子的流量,但我的所有消息都消失了 . 虽然我需要他们留在一些队列,除非他们被消费者阅读 .
也许我误解了一些东西,但从我读过的所有主题来看,Spring Cloud Stream似乎应该自动创建队列和绑定 . 如果没有,我该如何配置它以便我的消息保持不变?
我正在使用Spring Cloud Brixton.SR5和Spring Boot 1.4.0.RELEASE .
2 回答
一旦启动消费者应用程序,就会创建一个队列 .
对于Rabbit MQ,我们为每个使用者组分别设置了队列,并且我们事先无法知道所有组,如果您希望为事先已知的使用者组自动创建队列,则可以使用
requiredGroups
属性 . 生产环境 者 . 这将确保消息持续存在,直到启动该组的使用者 .详情请见:http://docs.spring.io/spring-cloud-stream/docs/Brooklyn.BUILD-SNAPSHOT/reference/htmlsingle/#_producer_properties
您需要一个消费者才能创建一个队列 .
在这里,您可以找到使用rabbitMq的 生产环境 者和消费者的示例:
http://ignaciosuay.com/how-to-implement-asyncronous-communication-between-microservices-using-spring-cloud-stream/