stream 多个字段分组_SpringCloud 进阶:消息驱动之Spring Cloud Stream 消费者分组

我的博客:程序员笑笑生,欢迎浏览博客!

上一章 SpringCloud进阶:Spring Cloud Stream 核心组件当中,我们了解了Spring Cloud Stream的核心组件和Spring Integration的简介,本章我们将聊一聊消费者分组相关的知识。

前言

在实际的企业应用场景下,一条消息只能被一个消费者消费,但是在我们部署的应用中,通常会一个消费者应用部署了多台实例。Spring Cloud Stream利用消费者分组就解决这个问题,确保当生产者发送一条消息后,多个实例当中只有一个能够消费到这样的消息。

一 、多实例未分组消费者测试

在我们之前的章节中, SpringCloud进阶-消息驱动pring Cloud Stream中,我们创建了消费者服务:server-receiver和生产者:server-sender,接下来我们结合之前的注册中心Eureka搭建多实例的消费者,首先在server-receiver引入Eureka客户端的依赖:

  <dependency>
    <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

在resource目录下新建applicaiton-s1.yml 、applicaiton-s2.yml 通过applicaiton.yml中配置spring.profiles.active属性激活不同的配置文件:

applicaiton-s1.yml

spring:
  cloud:
    stream:
      bindings:
         input:
            destination: mytopic
            binder: defaultRabbit
      binders:
         defaultRabbit:
             type: rabbit
             environment:
                spring:
                 rabbitmq:
                     host: localhost
                     port: 5672
server:
  port: 8081


eureka:
    instance:
      hostname: eureka7001.com  #eureka服务端的实例名称
      instance-id: receiver1
    client:
      service-url:
         # 与注册中心交互的url
        defaultZone: http://eureka7001.com:7001/eureka/
        enabled: true

applicaiton-s2.yml

spring:
  cloud:
    stream:
      bindings:
         input:
            destination: mytopic
            binder: defaultRabbit
      binders:
         defaultRabbit:
             type: rabbit
             environment:
                spring:
                 rabbitmq:
                     host: localhost
                     port: 5672
server:
  port: 8082

eureka:
    instance:
      hostname: eureka7001.com  #eureka服务端的实例名称
      instance-id: receiver2
    client:
      service-url:
         # 与注册中心交互的url
        defaultZone: http://eureka7001.com:7001/eureka/
        enabled: true

生产者:server-sender的代码不变化,也不需要注册到Eureak中,

@RestController
public class SenderController {

    @Autowired
    SenderSource source;

    @RequestMapping("/send")
    public String sender(String msg) {
        source.output().send(MessageBuilder.withPayload(msg).build());
        return "ok";
    }

先后启动Eureak和服务消费者server-receiver 两 个实例,最后在启动生产者:server-sender,我们看看Eureka中:

80fe47319e2ceff7ae4f0cb12269fcfa.png

显示了2个消费者,我们通过HTTP调用生产者的发送接口: http://localhost:8081/send?msg=test

我们看到receiver1:日志

917b7a9460783386ec8b4cb96b2bdc13.png

我们看到receiver2:日志

9aa81096f2ac39378fb594791ce94059.png

我们看到每个实例都会受到消息。这不是我们想要的,我们需要不管消费者服务有多少实例,确保只有一个实例消费信息。

二、添加分组配置

在Spring Cloud Stream中,如果不给消费者指定一个组Group,那么Spring Cloud Stream将会给当前的实例分配一个匿名的、独立的只有一个成员的消费组,这就导致了一个服务n个实例,就会有n个消费者分组

怎么样去配置是的所有的实例都是一个组呢?我们可以通过配置 spring.cloud.stream.bindings.input.group=group1就可以实现,我们在applicaiton-s1.yml 和 applicaiton-s2.yml 添加:

spring:
  cloud:
    stream:
      bindings:
         input:
           group: group1

重启项目后,再次通过生产者发送消息后,就能确保只有一个消费者收到消息了。

我们可以看到未分组之前是这样的:

我们看到每个实例都会受到消息。这不是我们想要的,我们需要不管消费者服务有多少实例,确保只有一个实例消费信息。

二、添加分组配置

在Spring Cloud Stream中,如果不给消费者指定一个组Group,那么Spring Cloud Stream将会给当前的实例分配一个匿名的、独立的只有一个成员的消费组,这就导致了一个服务n个实例,就会有n个消费者分组

怎么样去配置是的所有的实例都是一个组呢?我们可以通过配置 spring.cloud.stream.bindings.input.group=group1就可以实现,我们在applicaiton-s1.yml 和 applicaiton-s2.yml 添加:

spring:
  cloud:
    stream:
      bindings:
         input:
           group: group1

重启项目后,再次通过生产者发送消息后,就能确保只有一个消费者收到消息了。

我们可以看到未分组之前是这样的:

e23bb51c1ecd24dcc0306c574c285f2b.png

分组之后:

0864fb72b79d8528748894df40ca47bf.png

总结

本章主要是通过示例的方式,使用Spring Cloud Stream如何实现消费者分组,这也是在实际的开发中需要考虑的问题。

----END----

6ecd1b5c58704622a316cf68546c3169.png
SpringCloud基础教程(一)-微服务与SpringCloud​mp.weixin.qq.com
af826725e4dfb218a43d223b4cafa1b0.png
SpringCloud基础教程(二)-服务发现 Eureka​mp.weixin.qq.com
812df7adab73dd53dd886d891931a033.png
SpringCloud基础教程(三)-Eureka进阶​mp.weixin.qq.com
62b2589e47a369f0ad11e2516f3c97f8.png
SpringCloud 基础教程(四)-配置中心入门​mp.weixin.qq.com
5a23ae55e11360328b57ea95db712c2a.png
SpringCloud基础教程(五)-配置中心热生效和高可用​mp.weixin.qq.com
99aa439c5e6b4446247e370010b607c7.png
SpringCloud 基础教程(六)-负载均衡Ribbon​mp.weixin.qq.com
7b4625b108b9660567058ed74d1062b9.png
SpringCloud 基础教程(七)-Feign声明式服务调用​mp.weixin.qq.com
06d197f8febec45d99858c14d71c9918.png
SpringCloud 基础教程(八)-Hystrix熔断器(上)​mp.weixin.qq.com
05efa3ddd5b1e0e7ad16ccedb9a0faac.png
SpringCloud 基础教程(九)-Hystrix服务监控(下)​mp.weixin.qq.com
cff46afff195ee9be89fda6fed2d9060.png
SpringCloud 基础教程(十)-Zuul 服务网关​mp.weixin.qq.com
436f6aa9f1e271d08012d3036a7447fc.png
SpringCloud 基础教程(十一)- Sleuth 调用链追踪简介​mp.weixin.qq.com
51d1b1f9ffb630cec70590d88a515f10.png
SpringCloud 基础教程(十二)-Zipkin 分布式链路追踪系统搭建​mp.weixin.qq.com
3c94780062f1bc278b7dcbdd2aa52dd3.png
SpringCloud 进阶: 消息驱动(入门) Spring Cloud Stream【Greenwich.SR3】​mp.weixin.qq.com
87f6868abf922cadcb2b2ef7d91fd428.png
SpringCloud进阶:Spring Cloud Stream核心组件​mp.weixin.qq.com
243c9c580b7c1d76683e94e101f3cef8.png

更多精彩内容,请期待...

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值