Spring Cloud 应用篇 之 Spring Cloud Stream(消息驱动)

(一)简介

Spring Cloud Stream 是一个用来为微服务应用构建消息驱动能力的框架。它可以基于 Spring Boot 来创建独立的、可用于生产的 Spring 应用程序。Spring Cloud Stream 为一些供应商的消息中间件产品提供了个性化的自动化配置实现,并引入了发布-订阅、消费组、分区这三个核心概念。通过使用 Spring Cloud Stream,可以有效简化开发人员对消息中间件的使用复杂度,让系统开发人员可以有更多的精力关注于核心业务逻辑的处理。但是目前 Spring Cloud Stream 只支持 RabbitMQ 和 Kafka 的自动化配置。

(二)快速搭建

首先,我们通过一个简单的示例对 Spring Cloud Stream 有一个初步的认识。我们中间件使用 RabbitMQ,创建 spring-cloud-stream 模块

2.1 生产者

SpringCoud版本SR8

pom

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

yml

server:
  port: 8801
spring:
  application:
    name: spring-cloud-stream-production

  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
  cloud:
    stream: #服务整合处理
      bindings:
        output: #指定输入通道对应的主题名
          destination: studyExchange  #表示使用exchange的名字
          content-type: application/json #设置消息类型,本次为json 文本“text/plain”
          #  binder: defaultRabbit #需要绑定消息的具体设置

service

public interface MessageProduc {
    public void send();
}



/**
 * @author zhoukun 86547462@qq.com
 * @version 1.0
 * @date 2020/10/30 2:01
 * @EnableBinding 注解用来指定一个或多个定义了 @Input 或 @Output 注解的接口,
 * 以此实现对消息通道(Channel)的绑定。上面我们通过 @EnableBinding(value =
 *  {StreamClient.class}) 绑定了 StreamClient 接口,该接口是我们自己实现的对
 * 输入输出消息通道绑定的定义
 */
 
@EnableBinding(Source.class)//定义消息推送管道
public class MessageProducImpl implements MessageProduc{

    @Resource
    private MessageChannel output;//消息发送管道

    public void send() {
        String uuid= UUID.randomUUID().toString();
        output.send(MessageBuilder.withPayload(uuid).build());
        System.out.println("8801发布了"+uuid);
    }
}

controller

@RestController
public class MessageController {
    @Resource
    private MessageProducImpl messageProduc;

    @GetMapping("/send")
    public void  send(){
        messageProduc.send();
    }
}

启动类

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

在这里插入图片描述

2.1 消费者

pom

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

yml

server:
  port: 8802
spring:
  application:
    name: spring-cloud-stream-consumption

  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
  cloud:
    stream: #服务整合处理
      bindings:
        input: #指定输入通道对应的主题名(消费者就改了一下这个input)
          destination: studyExchange  #表示使用exchange的名字
          content-type: application/json #设置消息类型,本次为json 文本“text/plain”
          group: groupingA #解决重复消费的问题(分到同一组里就好了)
          #  binder: defaultRabbit #需要绑定消息的具体设置

controller

@RestController
@EnableBinding(Sink.class)
public class messagecontroller {


    @StreamListener(Sink.INPUT)
    public void input(Message<String> message)
    {

        System.out.println("8802服务消费者接收到了:"+message.getPayload());
    }
}

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值