前言

点击进入Spring官网文档

本文章为单体项目,将消费者和生产者写在同一个项目中,介意者不用向下看了。

本文介绍三种应用方式:

1:普通整合RabbitMQ

2:消息分区

3:按条件消费(多个消费者只消费同一队列中满足自己条件的消息)

1:核心依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
<version>${spring.cloud.stream}</version>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
<version>${spring.cloud.stream}</version>
</dependency>

全部依赖:

项目结构图:

2:普通整合RabbitMQ

2.1:application.properties

spring.rabbitmq.host=192.168.1.218
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456
spring.cloud.stream.bindings.dev-exchange.destination=dev-exchange
spring.cloud.stream.bindings.dev-exchange.group=dev-queue
spring.cloud.stream.bindings.dev-exchange.content-type=application/json
spring.cloud.stream.bindings.dev-exchange.consumer.concurrency=1
spring.cloud.stream.bindings.dev-exchange.consumer.max-attempts=1

2.2:定义生产者和消费者接口

import com.boot.rabbitmq.constance.MQConstants;
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.SubscribableChannel; public interface RabbitStream { /**
* 消息流入(消费)
**/
@Input(MQConstants.DEV_EXCHANGE)
SubscribableChannel devConsumer(); /**
* 消息流出(生产)
**/
@Output(MQConstants.DEV_EXCHANGE)
MessageChannel devProducer();
}

2.3:生产者/消费者的具体实现

生产者代码:

@Component
@EnableBinding(RabbitStream.class)
public class DevProducer { private static final Logger logger = LoggerFactory.getLogger(DevProducer.class); private final RabbitStream rabbitStream; public DevProducer(RabbitStream rabbitStream) {
this.rabbitStream = rabbitStream;
} public void sendMsg(MQModel model) {
logger.info("producer:{}", JSON.toJSONString(model));
rabbitStream.devProducer()
.send(MessageBuilder.withPayload(model).build());
}
}

消费者代码:

@Component
@EnableBinding(RabbitStream.class)
public class DevListener { private static final Logger logger = LoggerFactory.getLogger(DevListener.class); @StreamListener(MQConstants.DEV_EXCHANGE)
public void receiveMsgAutoCommit(@Payload String payload) {
logger.info("consumer:{}", payload);
} }

controller代码:

    @PostMapping(value = "/dev")
public void dev(@RequestBody MQModel model) {
devProducer.sendMsg(model);
}

2.4:测试

发送请求:

控制台日志:

3:消息分区

3.1:概念

所谓消息分区就是将一个大队列拆分成多个小队列,然后分解成 producer-A -> queue-A -> Consumer-A 的一种场景。

3.2:如何在项目中使用

1:不需要改很多东西,只需要添加少部分配置即可

## RabbitMQ 消息分区配置
spring.cloud.stream.bindings.partition-exchange.destination=partition-exchange
spring.cloud.stream.bindings.partition-exchange.group=partition-queue
spring.cloud.stream.bindings.partition-exchange.content-type=application/json
spring.cloud.stream.bindings.partition-exchange.consumer.concurrency=1
spring.cloud.stream.bindings.partition-exchange.consumer.max-attempts=1
## 消息分区
spring.cloud.stream.bindings.partition-exchange.consumer.partitioned=true
## 分区数量
spring.cloud.stream.bindings.partition-exchange.producer.partition-count=2
## 机器下标,最大值=partition-count-1
spring.cloud.stream.instance-index=0
## 分区策略表达式
spring.cloud.stream.bindings.partition-exchange.producer.partition-key-expression=payload.mid

然后消息的路由的时候会从payload拿到mid进行条件运算:

mid/2=1则放在应用队列下标为1的队列,mid/2=0则放在队列下标为0的队列。

消息的入队前会计算出该消息应该进入哪个队列,源码截图:



可以看到开启分区之后,payload 的类型不是String,而是具备键值对的实体对象。

4:条件消费

4.1:概念



对同一个队列中的消息按条件进行划分再派发给不同的消费者。

4.2:匹配条件讲解

消息实体:



除了可以用payload中的数据进行匹配条件外,headers中的数据也可以作为条件。

4.3:测试



效果

本文GitHub地址

个人理解,不精之处望指出。

SpringCloud Stream整合RabbitMQ3.5.0的更多相关文章

  1. Rabbitmq基本使用 SpringBoot整合Rabbit SpringCloud Stream+Rabbit

    https://blog.csdn.net/m0_37867405/article/details/80793601 四.docker中使用rabbitmq 1. 搭建和启动 使用地址:rabbitm ...

  2. 每天学点SpringCloud(十四):Zipkin使用SpringCloud Stream以及Elasticsearch

    在前面的文章中,我们已经成功的使用Zipkin收集了项目的调用链日志.但是呢,由于我们收集链路信息时采用的是http请求方式收集的,而且链路信息没有进行保存,ZipkinServer一旦重启后就会所有 ...

  3. 《springcloud 五》springcloud stream

    什么是消息驱动? SpringCloud Stream消息驱动可以简化开发人员对消息中间件的使用复杂度,让系统开发人员更多尽力专注与核心业务逻辑的开发.SpringCloud Stream基于Spri ...

  4. Spring Cloud Alibaba - Spring Cloud Stream 整合 RocketMQ

    Spring Cloud Stream 简介 在微服务的开发过程中,可能会经常用到消息中间件,通过消息中间件在服务与服务之间传递消息,不管你使用的是哪款消息中间件,比如RabbitMQ.Kafka和R ...

  5. SpringCloud Stream使用案例

    官方定义 Spring Cloud Stream 是一个构建消息驱动微服务的框架. 应用程序通过 inputs 或者 outputs 来与 Spring Cloud Stream 中binder 交互 ...

  6. SpringCloud Stream 消息驱动

    1.什么是消息驱动 SpringCloud Stream消息驱动可以简化开发人员对消息中间件的使用复杂度,让系统开发人员更多尽力专注与核心业务逻辑的开发.SpringCloud Stream基于Spr ...

  7. SpringCloud系列之SpringCloud Stream

    SpringCloud Stream SpringCloud Config SpringCloud Gatewa SpringCloud Hystrix SpringCloud 第一部分 文章目录 S ...

  8. 九. SpringCloud Stream消息驱动

    1. 消息驱动概述 1.1 是什么 在实际应用中有很多消息中间件,比如现在企业里常用的有ActiveMQ.RabbitMQ.RocketMQ.Kafka等,学习所有这些消息中间件无疑需要大量时间经历成 ...

  9. SpringCloud Alibaba整合Sentinel

    SpringCloud Alibaba整合Sentinel Sentinel 控制台 1. 概述 Sentinel 提供一个轻量级的开源控制台,它提供机器发现以及健康情况管理.监控(单机和集群),规则 ...

随机推荐

  1. Cobaltstrike去除特征

    出品|MS08067实验室(www.ms08067.com) 本文作者:BlackCat(Ms08067实验室内网小组成员) 前言: 红蓝对抗的时候,如果未修改CS特征.容易被蓝队溯源. 去特征的几种 ...

  2. 深入理解nodejs的HTTP处理流程

    目录 简介 使用nodejs创建HTTP服务 解构request 处理Request Body 处理异常 解构response 简介 我们已经知道如何使用nodejs搭建一个HTTP服务,今天我们会详 ...

  3. Redis 实战 —— 14. Redis 的 Lua 脚本编程

    简介 Redis 从 2.6 版本开始引入使用 Lua 编程语言进行的服务器端脚本编程功能,这个功能可以让用户直接在 Redis 内部执行各种操作,从而达到简化代码并提高性能的作用. P248 在不编 ...

  4. yum.repos.d中的变量($releasever与$basearch)

    今天打算更新一下centos的repo源,把原先国外的repo地址换成国内的,速度快一些.主要替换的文件是/etc/yum.repos.d/Centos-Base.repo .替换的时候,不知道大家有 ...

  5. for循环语句学习

    for循环又称为遍历循环,从名字就可以知道,它用于对象的遍历 语法格式: 会从可迭代对象对象中依次拿出值来赋值给变量,变量的值每次都会被修改 for 变量1[变量2...] in 可迭代对象: 代码块 ...

  6. 掌握数位dp

    最近遇到了数位dp题目,于是就屁颠屁颠的跑过来学习数位dp了~ "在信息学竞赛中,有这样一类问题:求给定区间中,满足给定条件的某个D 进制数或此类数的数量.所求的限定条件往往与数位有关,例如 ...

  7. Educational Codeforces Round 88 (Rated for Div. 2) B、New Theatre Square C、Mixing Water

    题目链接:B.New Theatre Square 题意: 你要把所有"." 都变成"*",你可以有两个选择,第一种就是一次铺一个方块(1*1),第二种就是同一 ...

  8. 牛客练习赛70 D.数树 (模拟,STL)

    题意:每次有\(3\)中操作,对两个点连条边,删去某条边,或者问当前大小不为\(1\)的树的数量.连重边或者删去一条不存在的边,这样的白痴操作可以无视qwq. 题解:水题,用map存一下pair然后分 ...

  9. Pod 实现机制

    Pod 为了亲密性应用而存在: 两个应用需要通过 127.0.0.1 或者 Socket 通信: 两个应用之间发生文件交互: 两个应用发生频繁的调用 共享网络 Pod 中的所有容器拥有同一个 IP 地 ...

  10. python之字符串replace的方法

    1.描述 replace()方法把字符串中的old(旧字符串)替换成new(新字符串),如果有指定第三个参数max,则替换的不超过max次 2.语法 str.replace(old,new[,max] ...