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=分组名