Spring Cloud Stream 4.0.4 rabbitmq 发送消息多function

这篇具有很好参考价值的文章主要介绍了Spring Cloud Stream 4.0.4 rabbitmq 发送消息多function。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

使用 idea 创建 Springboot 项目

please use 'spring.cloud.function.definition' property to explicitly define,Spring,rabbitmq,spring cloud

添加 Spring cloud stream 和 rabbitmq 依赖

please use 'spring.cloud.function.definition' property to explicitly define,Spring,rabbitmq,spring cloud

pom文件

<?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>3.2.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springcloudstream-demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloudstream-demo1</name>
    <description>springcloudstream-demo1</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2023.0.0-RC1</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-web</artifactId>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-test-binder</artifactId>
            <scope>test</scope>
        </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>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

修改配置文件

--- # rabbitmq 消费者配置
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: ruoyi
    password: ruoyi123
  cloud:
    stream:
      rabbit:
        bindings:
          demo2-in-0:
            consumer:
              delayed-exchange: true
      bindings:
        demo-in-0:
          content-type: application/json
          destination: demo-destination
          group: demo-group
          binder: rabbit
        demo1-in-0:
          content-type: application/json
          destination: demo1-destination
          group: demo1-group
          binder: rabbit
        demo2-in-0:
          content-type: application/json
          destination: demo2-destination
          group: demo2-group
          binder: rabbit
    function:
      definition: demo;demo1;demo2

创建消费者

package com.example.springcloudstreamdemo1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.function.Consumer;

@SpringBootApplication
public class SpringcloudstreamDemo1Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringcloudstreamDemo1Application.class, args);
    }
 	 /**
     * 注意方法名称 demo 要与配置文件中的spring.cloud.stream.bindings.demo-in-0 保持一致
     * 其中 -in-0 是固定写法,in 标识消费者类型,0是消费者索引
     */
    @Bean
    public Consumer<Person> demo() {
        return person -> {
            System.out.println("Received: " + person);
        };
    }
    @Bean
    public Consumer<String> demo1() {
        return msg -> {
            System.out.println("Received: " + msg);
        };
    }
    @Bean
    public Consumer<Person> demo2() {
        return msg -> {
            System.out.println("Received: " + msg);
        };
    }

    public static class Person {
        private String name;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String toString() {
            return this.name;
        }
    }
}

注意当多个消费者时,需要添加配置项:spring.cloud.function.definition
please use 'spring.cloud.function.definition' property to explicitly define,Spring,rabbitmq,spring cloud

启动项目

启动日志
please use 'spring.cloud.function.definition' property to explicitly define,Spring,rabbitmq,spring cloud
please use 'spring.cloud.function.definition' property to explicitly define,Spring,rabbitmq,spring cloud

查看 mq 控制台

交换机信息

please use 'spring.cloud.function.definition' property to explicitly define,Spring,rabbitmq,spring cloud
交换机名称对应:
spring.cloud.stream.bindings.demo-in-0.destination配置项的值

队列信息

please use 'spring.cloud.function.definition' property to explicitly define,Spring,rabbitmq,spring cloud

  • 队列名称是交换机名称+分组名

添加生产者配置

--- #生产者配置
spring:
  cloud:
    stream:
      rabbit:
        bindings:
          demo2-out-0:
            producer:
              delayedExchange: true #设置为延迟队列
      bindings:
        demo-out-0:
          content-type: application/json
          destination: demo-destination  # 同消费者保持一致
          binder: rabbit
        demo1-out-0:
          content-type: application/json
          destination: demo1-destination
          binder: rabbit
        demo2-out-0:
          content-type: application/json
          destination: demo2-destination
          binder: rabbit

创建消息生产者

package com.example.springcloudstreamdemo1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProducerController
{
    @Autowired
    private StreamBridge streamBridge;


    @GetMapping("sendMsg")
    public String sendMsg(int delay, String name){

        //delay 延时时间毫秒
        SpringcloudstreamDemo1Application.Person person = new SpringcloudstreamDemo1Application.Person();
        person.setName(name);
        Message<SpringcloudstreamDemo1Application.Person> message = MessageBuilder.withPayload(person)
                .setHeader("x-delay", delay).build();
        // 发送延时消息
        streamBridge.send("demo2-out-0", message);

        streamBridge.send("demo1-out-0", person);

        streamBridge.send("demo-out-0", person);

        return "发送成功";
    }
}



启动测试

发送消息

http://localhost:8080/sendMsg?delay=10000&name=zhangsan

打印消息

please use 'spring.cloud.function.definition' property to explicitly define,Spring,rabbitmq,spring cloud

问题总结

问题一

Multiple functional beans were found [*,*], thus can't determine default function definition. Please use 'spring.cloud.function.definition' property to explicitly define it.

解决办法:
查看配置是否正确:
spring.cloud.function.definition
please use 'spring.cloud.function.definition' property to explicitly define,Spring,rabbitmq,spring cloud文章来源地址https://www.toymoban.com/news/detail-825721.html

到了这里,关于Spring Cloud Stream 4.0.4 rabbitmq 发送消息多function的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Spring云原生系列】SpringBoot+Spring Cloud Stream:消息驱动架构(MDA)解析,实现异步处理与解耦合!

    🎉🎉 欢迎光临,终于等到你啦 🎉🎉 🏅我是 苏泽 ,一位对技术充满热情的探索者和分享者。🚀🚀 🌟持续更新的专栏 《Spring 狂野之旅:从入门到入魔》 🚀 本专栏带你从Spring入门到入魔   这是苏泽的个人主页可以看到我其他的内容哦👇👇 努力的苏泽 http://suzee.blog.

    2024年03月10日
    浏览(39)
  • spring cloud steam 整合kafka 进行消息发送与接收

    spring cloud steam : Binder和Binding Binder是SpringCloud Stream的一个抽象概念,是应用与消息中间件之间的粘合剂,目前SpringCloud Stream实现了Kafka和RabbitMQ的binder Binder可以生成Binding,Binding用来绑定消息容器的生产者和消费者,它有两种类型,INPUT和OUTPUT,INPUT对应于消费者,OUTPUT对应于

    2024年02月10日
    浏览(28)
  • Spring Boot 中的 RabbitMQ 消息发送配置

    RabbitMQ 是一个开源的消息代理系统,它实现了 AMQP(高级消息队列协议)标准,并支持多种消息传输协议。它具有高可用性、可扩展性和可靠性,广泛应用于分布式系统、微服务架构、异步任务处理、日志收集等场景。 RabbitMQ 的核心概念包括: Producer:消息生产者,负责将消

    2024年02月07日
    浏览(33)
  • Spring Cloud 项目中实现推送消息到 RabbitMQ 消息中间件

    (注:安装在虚拟机则填虚拟机地址,否则则为本机地址) 用户名和密码都为guest 看到如下页面则为RabbitMQ安装登录成功。 三、依赖注入 导入依赖坐标 四、配置yaml文件 配置yaml配置文件 (注:host为地址,如果安装在虚拟机则为虚拟机地址,安装在本机则本机地址。port为端

    2024年04月13日
    浏览(44)
  • 在Spring Cloud中使用RabbitMQ完成一个消息驱动的微服务

    Spring Cloud系列目前已经有了Spring Cloud五大核心组件:分别是,Eureka注册中心,Zuul网关,Hystrix熔断降级,openFeign声明式远程调用,ribbon负载均衡。这五个模块,对了,有没有发现,其实我这五个模块中ribbon好像还没有案例例举,目前只有一个Ribbon模块的搭建,后边我会完善的

    2024年02月04日
    浏览(34)
  • spring cloud 搭建消息中间件 RabbitMQ 环境、Mac/Windows下载安装RabbitMQ、配置RabbitMQ环境变量

    spring boot、spring cloud工程:Mac/Windows下载安装Erlang、RabbitMQ,并配置环境变量。 这里学习如何安装 RabbitMQ,因为远程配置中心的动态更新需要结合 RabbitMQ 来使用。 这里给出自己下载和使用的百度网盘链接:Erlang 版本为25.3.2、RabbitMQ版本为3.12.1 : 链接:百度网盘链接 提取码:

    2024年02月15日
    浏览(59)
  • Spring RabbitMQ那些事(1-交换机配置&消息发送订阅实操)

    在上一节 RabbitMQ中的核心概念和交换机类型 中我们介绍了RabbitMQ中的一些核心概念,尤其是各种交换机的类型,接下来我们将具体讲解各种交换机的配置和消息订阅实操。 我们先上应用启动配置文件 application.yml ,如下: 备注:这里我们指定了 RabbitListenerContainerFactory 的类型

    2024年01月17日
    浏览(30)
  • Sprint Cloud Stream整合RocketMq和websocket实现消息发布订阅

    1. 引入RocketMQ依赖 :首先,在 pom.xml 文件中添加RocketMQ的依赖: 2. 配置RocketMQ连接信息 :在 application.properties 或 application.yml 中配置RocketMQ的连接信息,包括Name Server地址等: 3.消息发布组件 4.消息发布控制器 项目结构: 接下来是websocket模块的搭建 1. 依赖添加 2.application.yml配

    2024年02月08日
    浏览(27)
  • Spring Cloud Stream集成Kafka

    Spring Cloud Stream是一个构建消息驱动微服务的框架,抽象了MQ的使用方式, 提供统一的API操作。Spring Cloud Stream通过Binder(绑定器)、inputs/outputs Channel完成应用程序和MQ的解耦。 Binder 负责绑定应用程序和MQ中间件,即指定应用程序是和KafKa交互还是和RabbitMQ交互或者和其他的MQ中间件交

    2024年02月13日
    浏览(39)
  • 将AWS iot消息数据发送Kinesis Firehose Stream存向S3

    观看此文章之前,请先学习AWS iot的数据收集: 使用Linux SDK客户端向AWS Iot发送数据-CSDN博客 1.1 规则 规则可让您的设备与 AWS 服务进行交互。分析规则并根据物品发送的消息执行操作。您可以使用规则来支持任务,例如补充或筛选从设备接收的数据。 1.2 目的地 目的地是定义规

    2024年01月25日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包