topic交换器与direct交换器在于direct交换器根据指定的路由key发送消息给相应的消息队列,而topic交换器在于根据路由key的值模糊匹配,发送到指定的消息队列
假设一个需求:这里模拟日志处理,对info、error级别设置两个消息队列,并设置一个all消息队列处理info、error、debug、warn级别的日志,三个服务用户、订单、产品分别发送消息并且通过info、error、all三个消息队列分别接受信息。
这里创建两个项目,一个为生产者,用来发生消息到,另一个为消费者,用来接受生产者发送的消息,并打印相应的日志级别
pom.xml
1.消费者
<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>1.5.21.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>per.czt</groupId>
<artifactId>rabbitmq-topic-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
App.java
package per.czt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
application.properties
spring.application.name=rabbitmq-direct-consumer
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#set exchange name
mq.config.exchange=log.topic
#info queue name
mq.config.queue.info=log.info
#error queue name
mq.config.queue.error=log.error
#all queue name
mq.config.queue.logs=log.all
ErrorReceiver.java
package per.czt.config;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/*
* 消息接收者
* @RabbitListener:
* bindings:绑定队列
* @QueueBinding:绑定队列的详细属性
* Queue:value 配置队列名称
* autoDelete:是否是可删除的临时队列
*
*/
@Component
@RabbitListener(
bindings=@QueueBinding(
value=@Queue(value="${mq.config.queue.error}",autoDelete="true"),
exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.TOPIC),
key="*.log.error"
)
)
public class ErrorReceiver {
/*
* 接受消息的方法,采用消息队列接收机制
*/
@RabbitHandler
public void process(String msg) {
System.out.println("....Error reveiver:"+msg);
}
}
InfoReceiver.java
package per.czt.config;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/*
* 消息接收者
* @RabbitListener:
* bindings:绑定队列
* @QueueBinding:绑定队列的详细属性
* Queue:value 配置队列名称
* autoDelete:是否是可删除的临时队列
*
*/
@Component
@RabbitListener(
bindings=@QueueBinding(
value=@Queue(value="${mq.config.queue.info}",autoDelete="true"),
exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.TOPIC),
key="*.log.info"
)
)
public class InfoReceiver {
/*
* 接受消息的方法,采用消息队列接收机制
*/
@RabbitHandler
public void process(String msg) {
System.out.println("....Info reveiver:"+msg);
}
}
LogsReceiver.java
package per.czt.config;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/*
* 消息接收者
* @RabbitListener:
* bindings:绑定队列
* @QueueBinding:绑定队列的详细属性
* Queue:value 配置队列名称
* autoDelete:是否是可删除的临时队列
*
*/
@Component
@RabbitListener(
bindings=@QueueBinding(
value=@Queue(value="${mq.config.queue.logs}",autoDelete="true"),
exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.TOPIC),
key="*.log.*"
)
)
public class LogsReceiver {
/*
* 接受消息的方法,采用消息队列接收机制
*/
@RabbitHandler
public void process(String msg) {
System.out.println("....All reveiver:"+msg);
}
}
2.生产者
pom.xml
<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>1.5.21.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>per.czt</groupId>
<artifactId>rabbitmq-topic-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
App.java
package per.czt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
application.properties
spring.application.name=rabbitmq-direct-provider
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#set exchange name
mq.config.exchange=log.topic
OrderSender.java
package per.czt.config;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/*
* 消息发送者
*/
@Component
public class OrderSender {
@Autowired
private AmqpTemplate amqpTemplate;
@Value("${mq.config.exchange}")
private String exchange;
/*
* 发生消息的方法
*/
public void send(String msg) {
//向消息队列发生消息
//参数一:交换器密匙
//参数二:路由健
//参数三:消息
amqpTemplate.convertAndSend(exchange,"order.log.info","order.log.info.."+msg);
amqpTemplate.convertAndSend(exchange,"order.log.debug","order.log.debug.."+msg);
amqpTemplate.convertAndSend(exchange,"order.log.warn","order.log.warn.."+msg);
amqpTemplate.convertAndSend(exchange,"order.log.error","order.log.error.."+msg);
}
}
ProductSender.java
package per.czt.config;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/*
* 消息发送者
*/
@Component
public class ProductSender {
@Autowired
private AmqpTemplate amqpTemplate;
@Value("${mq.config.exchange}")
private String exchange;
/*
* 发生消息的方法
*/
public void send(String msg) {
//向消息队列发生消息
//参数一:交换器密匙
//参数二:路由健
//参数三:消息
amqpTemplate.convertAndSend(exchange,"product.log.info","product.log.info.."+msg);
amqpTemplate.convertAndSend(exchange,"product.log.debug","product.log.debug.."+msg);
amqpTemplate.convertAndSend(exchange,"product.log.warn","product.log.warn.."+msg);
amqpTemplate.convertAndSend(exchange,"product.log.error","product.log.error.."+msg);
}
}
UserSender.java
package per.czt.config;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/*
* 消息发送者
*/
@Component
public class UserSender {
@Autowired
private AmqpTemplate amqpTemplate;
@Value("${mq.config.exchange}")
private String exchange;
/*
* 发生消息的方法
*/
public void send(String msg) {
//向消息队列发生消息
//参数一:交换器密匙
//参数二:路由健
//参数三:消息
amqpTemplate.convertAndSend(exchange,"user.log.info","user.log.info.."+msg);
amqpTemplate.convertAndSend(exchange,"user.log.debug","user.log.debug.."+msg);
amqpTemplate.convertAndSend(exchange,"user.log.warn","user.log.warn.."+msg);
amqpTemplate.convertAndSend(exchange,"user.log.error","user.log.error.."+msg);
}
}
QueueTest.java
package per.czt.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import per.czt.App;
import per.czt.config.OrderSender;
import per.czt.config.ProductSender;
import per.czt.config.UserSender;
/*
* 消息队列测试类
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes= {App.class})
public class QueueTest {
@Autowired
private UserSender userSender;
@Autowired
private ProductSender productSender;
@Autowired
private OrderSender orderSender;
@Test
public void test1() {
//发送消息
userSender.send("UserSender....");
productSender.send("ProductSender....");
orderSender.send("OrderSender....");
}
}
3.测试
先启动消费者的启动类,运行消费者项目
在执行生产者的单元测试方法,控制台打印
看出来用户、订单、产品服务分别发送了error、info、warn、debug四个级别的消息,并且error、info通过相应的消息队列接收,并且error、info、warn、debug都通过了all消息队列接收。