SpringCloud学习笔记(10)——消息驱动的微服务:Spring Cloud Stream
Spring Cloud Stream 是一个用来为微服务应用构建消息驱动能力的架构,它为一些供应商的消息中间件产品提供了个性化的自动化配置实现,并且引入了发布-订阅、消费组以及分区这三个核心概念。简单的说,Spring Cloud Stream本质上是整合了Spring Boot和Spring Integration,实现了一套轻量级的消息驱动的微服务框架。
一、快速入门
- 创建一个基础的Spring Boot 工程,命名为stream-hello.
- 编辑pom.xml 中的依赖关系,引入 Spring Cloud Stream 对 RabbitMQ的支持,具体如下:
<properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
- 创建用于接收来自 RabbitMQ 消息的消费者 SinkReceiver,具体如下 :
/** * Created by liaock on 2019/2/1 **/ @EnableBinding(Sink.class) public class SinkReceiver { private static Logger logger = LoggerFactory.getLogger(StreamHelloApplication.class); @StreamListener(Sink.INPUT) public void receive(Object payload){ logger.info("Received: " + payload); } }
- 创建应用主类
@SpringBootApplication public class StreamHelloApplication { public static void main(String[] args) { SpringApplication.run(StreamHelloApplication.class, args); } }
下面我们可以在 RabbitMQ 的控制台中进入 input.anonymous.AvaJwjXIThWpMSAyde7FJA 队列的管理页面,通过publist message 功能来发送一条消息到该队列中。
此时,我们可以在当前启动的Spring Boot 应用程序的控制台中看到下面的内容:
2019-02-01 10:01:59.586 INFO 7904 — [hWpMSAyde7FJA-1] c.g.streamhello.StreamHelloApplication : Received: send a message
测试成功 !
二、核心概念
上图是官方文档中 Spring Cloud Stream 应用模型的结构图。从中可看到,Spring Cloud Stream构建的应用程序与消息之间是通过 绑定器 Binder 相关联的,绑定器对于应用程序而言起到了隔离作用,它使得不同消息中间件的实现细节对应用程序来说是透明的。
1.绑定器
通过定义绑定器作为中间层,实现了应用程序与消息中间件细节之间的隔离。通过向应用程序暴露统一的Channel通过,使得应用程序不需要再考虑各种不同的消息中间件的实现。当需要升级消息中间件,或者是更换其他消息中间件产品时,我们需要做的就是更换对应的Binder绑定器而不需要修改任何应用逻辑 。
2.发布 - 订阅模式
Spring Cloud Stream 中的消息通信方式遵循了发布 - 订阅模式,当一条消息被投递到消息中间件之后,它会通过共享的Topic主题进行广播,消息消费者在订阅的主题中收到它并触发自身的业务逻辑。 在不同的消息中间件中,Topic可能对应不同的概念,比如:在RabbitMQ中,它对应Exchange,而在Kafka中则对应Kafka 中的Topic.
- SpringCloud微服务实战之消息驱动Stream
- Spring Cloud Stream:基于事件(消息)驱动的微服务框架
- SpringCloudStream 构建消息驱动的微服务框架
- SpringCloud微服务知识整理十:消息驱动的微服务:Spring Cloud Stream
- 使用 Spring Cloud Stream 构建消息驱动微服务
- 使用 Spring Cloud Stream 构建消息驱动微服务
- SpringCloudStream 构建消息驱动的微服务框架 集成kafka_http://blog.spring-cloud.io/blog/sc-stream.html
- SpringCloud Stream-----1、消息驱动的微服务概念
- Spring Cloud构建微服务架构:消息驱动的微服务(消费分区)【Dalston版】
- Spring Cloud 应用篇 之 Spring Cloud Stream(消息驱动)
- SpringCloud——Stream(消息驱动)
- 消息驱动: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版】