spring cloud stream RabbitMQ 特性介绍

1. 前言

1.1 概述

“Spring Cloud Stream is a framework for building message-driven microservice applications.”这是来自官方文档对spring cloud sream的介绍,大致可以理解为Spring Cloud Stream 是一个构建消息驱动微服务的框架。

       本文档是基于spring-cloud-stream(消息代理)对消息中间件rabbitMQ的支持。

2.使用指南

2.1 前期准备

         RabbitMQ的搭建,具体参考http://www.rabbitmq.com/install-windows.html

       应用程序通过pom.xml引用核心jar包

       <dependency>

              <groupId>org.springframework.boot</groupId>

              <artifactId>spring-boot-starter-web</artifactId>

       </dependency>

       <dependency>

              <groupId>org.springframework.cloud</groupId>

              <artifactId>spring-cloud-stream</artifactId>

       </dependency>

       <dependency>

              <groupId>org.springframework.cloud</groupId>

              <artifactId>spring-cloud-stream-binder-rabbit</artifactId>

       </dependency>

       <dependency>

              <groupId>org.springframework.boot</groupId>

              <artifactId>spring-boot-devtools</artifactId>

              <scope>runtime</scope>

       </dependency>

2.2 Spring Cloud Stream 相关概念

Application Model

 

从上图我们知道spring cloud stream 通过input channels和output channels跟Binder交互,特定Binder在与对应的外部broker节点交互。

Binder

Binder 是 Spring Cloud Stream 的一个抽象概念,是应用与消息中间件之间的粘合剂。目前 Spring Cloud Stream 实现了 Kafka 和 Rabbit MQ 的binder。

通过 binder ,可以很方便的连接中间件,可以动态的改变消息的
destinations(对应于 Kafka 的topic,Rabbit MQ 的 exchanges),这些都可以通过外部配置项来做到。

甚至可以任意的改变中间件的类型而不需要修改一行代码。

Publish-Subscribe

消息的发布(Publish)和订阅(Subscribe)是事件驱动的经典模式。Spring Cloud Stream 的数据交互也是基于这个思想。生产者把消息通过某个 topic 广播出去(Spring Cloud Stream 中的 destinations)。其他的微服务,通过订阅特定 topic 来获取广播出来的消息来触发业务的进行。

这种模式,极大的降低了生产者与消费者之间的耦合。即使有新的应用的引入,也不需要破坏当前系统的整体结构。

Consumer Groups

Spring Cloud Stream 的这个分组概念的意思基本和 Kafka 一致。

微服务中通过对同一个应用创建多个实例来达到更高的处理能力是非常必须的。对于这种情况,同一个事件防止被重复消费,只要把这些应用放置于同一个 “group” 中,就能够保证消息只会被其中一个应用消费一次。

通过spring.cloud.stream.bindings.<channelName>.group=demo这种方式在配置文件中配置。

Durability

消息事件的持久化是必不可少的。Spring Cloud Stream 可以动态的选择一个消息队列是持久化,还是非持久化。

Bindings

bindings 是我们通过配置把应用和spring cloud stream 的 binder 绑定在一起,之后我们只需要修改 binding 的配置来达到动态修改topic、exchange、type等一系列信息而不需要修改一行代码。

2.3 基于 RabbitMQ 使用

内置消息发送接收

消息接收

Spring Cloud Stream 基本用法,需要定义一个接口,如下是内置的一个接口。

public interface Sink {
     String INPUT = "input";
     @Input("input")
    SubscribableChannel input();
}
消息接收
消息的发送同消息的接受,都需要定义一个接口,不同的是接口方法的返回对象是 MessageChannel,下面是 Spring Cloud Stream 内置的接口:
public interface Source {
    String OUTPUT = "output";
    @Output("output")
    MessageChannel output();
}

自定义消息发送接收

Spring Cloud Stream 内置了两种接口,分别定义了 binding 为 “input” 的输入流,和 “output” 的输出流,而在我们实际使用中,往往是需要定义各种输入输出流。使用方法也很简单。

public interface ErrorHandlingAppLevel {      
        public static final String ERROR_HANDLING_INPUT = "ErrorHandling-input-appLevel";
        public static final String ERROR_HANDLING_OUTPUT = "ErrorHandling-output-appLevel"  
        @Input(ERROR_HANDLING_INPUT)
        SubscribableChannel input();
        @Output(ERROR_HANDLING_OUTPUT)
        MessageChannel send();
}

一个接口中,可以定义无数个输入输出流,可以根据实际业务情况划分。上述的接口,定义了一个异常输入,和异常输出两个 binding。

使用时,需要在 @EnableBinding 注解中,添加自定义的接口ErrorHandlingAppLevel。使用 @StreamListener 做监听的时候,需要指定 ErrorHandlingAppLevel. ERROR_HANDLING_INPUT,相关配置如下:

#--------rabbitMQ连接配置---------
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.rabbitmq.virtual-host=/
#------------rabbitMQ分组-----------------
spring.cloud.stream.bindings.ErrorHandling-output-appLevel.destination=ErrorHandling-input-appLevel
spring.cloud.stream.bindings.ErrorHandling-input-appLevel.group=demo1 

Message ttl(Time to Live)消息过期

#配置文件中加上如下配置,设置队列中消息的过期时间为一分钟spring.cloud.stream.rabbit.bindings.ErrorHandling-input-appLevel.consumer.ttl=60000

可以设置ErrorHandling-input-appLevel为这个队列中的消息过期时间为1分钟,当超过1分钟还没消费,则自动丢弃。如下图,设置了过期的队列会加上ttl.如下图所示。

Consumer Groups(广播防重复消费)

通过在配置文件加上如下配置,可以指订队列的分组spring.cloud.stream.bindings.ErrorHandling-input-appLevel.group=demo

不同应用订阅统一组(group)消息,只会发给组里的一个应用。针对集群,一个应用多个实例,可以把多个实例放到一个组里,这样保证广播一个应用重复消费。

concurrency并发收消息

这些属性通过org.springframework.cloud.stream.binder.ConsumerProperties声明,默认是1

#配置文件配置如下
spring.cloud.stream.bindings.input.consumer.concurrency=3

transacted事务通道

当需要使用事务通道的时候,通过如下配置,默认情况下为false

spring.cloud.stream.rabbit.bindings.<channelName>.consumer. transacted =true

Error Handling(异常处理)

异常消息重试消费

异常尝试机制,通过ConsumerProperties类中声明,默认3次

#配置文件如下spring.cloud.stream.bindings.ErrorHandling-input-appLevel.consumer.max-attempts=2

Drop Failed Messages(丢弃失败消息)

默认情况下,系统错误,消息被丢弃。

DLQ - Dead Letter Queue(死信队列)

DLQ允许失败消息被发送到指定目的地--- Dead Letter Queue.

#通过在配置文件中设置,spring.cloud.stream.rabbit.bindings.input.consumer.auto-bind-dlq=true
备注:input对应input chanel名

#死信队列 消息存放时间 ,最好不要设置不然消息将会丢弃

#spring.cloud.stream.rabbit.bindings.ErrorHandling-input-appLevel.consumer.dlqTtl=60000
#如下配置,该值为false如果设置了死信队列,消息对原封不动的发送到死信队列,如果为true,则消息对带上错误信息发送至死信队列
spring.cloud.stream.rabbit.bindings.ErrorHandling-input-appLevel.consumer.republishToDlq=true 
#默认false,只能抛AmqpRejectAndDontRequeueException消息才能到死信队列,如果为true,所有错误消息都会抛到死信队列
#spring.cloud.stream.rabbit.bindings.ErrorHandling-input-appLevel.consumer.requeueRejected=true

3. 参考资料

Spring-cloud-stream官网:http://cloud.spring.io/spring-cloud-static/spring-cloud-stream/Fishtown.RC1/single/spring-cloud-stream.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值