Spring boot對MQ類如RabbitMQ、kafka支持都很好,可是仍然要寫一些模板代碼。Spring cloud stream進一步掩蓋了這個差別,僅僅使用配置就能夠完成。spring
Spring cloud Stream 用了基於topic-subsriber的模式,雖然不支持所有MQ的特性,但絕大多數應用來講,這樣就足夠用了,畢竟方便不少。具體用法以下:數據庫
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> </dependency>
public interface MyMQInterface{ @Input("通道名") SubscribableChannel inputChannel(); @Output("通道名") MessageChannel outputChanel(); }
@EnableBinding(MyMQInterface.class)
@Autowired MyMQInterface myInterface; ..... myInterface.inputChannel().send(MessageBuilder....);
@SystemListener("通道名") public void onReceive(Message content)
spring.cloud.stream.bindings.testOrders.group=分組名