SpringCloud微服务实战之消息驱动Stream
2017-12-25 10:20
771 查看
Spring Cloud Stream 是一个用来为微服务应用构建消息驱动能力的架构,它为一些供应商的消息中间件产品提供了个性化的自动化配置实现,并且引入了发布-订阅、消费组以及分区这三个核心概念。简单的说,Spring Cloud Stream本质上是整合了Spring Boot和Spring Integration,实现了一套轻量级的消息驱动的微服务框架。
一、构建一个简单服务
1、创建Maven项目SpringCloud-Stream
2、在pom中添加依赖
3、编写消息接收类用于接收来自RabbitMQ的消息。@EnableBinding注解用来指定一个或者多个定义了@Input或@Output注解的接口,以此实现对消息通道的绑定。
4、创建主类
5、启动项目测试
启动时,控制台输出日志中可以看出使用guest用户创建了一个指向127.0.0.1:5672的连接。可以通过配置文件application.properties来对RabbitMQ的连接信息以及通道等进行配置。
登录RabbiltMQ管理中心:http://localhost:15672,在connection中也可看见这个连接
在RabbitMQ管理中心,发送一条测试消息
在项目启动控制台可以看见打印出: Recevied:[B@14c7e756 此时输出的是消息对象的引用。
一、构建一个简单服务
1、创建Maven项目SpringCloud-Stream
2、在pom中添加依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- add stream depend --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> </dependency> </dependencies>
3、编写消息接收类用于接收来自RabbitMQ的消息。@EnableBinding注解用来指定一个或者多个定义了@Input或@Output注解的接口,以此实现对消息通道的绑定。
@EnableBinding(Sink.class) public class SinkRecevier { @StreamListener(Sink.INPUT) public void receive(Object payload){ System.out.println("Recevied:"+payload); } }
4、创建主类
@SpringBootApplication public class StreamApplication { public static void main(String[] args) { SpringApplication.run(StreamApplication.class, args); } }
5、启动项目测试
启动时,控制台输出日志中可以看出使用guest用户创建了一个指向127.0.0.1:5672的连接。可以通过配置文件application.properties来对RabbitMQ的连接信息以及通道等进行配置。
spring.cloud.stream.bindings.input.destination=raw-sensor-data spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.username=springcloud spring.rabbitmq.password=springcloud
登录RabbiltMQ管理中心:http://localhost:15672,在connection中也可看见这个连接
在RabbitMQ管理中心,发送一条测试消息
在项目启动控制台可以看见打印出: Recevied:[B@14c7e756 此时输出的是消息对象的引用。
相关文章推荐
- 使用 Spring Cloud Stream 构建消息驱动微服务
- SpringCloudStream 构建消息驱动的微服务框架 集成kafka_http://blog.spring-cloud.io/blog/sc-stream.html
- SpringCloudStream 构建消息驱动的微服务框架
- 使用 Spring Cloud Stream 构建消息驱动微服务
- SpringCloud Stream-----1、消息驱动的微服务概念
- Spring Cloud架构教程 (八)消息驱动的微服务(消费组)【Dalston版】
- Spring Cloud架构教程 (八)消息驱动的微服务(消费组)【Dalston版】
- Spring Cloud Stream(消息驱动)
- Spring Cloud架构教程 (七)消息驱动的微服务(核心概念)【Dalston版】
- Spring Cloud架构教程 (六)消息驱动的微服务【Dalston版】
- Spring Cloud架构教程 (六)消息驱动的微服务【Dalston版】
- Spring Cloud架构教程 (七)消息驱动的微服务(核心概念)【Dalston版】
- Spring Cloud架构教程 (八)消息驱动的微服务(消费组)【Dalston版】
- Spring Cloud构建微服务架构:消息驱动的微服务(消费分区)【Dalston版】
- Spring Cloud构建微服务架构:消息驱动的微服务(消费分区)【Dalston版】
- Spring Cloud构建微服务架构:消息驱动的微服务(核心概念)【Dalston版】
- Spring Cloud Stream(消息驱动)介绍
- SpringCloud微服务实战之消息总线BUS-RabbitMQ
- Spring Cloud构建微服务架构:消息驱动的微服务(消费组)【Dalston版】
- (Spring Cloud微服务实战-书中之坑)spring cloud Stream注入绑定接口报错bean definition with this name already exists