生产者
编写消息生产者
导入依赖
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springboot_rabbitmq_producer</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
</parent>
<dependencies>
<!--rabbitmq依赖-->
<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>
</dependencies>
</project>
resources配置文件
application.yml
# 配置rabbitmq的基本信息
spring:
rabbitmq:
host: ip
port: 5672
username: admin
password: 123456
virtual-host: /
java文件
ProducerApplication
package li.chen.com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class);
}
}
RabbitMQConfig
package li.chen.com.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_queue_exchange";
// 1 交换机
@Bean("bootException")
public Exchange bootException(){
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}
//2 队列
@Bean("bootQueue")
public Queue bootQueue(){
return QueueBuilder.durable(QUEUE_NAME).build();
}
// 3 队列和交换机绑定关系 Binding
@Bean
public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue,
@Qualifier("bootException") Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
}
}
test测试文件
ProducerTest
package li.chen.com;
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;
import li.chen.com.config.RabbitMQConfig;
@SpringBootTest
@RunWith(SpringRunner.class)
public class ProducerTest {
//1 注入RabbitTemple
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void TestSend(){
rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.txt","boot mq hell....");
}
@Test
public void TestContextLoads() {
rabbitTemplate.convertAndSend("hello", "hello word");
}
}
执行结果:


消费者
编写消息消费者
导入依赖
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springboot_rabbitmq_consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
</parent>
<dependencies>
<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>
</dependencies>
</project>
resources配置文件
application.yml
# 配置rabbitmq的基本信息
spring:
rabbitmq:
host: ip
port: 5672
username: admin
password: 123456
virtual-host: /
java消费者
RabbitMQListener
package li.chen.com.controller;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
nt
public class RabbitMQListener {
@RabbitListener(queues = "boot_queue_exchange")
public void ListenerQueue(Message message){
System.out.println(message);
}
}
ConsumerApplication
package li.chen.com;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableRabbit //开启基于注解的rabbitMQ
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
运行
运行ProducerTest

运行ConsumerApplication

该博客详细介绍了如何在Spring Boot应用中集成并使用RabbitMQ,包括生产者和消费者的配置。首先,展示了在pom.xml中添加必要的依赖,然后在application.yml中配置RabbitMQ的连接信息。接着,创建了交换机、队列和绑定,并通过RabbitTemplate发送消息。在测试类中,实现了生产者发送消息的测试用例。最后,创建了消费者监听特定队列并接收消息的方法。整个过程详细且实用,为读者提供了完整的Spring Boot与RabbitMQ整合的实践指南。
1251

被折叠的 条评论
为什么被折叠?



