springCloudStream简介

由于市面上的消息中间件有很多,导致学习成本很大,而springCloudStream则将消息中间件进行再次封装,屏蔽了具体中间件的细节,应用程序通过inputs或者outputs来与Spring Cloud Stream中binder对象交互,通过配置来进行绑定,spring Cloud Stream的binder对象负责与消息中间件交互,目前支持RabbitMQ和kafka

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-stream</artifactId>
</dependency>

组成

Binder 应用和中间件之间的封装,可以很方便的连接中间件(对应于kafka的topic和rabbitmq的exchange),使用配置文件配置

  • @Input 注解标识输入通道,用于接收消息

    public interface Sink {

     String INPUT = "input";
    
     @Input(Sink.INPUT)
     SubscribableChannel input();
    

    }

  • @Output 注解标识输出通道,用于生产消息

    public interface Source {

     String OUTPUT = "output";
    
     @Output(Source.OUTPUT)
     MessageChannel output();
    

    }

  • @StreamListener 监听队列,用于消费者的队列的消息接收

    @StreamListener(value = Sink.INPUT)
    public void handle(String body) {

      System.out.println("Received: " + body);

    }

  • @EnableBinding 信道channel和exchange绑定

自信的水龙头
1 声望0 粉丝