【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
**开源地址:https://docs.qq.com/doc/DSmxTbFJ1cmN1R2dB **
spring.rabbitmq.username=dpb
spring.rabbitmq.password=123
#设置交换器的名称
mq.config.exchange=log.topic
#info 队列名称
mq.config.queue.info=log.info
#error 队列名称
mq.config.queue.error=log.error
log 队列名称
mq.config.queue.logs=log.all
三个消费者
@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 {
/**
-
接收消息的方法。采用消息队列监听机制
-
@param msg
*/
@RabbitHandler
public void process(String msg){
System.out.println("Info…receiver: "+msg);
}
}
@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 {
/**
-
接收消息的方法。采用消息队列监听机制
-
@param msg
*/
@RabbitHandler
public void process(String msg){
System.out.println("Error…receiver: "+msg);
}
}
@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 {
/**
-
接收消息的方法。采用消息队列监听机制
-
@param msg
*/
@RabbitHandler
public void process(String msg){
System.out.println("All…receiver: "+msg);
}
}
然后启动项目等待消息即可~
目录结构
配置文件
spring.application.name=springcloud-mq
spring.rabbitmq.host=192.168.88.150
spring.rabbitmq.port=5672
spring.rabbitmq.username=dpb
spring.rabbitmq.password=123
#设置交换器的名称
mq.config.exchange=log.topic
三个服务提供者
@Component
public class UserSender {
@Autowired
private AmqpTemplate rabbitAmqpTemplate;
//exchange 交换器名称
@Value(“${mq.config.exchange}”)
private String exchange;
/*
- 发送消息的方法
*/
public void send(String msg){
//向消息队列发送消息
//参数一:交换器名称。
//参数二:路由键
//参数三:消息
this.rabbitAmqpTemplate.convertAndSend(this.exchange,“user.log.debug”, “user.log.debug…”+msg);
this.rabbitAmqpTemplate.convertAndSend(this.exchange,“user.log.info”, “user.log.info…”+msg);
this.rabbitAmqpTemplate.convertAndSend(this.exchange,“user.log.warn”,“user.log.warn…”+msg);
this.rabbitAmqpTemplate.convertAndSend(this.exchange,“user.log.error”, “user.log.error…”+msg);
}
}
@Component
public class ProductSender {
@Autowired
private AmqpTemplate rabbitAmqpTemplate;
//exchange 交换器名称
@Value(“${mq.config.exchange}”)
private String exchange;
/*
- 发送消息的方法
*/
public void send(String msg){
//向消息队列发送消息