生产者和消费者的依赖基本一致:

注意工程名称不是一样的

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>SpringBoot-RabbitMQ-Producer</artifactId> <dependencies>
<!--amqp的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!--单元测试类-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

</dependencies> <properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties> </project>

配置信息也是一致的rabbitmq.properties:

# rabbitmq 信息
spring:
rabbitmq:
host: 192.168.2.121
port: 5672
username: test
password: 123456
virtual-host: /dzz

编写SpringBoot生产者服务:

启动类:

package cn.dzz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class Producer { public static void main(String[] args) {
SpringApplication.run(Producer.class);
}
}

RabbitMQ配置类:

package cn.dzz.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class RabbitMqConfig { public static final String EXCHANGE_NAME = "boot-topic-exchange";
public static final String QUEUE_NAME = "boot-topic-queue"; // 注入队列
@Bean("bootQueue")
public Queue springBootQueue() {
return QueueBuilder.durable(QUEUE_NAME).build();
} // 注入交换机
@Bean("bootTopicExchange")
public Exchange springBootExchange() {
return ExchangeBuilder
.topicExchange(EXCHANGE_NAME)
.durable(true)
.build();
} // 交换机与队列绑定
@Bean
public Binding bindingExchangeAndQueue(@Qualifier("bootQueue") Queue q, @Qualifier("bootTopicExchange") Exchange e) {
return BindingBuilder
.bind(q) // 绑定哪个队列
.to(e) // 给哪个交换机
.with("boot.#") // 路由分配方式
.noargs(); // 声明无参数
} }

测试类:

import cn.dzz.Producer;
import cn.dzz.config.RabbitMqConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest(classes = Producer.class)
@RunWith(SpringRunner.class
)
public class ProducerTest { @Autowired
private RabbitTemplate rabbitTemplate; @Test
public void sendMessage() {
rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_NAME, "boot.info", " 发送出去消息。。。。 ");
} }

执行完测试类之后,检查RabbitMQ面板上面是否还有信息:

编写SpringBoot消费者服务:

编写启动类:

package cn.dzz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class);
}
}

编写接收的监听器类:

package cn.dzz;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component; @Component
public class RabbitMqListener { @RabbitListener(queues = "boot-topic-queue")
public void
ListenerQueue(Message message) {
System.out.println(message);
}

}

IDEA这里还提示出结构信息:

打开可以看见:

执行启动类查看消息接收:

"C:\Program Files (x86)\Java\jdk1.8.0_291\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2021.2.1\lib\idea_rt.jar=56311:C:\Program Files\JetBrains\IntelliJ IDEA 2021.2.1\bin" -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\rt.jar;C:\Users\Administrator\IdeaProjects\RabbitMQ\SpringBoot-RabbitMQ-Consumer\target\classes;C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-starter-amqp\2.5.5\spring-boot-starter-amqp-2.5.5.jar;C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-starter\2.5.5\spring-boot-starter-2.5.5.jar;C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot\2.5.5\spring-boot-2.5.5.jar;C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.5.5\spring-boot-autoconfigure-2.5.5.jar;C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.5.5\spring-boot-starter-logging-2.5.5.jar;C:\Users\Administrator\.m2\repository\ch\qos\logback\logback-classic\1.2.6\logback-classic-1.2.6.jar;C:\Users\Administrator\.m2\repository\ch\qos\logback\logback-core\1.2.6\logback-core-1.2.6.jar;C:\Users\Administrator\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.14.1\log4j-to-slf4j-2.14.1.jar;C:\Users\Administrator\.m2\repository\org\apache\logging\log4j\log4j-api\2.14.1\log4j-api-2.14.1.jar;C:\Users\Administrator\.m2\repository\org\slf4j\jul-to-slf4j\1.7.32\jul-to-slf4j-1.7.32.jar;C:\Users\Administrator\.m2\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\Administrator\.m2\repository\org\yaml\snakeyaml\1.28\snakeyaml-1.28.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-messaging\5.3.10\spring-messaging-5.3.10.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-beans\5.3.10\spring-beans-5.3.10.jar;C:\Users\Administrator\.m2\repository\org\springframework\amqp\spring-rabbit\2.3.10\spring-rabbit-2.3.10.jar;C:\Users\Administrator\.m2\repository\org\springframework\amqp\spring-amqp\2.3.10\spring-amqp-2.3.10.jar;C:\Users\Administrator\.m2\repository\org\springframework\retry\spring-retry\1.3.1\spring-retry-1.3.1.jar;C:\Users\Administrator\.m2\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;C:\Users\Administrator\.m2\repository\com\rabbitmq\amqp-client\5.12.0\amqp-client-5.12.0.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-context\5.3.10\spring-context-5.3.10.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-aop\5.3.10\spring-aop-5.3.10.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-expression\5.3.10\spring-expression-5.3.10.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-tx\5.3.10\spring-tx-5.3.10.jar;C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-starter-test\2.5.5\spring-boot-starter-test-2.5.5.jar;C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-test\2.5.5\spring-boot-test-2.5.5.jar;C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-test-autoconfigure\2.5.5\spring-boot-test-autoconfigure-2.5.5.jar;C:\Users\Administrator\.m2\repository\com\jayway\jsonpath\json-path\2.5.0\json-path-2.5.0.jar;C:\Users\Administrator\.m2\repository\net\minidev\json-smart\2.4.7\json-smart-2.4.7.jar;C:\Users\Administrator\.m2\repository\net\minidev\accessors-smart\2.4.7\accessors-smart-2.4.7.jar;C:\Users\Administrator\.m2\repository\org\ow2\asm\asm\9.1\asm-9.1.jar;C:\Users\Administrator\.m2\repository\org\slf4j\slf4j-api\1.7.32\slf4j-api-1.7.32.jar;C:\Users\Administrator\.m2\repository\jakarta\xml\bind\jakarta.xml.bind-api\2.3.3\jakarta.xml.bind-api-2.3.3.jar;C:\Users\Administrator\.m2\repository\jakarta\activation\jakarta.activation-api\1.2.2\jakarta.activation-api-1.2.2.jar;C:\Users\Administrator\.m2\repository\org\assertj\assertj-core\3.19.0\assertj-core-3.19.0.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest\2.2\hamcrest-2.2.jar;C:\Users\Administrator\.m2\repository\org\junit\jupiter\junit-jupiter\5.7.2\junit-jupiter-5.7.2.jar;C:\Users\Administrator\.m2\repository\org\junit\jupiter\junit-jupiter-api\5.7.2\junit-jupiter-api-5.7.2.jar;C:\Users\Administrator\.m2\repository\org\apiguardian\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;C:\Users\Administrator\.m2\repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;C:\Users\Administrator\.m2\repository\org\junit\platform\junit-platform-commons\1.7.2\junit-platform-commons-1.7.2.jar;C:\Users\Administrator\.m2\repository\org\junit\jupiter\junit-jupiter-params\5.7.2\junit-jupiter-params-5.7.2.jar;C:\Users\Administrator\.m2\repository\org\junit\jupiter\junit-jupiter-engine\5.7.2\junit-jupiter-engine-5.7.2.jar;C:\Users\Administrator\.m2\repository\org\junit\platform\junit-platform-engine\1.7.2\junit-platform-engine-1.7.2.jar;C:\Users\Administrator\.m2\repository\org\mockito\mockito-core\3.9.0\mockito-core-3.9.0.jar;C:\Users\Administrator\.m2\repository\net\bytebuddy\byte-buddy\1.10.22\byte-buddy-1.10.22.jar;C:\Users\Administrator\.m2\repository\net\bytebuddy\byte-buddy-agent\1.10.22\byte-buddy-agent-1.10.22.jar;C:\Users\Administrator\.m2\repository\org\objenesis\objenesis\3.2\objenesis-3.2.jar;C:\Users\Administrator\.m2\repository\org\mockito\mockito-junit-jupiter\3.9.0\mockito-junit-jupiter-3.9.0.jar;C:\Users\Administrator\.m2\repository\org\skyscreamer\jsonassert\1.5.0\jsonassert-1.5.0.jar;C:\Users\Administrator\.m2\repository\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-core\5.3.10\spring-core-5.3.10.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-jcl\5.3.10\spring-jcl-5.3.10.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-test\5.3.10\spring-test-5.3.10.jar;C:\Users\Administrator\.m2\repository\org\xmlunit\xmlunit-core\2.8.2\xmlunit-core-2.8.2.jar" cn.dzz.ConsumerApplication

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.5) 2021-10-07 11:58:40.497 INFO 20732 --- [ main] cn.dzz.ConsumerApplication : Starting ConsumerApplication using Java 1.8.0_291 on DESKTOP-VA4SKMT with PID 20732 (C:\Users\Administrator\IdeaProjects\RabbitMQ\SpringBoot-RabbitMQ-Consumer\target\classes started by Administrator in C:\Users\Administrator\IdeaProjects\RabbitMQ)
2021-10-07 11:58:40.499 INFO 20732 --- [ main] cn.dzz.ConsumerApplication : No active profile set, falling back to default profiles: default
2021-10-07 11:58:41.389 INFO 20732 --- [ main] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [192.168.2.121:5672]
2021-10-07 11:58:41.410 INFO 20732 --- [ main] o.s.a.r.c.CachingConnectionFactory : Created new connection: rabbitConnectionFactory#ea1466:0/SimpleConnection@1b68ec6 [delegate=amqp://test@192.168.2.121:5672//dzz, localPort= 56322]
2021-10-07 11:58:41.440 INFO 20732 --- [ main] cn.dzz.ConsumerApplication : Started ConsumerApplication in 1.263 seconds (JVM running for 2.381)
(Body:' 发送出去消息。。。。 ' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=boot-topic-exchange, receivedRoutingKey=boot.info, deliveryTag=1, consumerTag=amq.ctag-XzTKbp4b1WJBhAf5GsSNkQ, consumerQueue=boot-topic-queue])

【RabbitMQ】07 SpringBoot整合RabbitMQ的更多相关文章

  1. RabbitMQ与SpringBoot整合

    RabbitMQ  SpringBoot  一.RabbitMQ的介绍 二.Direct模式 三.Topic转发模式 四.Fanout Exchange形式 原文地址: https://www.cnb ...

  2. springboot学习笔记-6 springboot整合RabbitMQ

    一 RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件.这些软件有很多,包括ActiveMQ(apache公司的),RocketMQ(阿 ...

  3. 【SpringBoot系列5】SpringBoot整合RabbitMQ

    前言: 因为项目需要用到RabbitMQ,前几天就看了看RabbitMQ的知识,记录下SpringBoot整合RabbitMQ的过程. 给出两个网址: RabbitMQ官方教程:http://www. ...

  4. SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...

  5. SpringBoot28 RabbitMQ知识点、Docker下载RabbitMQ、SpringBoot整合RabbtiMQ

    1 RabbitMQ知识点 1.1 整体架构图 消息生产者将消息投递到exchange中,exchange会以某种路由机制将生产者投递的消息路由到queue中,消息消费者再从queue中获取消息进行消 ...

  6. 一篇学习完rabbitmq基础知识,springboot整合rabbitmq

    一   rabbitmq 介绍 MQ全称为Message Queue,即消息队列, RabbitMQ是由erlang语言开发,基于AMQP(Advanced MessageQueue 高级消息队列协议 ...

  7. 【MQ中间件】RabbitMQ -- SpringBoot整合RabbitMQ(3)

    1.前言说明 前面一篇博客中提到了使用原生java代码进行测试RabbitMQ实现多种交换机类型的队列场景.但是在项目中我们一般使用SpringBoot项目,而且RabbitMQ天生对于Spring的 ...

  8. 功能:SpringBoot整合rabbitmq,长篇幅超详细

    SpringBoot整合rabbitMq 一.介绍 消息队列(Message Queue)简称mq,本文将介绍SpringBoot整合rabbitmq的功能使用 队列是一种数据结构,就像排队一样,遵循 ...

  9. springboot整合rabbitmq实现生产者消息确认、死信交换器、未路由到队列的消息

    在上篇文章  springboot 整合 rabbitmq 中,我们实现了springboot 和rabbitmq的简单整合,这篇文章主要是对上篇文章功能的增强,主要完成如下功能. 需求: 生产者在启 ...

  10. Springboot 整合RabbitMq ,用心看完这一篇就够了

    该篇文章内容较多,包括有rabbitMq相关的一些简单理论介绍,provider消息推送实例,consumer消息消费实例,Direct.Topic.Fanout的使用,消息回调.手动确认等. (但是 ...

随机推荐

  1. GeoGebra作圆的切线

    参考文档:<GeoGebra入门教程>唐家军 1. 目的 使用GeoGebra作出过一点的圆的切线. 2. 构造过程 文档种的描述如下: 按照上述构造过程,在输入条形框中依次输入上面的指令 ...

  2. ETL工具-nifi干货系列 第三讲 nifi web ui 使用教程

    1.nifi 服务启动之后,浏览器输入https://localhost:8443/nifi ,匿名登录或者输入用户名密码进入操作页面,如下图所示: 2.组件工具栏 处理器,鼠标放到图标上提示Proc ...

  3. vue中退出循环的方法

    forEachforEach不能使用break和continue.return也无法退出循环. 使用break,会报错(报错信息:SyntaxError: Illegal break statemen ...

  4. Xcode 配置账号

    Xcode 配置开发者账号 简介:在iOS 开发过程中,要打包到手机上是需要登陆账号和配置对应证书的. 配置账号 打开Xcode -> Preferences -> Accounts -& ...

  5. 非空处理 Java非空判断 非空处理及mysql数据库字段的not null

    1.mysql## 去掉非空,如果非空又没有默认值,这样程序在添加数据的时候i,如果没有设置值就会报错.该操作很危险.##ALTER TABLE `order_test` ADD COLUMN `te ...

  6. mybatis insert foreach批量添加

    mybatis insert foreach批量添加 int insertSelectiveBatch(List<ImageDetailEntity> myList); //写法1 < ...

  7. Android Verified Boot 2.0 AVB详解(基于Android P)

    原文地址:https://android.googlesource.com/platform/external/avb/+/master/ 译文地址:https://blog.csdn.net/sha ...

  8. OpenCV程序练习(三):图像运算

    一.图像加法运算 代码 import cv2 img=cv2.imread("demoimg.jpg",0) #读取图片,参数0等价于cv2.IMREAD_GRAYSCALE,将图 ...

  9. 价破天荒!99元国产ARM工业“评估板”再袭,14天限量抢购!

    上线即爆款!2000家企业选择! 凭借"79元超高性价比"."双核A7@1.2GHz"."国产化率100%"."ARM + DSP ...

  10. 「Pygors跨平台GUI」2:安装MinGW-w64、MSYS2还是WSL2

    「Pygors系列」一句话导读: MinGW-w64只有编译器,MSYS2带着更新环境,WSL2实用性比较高 历史与渊源   Windows平台 Linux平台 二进制兼容 WSL2:运行Linux程 ...