RabbitMQ实战.docx
- 文档编号:5822918
- 上传时间:2023-01-01
- 格式:DOCX
- 页数:17
- 大小:678.76KB
RabbitMQ实战.docx
《RabbitMQ实战.docx》由会员分享,可在线阅读,更多相关《RabbitMQ实战.docx(17页珍藏版)》请在冰豆网上搜索。
RabbitMQ实战
Springboot整合RabbitMQ
依赖
配置
spring:
application:
name:
rabbitmq
rabbitmq:
host:
127.0.0.1
port:
5672
username:
guest
password:
guest
publisher-returns:
true
publisher-confirms:
true
Queue配置
packagecom.neo.config;
importorg.springframework.amqp.core.Queue;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
@Configuration
publicclassQueueConfig{
@Bean
publicQueuecreateQueue(){
returnnewQueue("hello-queue");
}
@Bean
publicQueuecreate2Queue(){
returnnewQueue("hello2-queue");
}
}
Sender:
packagecom.neo.sender;
importorg.springframework.amqp.core.AmqpTemplate;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Component;
@Component
publicclassSender{
@Autowired
AmqpTemplateamqp;
publicvoidsend(Stringmessage){
amqp.convertAndSend("hello-queue",message);
}
publicStringsendAndReceive(Stringmessage){
return(String)amqp.convertSendAndReceive("hello2-queue",message);
}
}
Receiver:
packagecom.neo.receiver;
importorg.springframework.amqp.rabbit.annotation.RabbitListener;
importorg.springframework.stereotype.Component;
@Component
publicclassReceiver{
@RabbitListener(queues="hello-queue")
publicvoidreceive(Stringmessage){
System.out.println("接收消息:
"+message);
}
@RabbitListener(queues="hello2-queue")
publicStringreceiveAndSend(Stringmessage){
System.out.println("接收消息:
"+message);
returnmessage+"receiveBack";
}
}
Direct交换器:
(直接匹配)(一对一)
DirectExchange
处理路由键。
需要将一个队列绑定到交换机上,要求该消息与一个特定的路由键完全匹配。
这是一个完整的匹配。
如果一个队列绑定到该交换机上要求路由键“test”,则只有被标记为“test”的消息才被转发,不会转发test.aaa,也不会转发dog.123,只会转发test。
创建队列和交换机
packagecom.neo.config;
importorg.springframework.amqp.core.DirectExchange;
importorg.springframework.amqp.core.Exchange;
importorg.springframework.amqp.core.Queue;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
@Configuration
publicclassDirectQueueConfig{
@Bean
publicQueuecreateInfoQueue(){
returnnewQueue("info.queue");
}
@Bean
publicQueuecreateErrorQueue(){
returnnewQueue("error.queue");
}
@Bean
publicExchangecreateLogExchange(){
returnnewDirectExchange("log.direct");
}
}
编写receiver
InfoReceiver:
packagecom.neo.receiver;
importorg.springframework.amqp.core.ExchangeTypes;
importorg.springframework.amqp.rabbit.annotation.Exchange;
importorg.springframework.amqp.rabbit.annotation.Queue;
importorg.springframework.amqp.rabbit.annotation.QueueBinding;
importorg.springframework.amqp.rabbit.annotation.RabbitHandler;
importorg.springframework.amqp.rabbit.annotation.RabbitListener;
importorg.springframework.stereotype.Component;
@Component
/**
*@RabbitListenerbindings:
绑定队列
*@QueueBindingexchange:
交换器
*@QueueBindingkey:
指定路由
*@QueueBindingvalue:
队列名
*@Exchangetype:
指定具体的交换器类型
*@QueueautoDelete:
是否是一个可删除的临时队列,springboot2.0报错
*/
@RabbitListener(
bindings=@QueueBinding(
exchange=@Exchange(name="log.direct",type=ExchangeTypes.DIRECT),
value=@Queue(name="info.queue"),
key="info.routing.key"
)
)
publicclassInfoReceiver{
@RabbitHandler
publicvoidreceive(Stringmessage){
System.out.println("接收info消息:
"+message);
}
}
ErrorReceiver:
packagecom.neo.receiver;
importorg.springframework.amqp.core.ExchangeTypes;
importorg.springframework.amqp.rabbit.annotation.Exchange;
importorg.springframework.amqp.rabbit.annotation.Queue;
importorg.springframework.amqp.rabbit.annotation.QueueBinding;
importorg.springframework.amqp.rabbit.annotation.RabbitHandler;
importorg.springframework.amqp.rabbit.annotation.RabbitListener;
importorg.springframework.stereotype.Component;
@Component
@RabbitListener(
bindings=@QueueBinding(
exchange=@Exchange(name="log.direct",type=ExchangeTypes.DIRECT),
value=@Queue(name="error.queue"),
key="error.routing.key"
)
)
publicclassErrorReceiver{
@RabbitHandler
publicvoidreceive(Stringmessage){
System.out.println("接收eror消息:
"+message);
}
}
Sender:
packagecom.neo.sender;
importorg.springframework.amqp.core.AmqpTemplate;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Component;
@Component
publicclassSender{
@Autowired
AmqpTemplateamqp;
publicvoidsendInfo(Stringmessage){
amqp.convertAndSend("log.direct","info.routing.key",message);
}
publicvoidsendError(Stringmessage){
amqp.convertAndSend("log.direct","error.routing.key",message);
}
}
Topic交换器:
(规则匹配)(多对多)
TopicExchange
将路由键和某模式进行匹配。
此时队列需要绑定要一个模式上。
符号“#”匹配一个或多个词,符号“*”匹配不多不少一个词。
因此“audit.#”能够匹配到“audit.irs.corporate”,但是“audit.*”只会匹配到“audit.irs”。
创建队列和路由
packagecom.neo.config;
importorg.springframework.amqp.core.Exchange;
importorg.springframework.amqp.core.Queue;
importorg.springframework.amqp.core.TopicExchange;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
@Configuration
publicclassTopicQueueConfig{
@Bean
publicQueuecreateInfoQueue(){
returnnewQueue("log.info");
}
@Bean
publicQueuecreateErrorQueue(){
returnnewQueue("log.error");
}
@Bean
publicQueuecreateDebugQueue(){
returnnewQueue("log.debug");
}
@Bean
publicQueuecreateAllQueue(){
returnnewQueue("log.all");
}
@Bean
publicExchangecreateLogExchange(){
returnnewTopicExchange("log.topic");
}
}
创建sender
UserSender
packagecom.neo.sender;
importorg.springframework.amqp.core.AmqpTemplate;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Component;
@Component
publicclassUserSender{
@Autowired
AmqpTemplateamqp;
publicvoidsendInfo(Stringmessage){
amqp.convertAndSend("log.topic","user.log.info","user.log.info"+message);
amqp.convertAndSend("log.topic","user.log.error","user.log.error"+message);
amqp.convertAndSend("log.topic","user.log.debug","user.log.debug"+message);
amqp.convertAndSend("log.topic","user.log.warn","user.log.warn"+message);
}
}
ProductSender
packagecom.neo.sender;
importorg.springframework.amqp.core.AmqpTemplate;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Component;
@Component
publicclassProductSender{
@Autowired
AmqpTemplateamqp;
publicvoidsendInfo(Stringmessage){
amqp.convertAndSend("log.topic","product.log.info","product.log.info"+message);
amqp.convertAndSend("log.topic","product.log.error","product.log.error"+message);
amqp.convertAndSend("log.topic","product.log.debug","product.log.debug"+message);
amqp.convertAndSend("log.topic","product.log.warn","product.log.warn"+message);
}
}
OrderSender:
packagecom.neo.sender;
importorg.springframework.amqp.core.AmqpTemplate;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Component;
@Component
publicclassOrderSender{
@Autowired
AmqpTemplateamqp;
publicvoidsendInfo(Stringmessage){
amqp.convertAndSend("log.topic","order.log.info","order.log.info"+message);
amqp.convertAndSend("log.topic","order.log.error","order.log.error"+message);
amqp.convertAndSend("log.topic","order.log.debug","order.log.debug"+message);
amqp.convertAndSend("log.topic","order.log.warn","order.log.warn"+message);
}
}
创建Receiver
LogsReceiver
packagecom.neo.receiver;
importorg.springframework.amqp.core.ExchangeTypes;
importorg.springframework.amqp.rabbit.annotation.Exchange;
importorg.springframework.amqp.rabbit.annotation.Queue;
importorg.springframework.amqp.rabbit.annotation.QueueBinding;
importorg.springframework.amqp.rabbit.annotation.RabbitHandler;
importorg.springframework.amqp.rabbit.annotation.RabbitListener;
importorg.springframework.stereotype.Component;
@Component
@RabbitListener(
bindings=@QueueBinding(
exchange=@Exchange(name="log.topic",type=ExchangeTypes.TOPIC),
value=@Queue(name="log.all"),
key="*.log.*"
)
)
publicclassLogsReceiver{
@RabbitHandler
publicvoidreceive(Stringmessage){
System.out.println("接收log.all消息:
"+message);
}
}
InfoReceiver
packagecom.neo.receiver;
importorg.springframework.amqp.core.ExchangeTypes;
importorg.springframework.amqp.rabbit.annotation.Exchange;
importorg.springframework.amqp.rabbit.annotation.Queue;
importorg.springframework.amqp.rabbit.annotation.QueueBinding;
importorg.springframework.amqp.rabbit.annotation.RabbitHandler;
importorg.springframework.amqp.rabbit.annotation.RabbitListener;
importorg.springframework.stereotype.Component;
@Component
@RabbitListener(
bindings=@QueueBinding(
exchange=@Exchange(name="log.topic",type=ExchangeTypes.TOPIC),
value=@Queue(name="log.info"),
key="*.log.info"
)
)
publicclassInfoReceiver{
@RabbitHandler
publicvoidreceive(Stringmessage){
System.out.println("接收log.info消息:
"+message);
}
}
ErrorReceiver
packagecom.neo.receiver;
importorg.springframework.amqp.core.ExchangeTypes;
importorg.springframework.amqp.rabbit.annotation.Exchange;
importorg.springframework.amqp.rabbit.annotation.Queue;
importorg.springframework.amqp.rabbit.annotation.QueueBinding;
importorg.springframework.amqp.rabbit.annotation.RabbitHandler;
importorg.springframework.amqp.rabbit.annotation.RabbitListener;
importorg.springframework.stereotype.Component;
@Component
@RabbitListener(
bindings=@QueueBinding(
exchange=@Exchange(name="log.topic",type=ExchangeTypes.TOPIC),
value=@Queue(name="log.error"),
key="*.log.error"
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- RabbitMQ 实战
![提示](https://static.bdocx.com/images/bang_tan.gif)