一、大概步骤
spring boot项目中只需要引入对应的amqp启动器依赖即可,方便的使用RabbitTemplate发送消息,使用注解接收消息。
一般在开发过程中:
生产者工程:
1. application.yml文件配置RabbitMQ相关信息;
2. 在生产者工程中编写配置类,用于创建交换机和队列,并进行绑定
3. 注入RabbitTemplate对象,通过RabbitTemplate对象发送消息到交换机
消费者工程:
1. application.yml文件配置RabbitMQ相关信息
2. 创建消息处理类,用于接收队列中的消息并进行处理
二、生产者
创建项目producer
2.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 http://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.1.5.RELEASE</version>
</parent>
<groupId>org.example</groupId>
<artifactId>producer</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</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-test</artifactId>
</dependency>
</dependencies>
</project>
注意:不能没有
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
2.2 配置启动类
package xiaoxu.rabbit;
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);
}
}
2.3 配置application.yml
spring:
rabbitmq:
host: localhost
virtual-host: /rabbit
username: rabbit
password: rabbit
port: 5672
2.4 生产者配置类
package xiaoxu.rabbit.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 XU_QUEUE = "xu_queue";
public static final String XU_TOPIC_EXCHANGE = "xu_topic_exchange";
//1、队列
@Bean("xuQueue")
public Queue xuQueue(){
return QueueBuilder.durable(XU_QUEUE).build();
}
//2、交换机
@Bean("xuTopicExchange")
public Exchange topicExchange(){
return ExchangeBuilder.topicExchange(XU_TOPIC_EXCHANGE).durable(true).build();
}
//3、绑定队列到交换机
@Bean
public Binding xuQueueExchange(@Qualifier("xuQueue") Queue queue,
@Qualifier("xuTopicExchange") Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("xu.#").noargs();
}
}
注意:各个注解的作用
三、消费者
创建消费者consumer项目
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.5.RELEASE</version>
</parent>
<groupId>org.example</groupId>
<artifactId>consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
</project>
3.2 启动类
package xiaoxu.consumer.rabbit;
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,args);
}
}
3.3 application.yml
spring:
rabbitmq:
host: localhost
virtual-host: /rabbit
username: rabbit
password: rabbit
port: 5672
3.4 消费者监听配置类
package xiaoxu.consumer.rabbit.config;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class MyListener {
@RabbitListener(queues = "xu_queue") //监听队列为 xu_queue
public void myListener1(String message){
System.out.println("消费者接收到的消息为:" + message);
}
}
四、测试配置类(producer)
import javafx.application.Application;
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 xiaoxu.rabbit.ProducerApplication;
import xiaoxu.rabbit.config.RabbitMQConfig;
@RunWith(SpringRunner.class)//@Autowired自动注入的类才会生效
@SpringBootTest(classes = ProducerApplication.class) //测试注解
public class RabbitMQTest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void test(){
//创建3条消息
rabbitTemplate.convertAndSend(RabbitMQConfig.XU_TOPIC_EXCHANGE,"xu.insert","商品新增,routing key 为xu.insert");
rabbitTemplate.convertAndSend(RabbitMQConfig.XU_TOPIC_EXCHANGE,"xu.update","商品新增,routing key 为xu.update");
rabbitTemplate.convertAndSend(RabbitMQConfig.XU_TOPIC_EXCHANGE,"xu.delete","商品新增,routing key 为xu.delete");
}
}
注意:
生产者:
编写的测试类RabbitMQTest,利用RabbitTemplate发送3条消息,这3条消息的路由key分别是item.insert,item.update,item.delete
消费者:在IDEA控制台查看是否能接收到符合路由key的消息