Spring Cloud Stream消息驱动组件

58 篇文章 1 订阅
29 篇文章 1 订阅

Spring Cloud Stream消息驱动组件

Spring Cloud Stream 是⼀个构建消息驱动微服务的框架。应⽤程序通过inputs(相当于消息消费者
consumer)或者outputs(相当于消息⽣产者producer)来与Spring Cloud Stream中的binder对象交
互,⽽Binder对象是⽤来屏蔽底层MQ细节的,它负责与具体的消息中间件交互。
说⽩了:对于我们来说,只需要知道如何使⽤Spring Cloud Stream与Binder对象交互即可

Stream

在这里插入图片描述
在这里插入图片描述

Binder绑定器
Binder绑定器是Spring Cloud Stream 中⾮常核⼼的概念,就是通过它来屏蔽底层不同MQ消息中间件
的细节差异,当需要更换为其他消息中间件时,我们需要做的就是更换对应的Binder绑定器⽽不需要修
改任何应⽤逻辑(Binder绑定器的实现是框架内置的, Spring Cloud Stream⽬前⽀持Rabbit、 Kafka两
种消息队列)

传统MQ模型与Stream消息驱动模型

在这里插入图片描述

Stream消息通信⽅式及编程模型
  • Stream消息通信⽅式

Stream中的消息通信⽅式遵循了发布—订阅模式。
在Spring Cloud Stream中的消息通信⽅式遵循了发布-订阅模式,当⼀条消息被投递到消息中间件之
后,它会通过共享的 Topic 主题进⾏⼴播,消息消费者在订阅的主题中收到它并触发⾃身的业务逻辑处
理。这⾥所提到的 Topic 主题是Spring Cloud Stream中的⼀个抽象概念,⽤来代表发布共享消息给消
费者的地⽅。在不同的消息中间件中, Topic 可能对应着不同的概念,⽐如:在RabbitMQ中的它对应
了Exchange、在Kakfa中则对应了Kafka中的Topic。

  • 编程模型
    如下的注解⽆⾮在做⼀件事,把我们结构图中那些组成部分上下关联起来,打通通道(这样的话⽣产者
    的message数据才能进⼊mq, mq中数据才能进⼊消费者⼯程)
注解描述
@Input(在消费者⼯程中使⽤)注解标识输⼊通道,通过该输⼊通道接收到的消息进⼊应⽤程序
@Output(在⽣产者⼯程中使⽤)注解标识输出通道,发布的消息将通过该通道离开应⽤程序
@StreamListener(在消费者⼯程中使⽤,监听message的到来)监听队列,⽤于消费者的队列的消息的接收有消息监听…)
@EnableBinding把Channel和Exchange(对于RabbitMQ)绑定在⼀起

新建生产者


<!--主要配置-->
    <dependencies>
        <!--eureka client 客户端依赖引⼊-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--spring cloud stream 依赖(rabbit) -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
        </dependency>
    </dependencies>

 <dependencyManagement>
        <dependencies>
            <!--spring cloud依赖管理,引入了Spring Cloud的版本-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Actuator可以帮助你监控和管理Spring Boot应用-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <!--日志依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <!--测试依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>

        <!--eureka server 需要引入Jaxb,开始-->
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.2.10-b140310.1920</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!--引入Jaxb,结束-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-commons</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>


    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <!--编译插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <delimiters>
                        <delimit>@</delimit>
                        <useDefaultDelimiters>false</useDefaultDelimiters>
                    </delimiters>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • 启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class StreamProduct9090Application {
    public static void main(String[] args) {
        SpringApplication.run(StreamProduct9090Application.class,args);
    }
}

  • service


public interface IMessageProducer {


    public void sendMessage(String content);
}

import com.liu.service.IMessageProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.support.MessageBuilder;

// Source.class里面就是对输出通道的定义(这是Spring Cloud Stream内置的通道封装)
@EnableBinding(Source.class)
public class MessageProducerImpl implements IMessageProducer {

    // 将MessageChannel的封装对象Source注入到这里使用
    @Autowired
    private Source source;


    @Override
    public void sendMessage(String content) {
        // 向mq中发送消息(并不是直接操作mq,应该操作的是spring cloud stream)
        // 使用通道向外发出消息(指的是Source里面的output通道)
        source.output().send(MessageBuilder.withPayload(content).build());

    }
}


  • application.yml
server:
  port: 9090

# 注册到eureka注册中
eureka:
  client:
    service-url:
      # 使用逗号分隔 多个实例,
      defaultZone: http://EduCloudEurekaServerB:8762/eureka,http://EduCloudEurekaServerA:8761/eureka
  instance:
    prefer-ip-address: true #服务实例中显示ip,而不是显示主机名(兼容老的eureka版本)
    # 实例名称: 192.168.1.103:lagou-service-resume:8080,我们可以自定义它
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
    # 自定义元数据
#    metadata-map:
#      cluster: cl1
#      region: rn1
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always
logging:
  level:
    com.liu.service.feign.ResumeFeignClient: debug


spring:
  application:
    name: edu-cloud-stream-product
  cloud:
    stream:
      binders: # 绑定MQ服务信息(此处我们是RabbitMQ)
        eduRabbitBinder: # 给Binder定义的名称,用于后面的关联
          type: rabbit #类型kafka
          environment:
            spring:
              rabbitmq:
                host: 127.0.0.1
                port: 5672
                username: root
                password: root123
                virtual-host: dev_tests
      bindings: #关联整合通道和binder对象
        output:  # output是我们定义的通道名称,此处不能乱改
          destination: eduExchange # 要使用的Exchange名称(消息队列主题名称)
          contentType: text/plain # application/json # 消息类型设置,比如json
          binder: eduRabbitBinder





  • 测试类
@SpringBootTest(classes = {StreamProduct9090Application.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class MessageProducerTest {

    @Autowired
    private IMessageProducer messageProducer;

    @Test
    public void testSendMessage() {
        messageProducer.sendMessage("hello world-edu102");

    }
}

消费者

  • 启动类
@EnableDiscoveryClient
@SpringBootApplication
public class SteamConsumer9091Applicatin {
    public static void main(String[] args) {
        
        SpringApplication.run(SteamConsumer9091Applicatin.class,args);
    }
}


  • service -监听消息
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;

@EnableBinding(Sink.class)
public class MessageConsumerService {


    @StreamListener(Sink.INPUT)
    public void recevieMessages(Message<String> message) {
        System.err.println("=========接收到的消息: " + message);
    }
}

*配置 applicatin.yml

server:
  port: 9091

# 注册到eureka注册中
eureka:
  client:
    service-url:
      # 使用逗号分隔 多个实例,
      defaultZone: http://EduCloudEurekaServerB:8762/eureka,http://EduCloudEurekaServerA:8761/eureka
  instance:
    prefer-ip-address: true #服务实例中显示ip,而不是显示主机名(兼容老的eureka版本)
    # 实例名称: 192.168.1.103:lagou-service-resume:8080,我们可以自定义它
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
    # 自定义元数据
#    metadata-map:
#      cluster: cl1
#      region: rn1
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always
logging:
  level:
    com.liu.service.feign.ResumeFeignClient: debug


spring:
  application:
    name: edu-cloud-stream-consumer
  cloud:
    stream:
      binders: # 绑定MQ服务信息(此处我们是RabbitMQ)
        eduRabbitBinder: # 给Binder定义的名称,用于后面的关联
          type: rabbit #类型kafka
          environment:
            spring:
              rabbitmq:
                host: 127.0.0.1
                port: 5672
                username: root
                password: root123
                virtual-host: dev_tests
      bindings: #关联整合通道和binder对象
        input:  # output是我们定义的通道名称,此处不能乱改
          destination: eduExchange # 要使用的Exchange名称(消息队列主题名称)
          contentType: text/plain # application/json # 消息类型设置,比如json
          binder: eduRabbitBinder





在这里插入图片描述
使用测试类发消息,消费者收到消息

=========接收到的消息: GenericMessage [payload=hello world-edu102, headers={amqp_receivedDeliveryMode=PERSISTENT, amqp_receivedExchange=eduExchange, amqp_deliveryTag=1, deliveryAttempt=1, amqp_consumerQueue=eduExchange.anonymous.cpmhb3fNTTeNLzZXOcCBaw, amqp_redelivered=false, amqp_receivedRoutingKey=eduExchange, amqp_timestamp=Tue Aug 25 17:34:48 CST 2020, amqp_messageId=d74b256b-1b61-b06a-0776-1cfb736edfc9, id=5c592013-56ee-b170-9d52-b1cf910312ad, amqp_consumerTag=amq.ctag-qzxov5dCRtrx1OBjq9LlUw, contentType=text/plain, timestamp=1598348088922}]
自定义消息通道

public interface CustomChannel {
    String INPUT_LOG = "inputLog";
    String OUTPUT_LOG = "outputLog";

    @Input(value = INPUT_LOG)
    SubscribableChannel inputLog();


    @Output(OUTPUT_LOG)
    MessageChannel outputLog();
}

如何使⽤?
1)在 @EnableBinding 注解中,绑定⾃定义的接⼝
2)使⽤ @StreamListener 做监听的时候,需要指定 CustomChannel.INPUT_LOG

bindings:
   inputLog:
     destination: eduExchange
   outputLog:
     destination: eduExchange
消费者端分组

消费者端有两个(消费同⼀个MQ的同⼀个主题),但是呢我们的业务场景中希望这
个主题的⼀个Message只能被⼀个消费者端消费处理,此时我们就可以使⽤消息分组

我们仅仅需要在服务消费者端设置 spring.cloud.stream.bindings.input.group 属性,多个消费者实例配置为同⼀个group名称(在同⼀个group中的多个消费者只有⼀个可以获取到消息并消费)


  cloud:
    stream:
      binders: # 绑定MQ服务信息(此处我们是RabbitMQ)
        eduRabbitBinder: # 给Binder定义的名称,用于后面的关联
          type: rabbit #类型kafka
          environment:
            spring:
              rabbitmq:
                host: 127.0.0.1
                port: 5672
                username: root
                password: root123
                virtual-host: dev_tests
      bindings: #关联整合通道和binder对象
        input:  # output是我们定义的通道名称,此处不能乱改
          destination: eduExchange # 要使用的Exchange名称(消息队列主题名称)
          contentType: text/plain # application/json # 消息类型设置,比如json
          binder: eduRabbitBinder
          group:  edu001

在这里插入图片描述

  • 关闭消费者
    在这里插入图片描述
  • 启动消费者

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值