SpringCloudStream 3.x rabbit 使用

2024-04-28 07:04

本文主要是介绍SpringCloudStream 3.x rabbit 使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 前言

今天带来的是SpringCloudStream 3.x 的新玩法,通过四大函数式接口的方式进行数据的发送和监听。本文将通过 rabbitMQ 的方式进行演示

3.x版本后是 可以看到 @StreamListener 和 @EnableBinding 都打上了@Deprecated 注解。后续的版本更新中会逐渐替换成函数式的方式实现。 既然通过四大函数式接口的方式替换了注解的方式 那么
该如何进行绑定呢?通过:spring.cloud.stream.function.definition: 名称 的方式进行绑定 公开topic。不管是创建 Consumer 还是 Supplier 或者是 Function Stream都会将其的 方法名称 进行 一个topic 拆封 和 绑定 假设 创建了一个 Consumer< String > myTopic 的方法,Stream 会将其 拆分成 in 和 out 两个通道 input - < functionName > + -in- + < index > output - <
functionName > + -out- + < index > 格式拆分 myTopic-in-0 myTopic-out-0

2. 项目演练

spring boot用的是2.7.0的

2.1 引用依赖

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

2.2 修改配置文件

server:port: 8080
# rabbitmq 消费者配置
spring:rabbitmq:host: localhost  # rabbitmq服务地址port: 5672username: guestpassword: guestcloud:stream:bindings:testSupplier-out-0: # 生产者配置content-type: application/jsondestination: demo-destination #交换机binder: rabbit # mq类型testConsumer-in-0: # 消费者配置content-type: application/jsondestination: demo-destination #交换机group: demo-group #消费者分组binder: rabbittestSupplier1-out-0: # 生产者配置content-type: application/jsondestination: demo1-destinationbinder: rabbittestFunction-in-0: # 消费者配置content-type: application/jsondestination: demo1-destinationgroup: demo1-groupbinder: rabbittestFunction-out-0: # 生产者配置content-type: application/jsondestination: demo2-destinationbinder: rabbittestConsumer1-in-0: # 消费者配置content-type: application/jsondestination: demo2-destinationgroup: demo2-groupbinder: rabbitfunction:definition:  testSupplier;testConsumer;testSupplier1;testFunction;testConsumer1; # 绑定

2.3 具体使用

2.3.1 自动发送消息

修改配置文件
在这里插入图片描述
在这里插入图片描述
定义生产者bean

  /*** 注意方法名称 testSupplier 要与配置文件中的spring.cloud.stream.bindings.testSupplier-out-0 保持一致* 其中 -out-0 是固定写法,out 标识生产者类型,0是生产者索引*/@Beanpublic Supplier<Person> testSupplier() {return ()->{Person person = new Person();person.setName("zhang");System.out.println("testSupplier生产消息:"+person);return person;};}

使用Supplier函数作为生产者,这个生产者,会一直自动生产消息。
在这里插入图片描述

定义消费者bean

 /*** 注意方法名称 testConsumer 要与配置文件中的spring.cloud.stream.bindings.testConsumer-in-0 保持一致* 其中 -in-0 是固定写法,in 标识消费者类型,0是消费者索引*/@Beanpublic Consumer<Person> testConsumer() {return msg -> {System.out.println("testConsumer消费消息: " + msg);};}

使用Consumer函数作为消费者,是自动检测的,只要队列中有数据就会取出来消费,本项目中该消费者配置如下:

    testConsumer-in-0: # 消费者配置content-type: application/jsondestination: demo-destination #交换机group: demo-group #消费者分组binder: rabbit

该消费者会一直监控队列destination.group ,也就是demo-destination.demo-group
在这里插入图片描述
在这里插入图片描述

2.3.2 手动发送消息

只绑定消费者,生产者不绑定,其他的和自动发送消息一样不变
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
发送消息

    @GetMapping("sendMsg")public String sendMsg(){Person person = new Person();person.setName("controller测试");Message<Person> message = MessageBuilder.withPayload(person).build();// 发送消息streamBridge.send("testSupplier-out-0", message);return "发送成功";}

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

2.3.3 加工消息

  1. 修改配置文件
    在这里插入图片描述
  2. 生产者定义
   @Beanpublic Supplier<Person> testSupplier1() {return ()->{Person person = new Person();person.setName("测试function");System.out.println("testSupplier1生产消息:"+person);return person;};}
  1. 消费者定义
    @Beanpublic Consumer<Person> testConsumer1() {return msg -> {System.out.println("testConsumer1消费消息: " + msg);};}
  1. 加工funtion定义
    @Beanpublic Function<Person, Person> testFunction() {return msg -> {msg.setName(msg.getName()+"_加工消息");return msg;};}
  1. 结果
    在这里插入图片描述

3 项目源码

3.1 pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.0</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.zcl</groupId><artifactId>rabitMQDemo</artifactId><version>0.0.1-SNAPSHOT</version><name>rabitMQDemo</name><description>rabitMQDemo</description><properties><java.version>1.8</java.version><spring-cloud.version>2021.0.3</spring-cloud.version></properties><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-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

3.2 application.yaml

server:port: 8080
--- # rabbitmq 消费者配置
spring:rabbitmq:host: localhostport: 5672username: guestpassword: guestcloud:stream:bindings:testSupplier-out-0:content-type: application/jsondestination: demo-destinationgroup: demo-groupbinder: rabbittestConsumer-in-0:content-type: application/jsondestination: demo-destinationgroup: demo-groupbinder: rabbittestSupplier1-out-0:content-type: application/jsondestination: demo1-destinationgroup: demo1-groupbinder: rabbittestFunction-in-0:content-type: application/jsondestination: demo1-destinationgroup: demo1-groupbinder: rabbittestFunction-out-0:content-type: application/jsondestination: demo2-destinationgroup: demo2-groupbinder: rabbittestConsumer1-in-0:content-type: application/jsondestination: demo2-destinationgroup: demo2-groupbinder: rabbitfunction:definition: testSupplier1;testFunction;testConsumer1;

3.3 RabbitMqComponent.java

package com.zcl.component;import com.zcl.RabitMqDemoApplication;
import com.zcl.entity.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;@Component
public class RabbitMqComponent {/*** 注意方法名称 testConsumer 要与配置文件中的spring.cloud.stream.bindings.testConsumer-in-0 保持一致* 其中 -in-0 是固定写法,in 标识消费者类型,0是消费者索引*/@Beanpublic Consumer<Person> testConsumer() {return msg -> {System.out.println("testConsumer消费消息: " + msg);};}/*** 注意方法名称 testSupplier 要与配置文件中的spring.cloud.stream.bindings.testSupplier-out-0 保持一致* 其中 -out-0 是固定写法,out 标识生产者类型,0是生产者索引*/@Beanpublic Supplier<Person> testSupplier() {return ()->{Person person = new Person();person.setName("zhang");System.out.println("testSupplier生产消息:"+person);return person;};}@Beanpublic Supplier<Person> testSupplier1() {return ()->{Person person = new Person();person.setName("测试function");System.out.println("testSupplier1生产消息:"+person);return person;};}@Beanpublic Function<Person, Person> testFunction() {return msg -> {msg.setName(msg.getName()+"_加工消息");return msg;};}@Beanpublic Consumer<Person> testConsumer1() {return msg -> {System.out.println("testConsumer1消费消息: " + msg);};}
}

这篇关于SpringCloudStream 3.x rabbit 使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/942585

相关文章

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

Linux join命令的使用及说明

《Linuxjoin命令的使用及说明》`join`命令用于在Linux中按字段将两个文件进行连接,类似于SQL的JOIN,它需要两个文件按用于匹配的字段排序,并且第一个文件的换行符必须是LF,`jo... 目录一. 基本语法二. 数据准备三. 指定文件的连接key四.-a输出指定文件的所有行五.-o指定输出

Linux jq命令的使用解读

《Linuxjq命令的使用解读》jq是一个强大的命令行工具,用于处理JSON数据,它可以用来查看、过滤、修改、格式化JSON数据,通过使用各种选项和过滤器,可以实现复杂的JSON处理任务... 目录一. 简介二. 选项2.1.2.2-c2.3-r2.4-R三. 字段提取3.1 普通字段3.2 数组字段四.

Linux kill正在执行的后台任务 kill进程组使用详解

《Linuxkill正在执行的后台任务kill进程组使用详解》文章介绍了两个脚本的功能和区别,以及执行这些脚本时遇到的进程管理问题,通过查看进程树、使用`kill`命令和`lsof`命令,分析了子... 目录零. 用到的命令一. 待执行的脚本二. 执行含子进程的脚本,并kill2.1 进程查看2.2 遇到的

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

k8s按需创建PV和使用PVC详解

《k8s按需创建PV和使用PVC详解》Kubernetes中,PV和PVC用于管理持久存储,StorageClass实现动态PV分配,PVC声明存储需求并绑定PV,通过kubectl验证状态,注意回收... 目录1.按需创建 PV(使用 StorageClass)创建 StorageClass2.创建 PV

Redis 基本数据类型和使用详解

《Redis基本数据类型和使用详解》String是Redis最基本的数据类型,一个键对应一个值,它的功能十分强大,可以存储字符串、整数、浮点数等多种数据格式,本文给大家介绍Redis基本数据类型和... 目录一、Redis 入门介绍二、Redis 的五大基本数据类型2.1 String 类型2.2 Hash

Redis中Hash从使用过程到原理说明

《Redis中Hash从使用过程到原理说明》RedisHash结构用于存储字段-值对,适合对象数据,支持HSET、HGET等命令,采用ziplist或hashtable编码,通过渐进式rehash优化... 目录一、开篇:Hash就像超市的货架二、Hash的基本使用1. 常用命令示例2. Java操作示例三

Linux创建服务使用systemctl管理详解

《Linux创建服务使用systemctl管理详解》文章指导在Linux中创建systemd服务,设置文件权限为所有者读写、其他只读,重新加载配置,启动服务并检查状态,确保服务正常运行,关键步骤包括权... 目录创建服务 /usr/lib/systemd/system/设置服务文件权限:所有者读写js,其他