spring cloud笔记

来源:互联网 发布:启用新域名 编辑:程序博客网 时间:2024/06/05 21:09

本文出自于EumJi的个人笔记,仅限于学习用途的转载,转载请注明出处 http://www.eumji025.com/article/details/252989

stream介绍

Spring Cloud Stream is a framework for building message-driven microservice applications. Spring Cloud Stream builds upon Spring Boot to create standalone, production-grade Spring applications, and uses Spring Integration to provide connectivity to message brokers. It provides opinionated configuration of middleware from several vendors, introducing the concepts of persistent publish-subscribe semantics, consumer groups, and partitions.

译:Spring Cloud Stream是构建消息驱动的微服务应用程序的框架。Spring Cloud Stream基于Spring Boot建立独立的生产级Spring应用程序,并使用Spring Integration提供与消息代理的连接。它提供了来自几家供应商的中间件的意见配置,介绍了持久发布订阅语义,消费者组和分区的概念。

Sink介绍

Sink是stream中接受消息的接口,主要用来绑定管道接受消息。代码如下:
public interface Sink {    String INPUT = "input";    @Input(Sink.INPUT)    SubscribableChannel input();}

从字面的介绍也可以看出@Input注解表示绑定订阅输入管道,通过管道接受来自发布者的消息。

Source介绍

Source是stream中的发布消息的接口,主要用来绑定管道发布消息。代码如下:
public interface Source {  String OUTPUT = "output";  @Output(Source.OUTPUT)  MessageChannel output();}

@output注解表示绑定输出管道,通过管道发布消息。

Processer介绍

Processor是stream中绑定输入输出的接口,主要用来讲发布者和订阅者绑定到一起。代码如下:

public interface Processor extends Source, Sink {}

Processer继承了 SourceSink 具有输入和输出管道。

案例展示

说明一下,本系列使用的是RabbitMQ作为消息队列服务器。需要先了解RabbitMQ。

Sink模块

首先创建一个Sink的maven项目spring-cloud-stream-binding-input

pom.xml

<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>

application.yml

配置文件中主要配置端口信息和绑定管道。

server:  port: 8080spring:  cloud:    stream:      bindings:        input:          destination: sink-channel

Application.java

@SpringBootApplication@EnableBinding(Sink.class)public class SpringCloudStreamBindingInputApplication {    private static Logger logger = LoggerFactory.getLogger(SpringCloudStreamBindingInputApplication.class);    public static void main(String[] args) {        SpringApplication.run(SpringCloudStreamBindingInputApplication.class, args);    }   @StreamListener(Sink.INPUT)    public void sinkMessage(Object message){        logger.info("received message: "+message);    }}

@EnableBinding注解用于绑定一个或者多个接口作为参数,本文中绑定sink接口来接受消息。

@StreamListener注解使方法接受管道推送的消息。

本例子只是简单得打印消息。

Source模块

新建一个source的maven项目spring-cloud-stream-binding-output

pom.xml

和sink例子中的一样,这里就不进行描述。

application.yml

设置端口和管道信息。

server:  port: 8081fixedDelay: 5000spring:  cloud:    stream:      bindings:        output:          destination: sink-channel

 application.java

@SpringBootApplication@EnableBinding(Source.class)public class   SpringCloudStreamBindingSourceApplication {    private static Logger logger = LoggerFactory.getLogger(SpringCloudStreamBindingSourceApplication.class);   public static void main(String[] args) {      SpringApplication.run(SpringCloudStreamBindingSourceApplication.class, args);   }    @InboundChannelAdapter(value = Source.OUTPUT)    public String timerMessageSource() {        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());        logger.info("publish message :"+format);        return format;    }}

@InboundChannelAdapter注解用来绑定输出的管道,进行消息推送。本案例就推送格式化的时间。

测试

首先运行input模块,开始等待消息的推送。

然后运行output模块,开始推送消息,两边的控制台分别输出如下类似的信息。

input部分:

2017-05-27 17:13:45.511  INFO 23183 --- [nW9UuKzh3GPEg-1] inkDemo$$EnhancerBySpringCGLIB$$f1c19db8 : received message: 2017-05-27 17:13:452017-05-27 17:13:46.513  INFO 23183 --- [nW9UuKzh3GPEg-1] inkDemo$$EnhancerBySpringCGLIB$$f1c19db8 : received message: 2017-05-27 17:13:462017-05-27 17:13:47.514  INFO 23183 --- [nW9UuKzh3GPEg-1] inkDemo$$EnhancerBySpringCGLIB$$f1c19db8 : received message: 2017-05-27 17:13:472017-05-27 17:13:48.516  INFO 23183 --- [nW9UuKzh3GPEg-1] inkDemo$$EnhancerBySpringCGLIB$$f1c19db8 : received message: 2017-05-27 17:13:482017-05-27 17:13:49.518  INFO 23183 --- [nW9UuKzh3GPEg-1] inkDemo$$EnhancerBySpringCGLIB$$f1c19db8 : received message: 2017-05-27 17:13:49

output部分:

2017-05-27 17:13:45.508  INFO 23092 --- [ask-scheduler-2] eSource$$EnhancerBySpringCGLIB$$fb67f863 : publish message :2017-05-27 17:13:452017-05-27 17:13:46.510  INFO 23092 --- [ask-scheduler-4] eSource$$EnhancerBySpringCGLIB$$fb67f863 : publish message :2017-05-27 17:13:462017-05-27 17:13:47.511  INFO 23092 --- [ask-scheduler-1] eSource$$EnhancerBySpringCGLIB$$fb67f863 : publish message :2017-05-27 17:13:472017-05-27 17:13:48.513  INFO 23092 --- [ask-scheduler-5] eSource$$EnhancerBySpringCGLIB$$fb67f863 : publish message :2017-05-27 17:13:482017-05-27 17:13:49.515  INFO 23092 --- [ask-scheduler-3] eSource$$EnhancerBySpringCGLIB$$fb67f863 : publish message :2017-05-27 17:13:49

扩展

前面我通过sinksource就完成了消息的输入和输出,现在我们模拟一下他们不直接通信,而通过processer进行中转。

通道修改

我们讲input项目里面的sink绑定的管道进行修改。

spring:  cloud:    stream:      bindings:        input:          destination: source-channel

Processer模块

创建maven项目spring-cloud-stream-binding-transform,用于消息传递。

pom.xml

<dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-configuration-processor</artifactId>            <optional>true</optional>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>

application.yml

server:  port: 8082spring:  cloud:    stream:      bindings:        output:          destination: source-channel        input:          destination: sink-channel

配置分别和sink和source绑定的管道。

application.java

@SpringBootApplication@EnableBinding(Processor.class)public class SpringCloudStreamTransformApplication {    private static Logger logger = LoggerFactory.getLogger(SpringCloudStreamTransformApplication.class);    private static String name = "logging";    public static void main(String[] args) {        SpringApplication.run(SpringCloudStreamTransformApplication.class, args);    }   @ServiceActivator(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)    public Object transform(Object payload) {        logger.info("Transformed by " + this.name + ": " + payload);        return payload;    }}

@ServiceActivator注解分别绑定输入和输出的管道,两个管道之间消息的传递。

测试

然后再次先运行input模块,等待消息。

然后运行output模块,推送消息,此时input控制台没有变化,因为不在一个管道,消息无法传递。

最后运行transform模块,进行消息传递,再次观察input和output模块的控制台,发现都在输出消息。

结语

本部分只是简单的对steam中sink,sourceprocesser进行 简单的使用。

后续要有更多深入的探讨。

与君共勉!!!

源码地址

spring-cloud-stream-binding-input
spring-cloud-stream-binding-output
spring-cloud-stream-binding-processer

原创粉丝点击