SpringBoot+RabbitMQ学习笔记(二)使用RabbitMQ的三种交换器之Direct

摘要:
不会转发test.aaa。

一丶简介

Direct Exchange 
处理路由键。需要将一个队列绑定到交换器上,要求该消息与一个特定的路由键完全匹配。这是一个完整的匹配。如果一个队列绑定到该交换器上要求路由键 “test”,则只有被标记为“test”的消息才被转发,不会转发test.aaa,也不会转发dog.123,只会转发test。 

业务场景,系统日志处理场景:

1.微服务产生日志,交给日志服务器处理。

2.日志服务器一共有4个服务,分别问DEBUG、INFO、WARN、ERROR(这里只写两个INFO和ERROR)。

3.服务之间的通信采用Direct(发布订阅)。

 SpringBoot+RabbitMQ学习笔记(二)使用RabbitMQ的三种交换器之Direct第1张

丶配置文件

我分别创建了两个项目,一个作为生产者来发送日志,一个作为消费者来接收日志。

生产者配置:

SpringBoot+RabbitMQ学习笔记(二)使用RabbitMQ的三种交换器之Direct第2张SpringBoot+RabbitMQ学习笔记(二)使用RabbitMQ的三种交换器之Direct第3张
 1 server.port=8883
 2 
 3 spring.application.name=hello-world
 4 
 5 spring.rabbitmq.host=localhost
 6 spring.rabbitmq.port=5672
 7 spring.rabbitmq.username=guest
 8 spring.rabbitmq.password=guest
 9 
10 #设置交换器名称
11 mq.config.exchange=log.direct
12 #info的路由键
13 mq.config.queue.info.routing.key=log.info.routing.key
14 #error的队列名称
15 mq.config.queue.error=log.error
16 #error的路由键
17 mq.config.queue.error.routing.key=log.error.routing.key
View Code

消费者配置:

SpringBoot+RabbitMQ学习笔记(二)使用RabbitMQ的三种交换器之Direct第4张SpringBoot+RabbitMQ学习笔记(二)使用RabbitMQ的三种交换器之Direct第5张
 1 server.port=8884
 2 
 3 spring.application.name=lesson1
 4 
 5 spring.rabbitmq.host=localhost
 6 spring.rabbitmq.port=5672
 7 spring.rabbitmq.username=guest
 8 spring.rabbitmq.password=guest
 9 
10 #设置交换器名称
11 mq.config.exchange=log.direct
12 #info队列名称
13 mq.config.queue.info=log.info
14 #info的路由键
15 mq.config.queue.info.routing.key=log.info.routing.key
16 #error队列名称
17 mq.config.queue.error=log.error
18 #error的路由键
19 mq.config.queue.error.routing.key=log.error.routing.key
View Code

三丶创建生产者

SpringBoot+RabbitMQ学习笔记(二)使用RabbitMQ的三种交换器之Direct第6张SpringBoot+RabbitMQ学习笔记(二)使用RabbitMQ的三种交换器之Direct第7张
 1 package com.example.ampq;
 2 
 3 import org.springframework.amqp.core.AmqpTemplate;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.beans.factory.annotation.Value;
 6 import org.springframework.stereotype.Component;
 7 
 8 /**
 9  * Author:aijiaxiang
10  * Date:2020/4/26
11  * Description:发送消息
12  */
13 @Component
14 public class Sender {
15 
16     @Autowired
17     private AmqpTemplate amqpTemplate;
18 
19     //exChange 交换器
20     @Value("${mq.config.exchange}")
21     private String exChange;
22 
23     //routingkey 路由键
24     @Value("${mq.config.queue.error.routing.key}")
25     private String routingKey;
26     /**
27      * 发送消息的方法
28      * @param msg
29      */
30     public void send(String msg){
31         //向消息队列发送消息
32         //参数1:交换器名称
33         //参数2:路由键
34         //参数3:消息
35         this.amqpTemplate.convertAndSend(exChange,routingKey,msg);
36 
37     }
38 }
View Code

这里是向error队列发送消息,若要向info队列发送消息可将“routingKey”上的配置改为如下:

@Value("${mq.config.queue.info.routing.key}")
private String routingKey;

四丶创建消费者

1.error消息接收类

SpringBoot+RabbitMQ学习笔记(二)使用RabbitMQ的三种交换器之Direct第8张SpringBoot+RabbitMQ学习笔记(二)使用RabbitMQ的三种交换器之Direct第9张
package com.ant.amqpdirectconsumer;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * Author:aijiaxiang
 * Date:2020/4/26
 * Description:消息接收者
 * @RabbitListener bindings:绑定队列
 * @QueueBinding  value:绑定队列的名称
 *                  exchange:配置交换器
 * @Queue : value:配置队列名称
 *          autoDelete:是否是一个可删除的临时队列
 * @Exchange value:为交换器起个名称
 *           type:指定具体的交换器类型
 */
@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 {

    /**
     * 接收消息的方法,采用消息队列监听机制
     * @param msg
     */
    @RabbitHandler
    public void process(String msg){
        System.out.println("error-receiver:"+msg);
    }
}
View Code

2.info消息接收类

SpringBoot+RabbitMQ学习笔记(二)使用RabbitMQ的三种交换器之Direct第10张SpringBoot+RabbitMQ学习笔记(二)使用RabbitMQ的三种交换器之Direct第11张
package com.ant.amqpdirectconsumer;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * Author:aijiaxiang
 * Date:2020/4/26
 * Description:消息接收者
 * @RabbitListener bindings:绑定队列
 * @QueueBinding  value:绑定队列的名称
 *                  exchange:配置交换器
 * @Queue : value:配置队列名称
 *          autoDelete:是否是一个可删除的临时队列
 * @Exchange value:为交换器起个名称
 *           type:指定具体的交换器类型
 */
@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.error.routing.key}"
        )
)
public class InfoReceiver {

    /**
     * 接收消息的方法,采用消息队列监听机制
     * @param msg
     */
    @RabbitHandler
    public void process(String msg){
        System.out.println("receiver:"+msg);
    }
}
View Code

五丶测试一波

SpringBoot+RabbitMQ学习笔记(二)使用RabbitMQ的三种交换器之Direct第12张SpringBoot+RabbitMQ学习笔记(二)使用RabbitMQ的三种交换器之Direct第13张
package com.example.amqp;

import com.example.ampq.Sender;
import com.example.helloworld.HelloworldApplication;
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.SpringRunner;

/**
 * Author:aijiaxiang
 * Date:2020/4/26
 * Description:
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = HelloworldApplication.class)
public class QueueTest {
    @Autowired
    private Sender sender;

    /**
     * 测试消息队列
     */
    @Test
    public void test1() throws InterruptedException {
        while (true){
            Thread.sleep(1000);
            sender.send("hello");
        }

    }

  
}
View Code

免责声明:文章转载自《SpringBoot+RabbitMQ学习笔记(二)使用RabbitMQ的三种交换器之Direct》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Fiddler (三) Composer创建和发送HTTP RequestC#-微信公众平台接口-上传临时素材下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

[小技巧]window10删除此电脑左侧栏中的非磁盘文件夹

一、问题说明    在初始的 此电脑 中,有置顶的文件夹栏,包含图片、视频、下载等多个文件夹 二、解决方式    通过 WIN+R 键,输入 regedit 后确认,打开注册表。定位到  HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionExplorerFolderDescriptions  中...

eclipse编辑工具小结

eclipse编辑工具小结这两天从myeclipse转入eclipse,整体感觉不错,速度更快些,也没在出现各种意外的调试错误、不能断点等情况,并且对整个编辑环境的使用有了更深入的认识,再次对主要几个方面总结如下: 1.编辑环境设置的备份、恢复(如工程编码格式、文件打开关联,代码提示,字体、背景颜色、快捷键及一些template等等) 方法一:使用ecl...

flutter 蓝牙开发记录

记录自己开发蓝牙通信功能过程 插件使用:flutter_blue 蓝牙硬件:hc-08  在代码之前需要设置蓝牙权限和位置权限 1:扫描蓝牙设备,返回 deviceid 列表      //可以提前注册扫描监听事件FlutterBlue flutterBlue = FlutterBlue.instance; List<String> b...

xxxx(四):接受消息hook地址分析

   今天来分析一下xxxx接受消息的call;测试的账号在虚拟机,发消息的账号在物理机;    1、老规矩:逆向分析的起点都从CE开始;给测试账号发送辨识度高的消息,这时暂时不要在测试账号打开消息。因为此时的消息刚经过网络传输,还是ASCII格式,所以千万不要勾选UTF-16,只能找ASCII码找(下面详细解释原因)!  重复发送0000000000、...

AWS系列-Amazon Simple Notification Service (SNS)

SNS是一项 Web 服务,用于协调和管理向订阅终端节点或客户交付或发送消息的过程。在 Amazon SNS 中有两种类型的客户端:发布者和订阅者,也称为生产者和消费者。发布者通过创建消息并将消息发送至主题与订阅者进行异步交流,主题是一个逻辑访问点和通信渠道。订阅者(即 Web 服务器、电子邮件地址、Amazon SQS 队列、AWS Lambda 函数)...

在Linux下最好用的截图工具Flameshot

在使用StationP1的时候常常需要截图,习惯性的试用了QQ的快捷键截图,截图标注编辑一气呵成,但是Ubuntu仅仅只有系统的截图工具,虽然也有一些功能,但是无法编辑,标注等。于是还是决定上了Flameshot下图是截图效果: 除了随心所欲的选择区域截屏,还附带编辑工具,简直一次过满足多个愿望安装与配置方法:1,使用命令安装 sudo apt insta...