假设一个需求:某服务通过Direct交换器根据指定路由键的key向指定消息队列发消息进行相应的处理服务,这里模拟日志处理的四个级别,对info、run、error、warn四个级别分别设置四个消息队列,并分别指定相应的路由健
这里创建两个项目,一个为生产者,用来发生消息到指定的,另一个为消费者,用来接受生产者发送的消息,并打印相应的日志级别
pom.xml文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.21.RELEASE
per.czt
rabbitmq-provider-consumer
0.0.1-SNAPSHOT
jar
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-starter-amqp
org.springframework.boot
spring-boot-maven-plugin
启动类
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.direct
#info queue name
mq.config.queue.info=log.info
#info routing key
mq.config.queue.info.routing.key=log.info.routing.key
#error queue name
mq.config.queue.error=log.error
#error routing key
mq.config.queue.error.routing.key=log.error.routing.key
#debug queue name
mq.config.queue.debug=log.debug
#debug routing key
mq.config.queue.debug.routing.key=log.debug.routing.key
#run queue name
mq.config.queue.warn=log.warn
#error routing key
mq.config.queue.warn.routing.key=log.warn.routing.key
下面是四个接收消息的类
DebugReceiver.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.debug}",autoDelete="true"),
exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.DIRECT),
key="${mq.config.queue.debug.routing.key}"
)
)
public class DebugReceiver {
/*
* 接受消息的方法,采用消息队列接收机制
*/
@RabbitHandler
public void process(String msg) {
System.out.println("Debug reveiver:"+msg);
}
}
WarnReceiver.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.warn}",autoDelete="true"),
exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.DIRECT),
key="${mq.config.queue.warn.routing.key}"
)
)
public class WarnReceiver {
/*
* 接受消息的方法,采用消息队列接收机制
*/
@RabbitHandler
public void process(String msg) {
System.out.println("Warn 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.DIRECT),
key="${mq.config.queue.info.routing.key}"
)
)
public class InfoReceiver {
/*
* 接受消息的方法,采用消息队列接收机制
*/
@RabbitHandler
public void process(String msg) {
System.out.println("Info reveiver:"+msg);
}
}
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.DIRECT),
key="${mq.config.queue.error.routing.key}"
)
)
public class ErrorReceiver {
/*
* 接受消息的方法,采用消息队列接收机制
*/
@RabbitHandler
public void process(String msg) {
System.out.println("Error reveiver:"+msg);
}
}
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.21.RELEASE
per.czt
rabbitmq-direct-provider
0.0.1-SNAPSHOT
jar
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-starter-amqp
org.springframework.boot
spring-boot-maven-plugin
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);
}
}
Sender.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 Sender {
@Autowired
private AmqpTemplate amqpTemplate;
@Value("${mq.config.exchange}")
private String exchange;
//发送warn级别的消息
@Value("${mq.config.queue.warn.routing.key}")
private String routingkey;
/*
* 发生消息的方法
*/
public void send(String msg) {
//向消息队列发生消息
//参数一:交换器密匙
//参数二:路由健
//参数三:消息
amqpTemplate.convertAndSend(exchange,routingkey,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.Sender;
/*
* 消息队列测试类
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes= {App.class})
public class QueueTest {
@Autowired
private Sender sender;
@Test
public void test1() {
//发送消息
sender.send("Hello1");
}
@Test
public void test2() throws InterruptedException {
//发送消息
while(true) {
Thread.sleep(1000);
sender.send("Hello");
}
}
}
先启动消费者的启动类,运行消费者项目
在执行生产者的单元测试方法2
由于生产者发送的是warn的消息,控制台打印出接收到warn消息
再修改生产者的routingkey为application.propertiesde中info对应的key
再次启动测试类的方法2
发现接受了info级别和warn级别的消息