生产者和消费者的依赖基本一致:
注意工程名称不是一样的
<?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)Javajdk1.8.0_291injava.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always "-javaagent:C:Program FilesJetBrainsIntelliJ IDEA 2021.2.1libidea_rt.jar=56311:C:Program FilesJetBrainsIntelliJ IDEA 2021.2.1in" -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)Javajdk1.8.0_291jrelibcharsets.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibdeploy.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibextaccess-bridge-32.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibextcldrdata.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibextdnsns.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibextjaccess.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibextjfxrt.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibextlocaledata.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibext ashorn.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibextsunec.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibextsunjce_provider.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibextsunmscapi.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibextsunpkcs11.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibextzipfs.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibjavaws.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibjce.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibjfr.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibjfxswt.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibjsse.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibmanagement-agent.jar;C:Program Files (x86)Javajdk1.8.0_291jrelibplugin.jar;C:Program Files (x86)Javajdk1.8.0_291jrelib esources.jar;C:Program Files (x86)Javajdk1.8.0_291jrelib t.jar;C:UsersAdministratorIdeaProjectsRabbitMQSpringBoot-RabbitMQ-Consumer argetclasses;C:UsersAdministrator.m2 epositoryorgspringframeworkootspring-boot-starter-amqp2.5.5spring-boot-starter-amqp-2.5.5.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkootspring-boot-starter2.5.5spring-boot-starter-2.5.5.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkootspring-boot2.5.5spring-boot-2.5.5.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkootspring-boot-autoconfigure2.5.5spring-boot-autoconfigure-2.5.5.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkootspring-boot-starter-logging2.5.5spring-boot-starter-logging-2.5.5.jar;C:UsersAdministrator.m2 epositorychqoslogbacklogback-classic1.2.6logback-classic-1.2.6.jar;C:UsersAdministrator.m2 epositorychqoslogbacklogback-core1.2.6logback-core-1.2.6.jar;C:UsersAdministrator.m2 epositoryorgapachelogginglog4jlog4j-to-slf4j2.14.1log4j-to-slf4j-2.14.1.jar;C:UsersAdministrator.m2 epositoryorgapachelogginglog4jlog4j-api2.14.1log4j-api-2.14.1.jar;C:UsersAdministrator.m2 epositoryorgslf4jjul-to-slf4j1.7.32jul-to-slf4j-1.7.32.jar;C:UsersAdministrator.m2 epositoryjakartaannotationjakarta.annotation-api1.3.5jakarta.annotation-api-1.3.5.jar;C:UsersAdministrator.m2 epositoryorgyamlsnakeyaml1.28snakeyaml-1.28.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkspring-messaging5.3.10spring-messaging-5.3.10.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkspring-beans5.3.10spring-beans-5.3.10.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkamqpspring-rabbit2.3.10spring-rabbit-2.3.10.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkamqpspring-amqp2.3.10spring-amqp-2.3.10.jar;C:UsersAdministrator.m2 epositoryorgspringframework etryspring-retry1.3.1spring-retry-1.3.1.jar;C:UsersAdministrator.m2 epositoryjavaxannotationjavax.annotation-api1.3.2javax.annotation-api-1.3.2.jar;C:UsersAdministrator.m2 epositorycom abbitmqamqp-client5.12.0amqp-client-5.12.0.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkspring-context5.3.10spring-context-5.3.10.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkspring-aop5.3.10spring-aop-5.3.10.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkspring-expression5.3.10spring-expression-5.3.10.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkspring-tx5.3.10spring-tx-5.3.10.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkootspring-boot-starter-test2.5.5spring-boot-starter-test-2.5.5.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkootspring-boot-test2.5.5spring-boot-test-2.5.5.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkootspring-boot-test-autoconfigure2.5.5spring-boot-test-autoconfigure-2.5.5.jar;C:UsersAdministrator.m2 epositorycomjaywayjsonpathjson-path2.5.0json-path-2.5.0.jar;C:UsersAdministrator.m2 epository etminidevjson-smart2.4.7json-smart-2.4.7.jar;C:UsersAdministrator.m2 epository etminidevaccessors-smart2.4.7accessors-smart-2.4.7.jar;C:UsersAdministrator.m2 epositoryorgow2asmasm9.1asm-9.1.jar;C:UsersAdministrator.m2 epositoryorgslf4jslf4j-api1.7.32slf4j-api-1.7.32.jar;C:UsersAdministrator.m2 epositoryjakartaxmlindjakarta.xml.bind-api2.3.3jakarta.xml.bind-api-2.3.3.jar;C:UsersAdministrator.m2 epositoryjakartaactivationjakarta.activation-api1.2.2jakarta.activation-api-1.2.2.jar;C:UsersAdministrator.m2 epositoryorgassertjassertj-core3.19.0assertj-core-3.19.0.jar;C:UsersAdministrator.m2 epositoryorghamcresthamcrest2.2hamcrest-2.2.jar;C:UsersAdministrator.m2 epositoryorgjunitjupiterjunit-jupiter5.7.2junit-jupiter-5.7.2.jar;C:UsersAdministrator.m2 epositoryorgjunitjupiterjunit-jupiter-api5.7.2junit-jupiter-api-5.7.2.jar;C:UsersAdministrator.m2 epositoryorgapiguardianapiguardian-api1.1.0apiguardian-api-1.1.0.jar;C:UsersAdministrator.m2 epositoryorgopentest4jopentest4j1.2.0opentest4j-1.2.0.jar;C:UsersAdministrator.m2 epositoryorgjunitplatformjunit-platform-commons1.7.2junit-platform-commons-1.7.2.jar;C:UsersAdministrator.m2 epositoryorgjunitjupiterjunit-jupiter-params5.7.2junit-jupiter-params-5.7.2.jar;C:UsersAdministrator.m2 epositoryorgjunitjupiterjunit-jupiter-engine5.7.2junit-jupiter-engine-5.7.2.jar;C:UsersAdministrator.m2 epositoryorgjunitplatformjunit-platform-engine1.7.2junit-platform-engine-1.7.2.jar;C:UsersAdministrator.m2 epositoryorgmockitomockito-core3.9.0mockito-core-3.9.0.jar;C:UsersAdministrator.m2 epository etytebuddyyte-buddy1.10.22yte-buddy-1.10.22.jar;C:UsersAdministrator.m2 epository etytebuddyyte-buddy-agent1.10.22yte-buddy-agent-1.10.22.jar;C:UsersAdministrator.m2 epositoryorgobjenesisobjenesis3.2objenesis-3.2.jar;C:UsersAdministrator.m2 epositoryorgmockitomockito-junit-jupiter3.9.0mockito-junit-jupiter-3.9.0.jar;C:UsersAdministrator.m2 epositoryorgskyscreamerjsonassert1.5.0jsonassert-1.5.0.jar;C:UsersAdministrator.m2 epositorycomvaadinexternalgoogleandroid-json .0.20131108.vaadin1android-json-0.0.20131108.vaadin1.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkspring-core5.3.10spring-core-5.3.10.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkspring-jcl5.3.10spring-jcl-5.3.10.jar;C:UsersAdministrator.m2 epositoryorgspringframeworkspring-test5.3.10spring-test-5.3.10.jar;C:UsersAdministrator.m2 epositoryorgxmlunitxmlunit-core2.8.2xmlunit-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:UsersAdministratorIdeaProjectsRabbitMQSpringBoot-RabbitMQ-Consumer argetclasses started by Administrator in C:UsersAdministratorIdeaProjectsRabbitMQ) 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])