RabbitMQ的交换机(原理及代码实现)

1.交换机类型

  • Fanout Exchange(扇形)
  • Direct Exchange(直连)
  • opic Exchange(主题)
  • Headers Exchange(头部)

2.Fanout Exchange

2.1 简介

Fanout 扇形的,散开的; 扇形交换机

投递到所有绑定的队列,不需要路由键,不需要进行路由键的匹配,相当于广播、群发;

如下图所示

P代表provider提供者,X代表exchange交换机,第三部分代表队列

2.2 代码示例

2.2.1 扇形交换机与队列

定义交换机主要由3部分组成,

  • 1.定义交换机
  • 2.定义队列
  • 3.绑定交换机
java 复制代码
@Configuration
public class RabbitConfig {
    //rabbitmq三部曲
    //1.定义交换机
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("exchange.fanout");
    }
    //2.定义队列
    //此处定义两个队列
    @Bean
    public Queue queueA(){
        return new Queue("queue.fanout.a");
    }
    @Bean
    public Queue queueB(){
        return new Queue("queue.fanout.b");
    }
    //3.绑定交换机
    @Bean
    public Binding bindingA(FanoutExchange fanoutExchange,Queue queueA){
        //将队列A绑定到扇形交换机
        return BindingBuilder.bind(queueA).to(fanoutExchange);
    }
    @Bean
    public Binding bindingB(FanoutExchange fanoutExchange,Queue queueB){
        //将队列A绑定到扇形交换机
        return BindingBuilder.bind(queueB).to(fanoutExchange);
    }
}

2.2.2 模拟provider发送消息

发送消息模拟

java 复制代码
@Component
@Slf4j
public class MessageService {
    @Resource
    private RabbitTemplate rabbitTemplate;

    public void senMsg(){
        //定义消息
        String msg="hello world";
        //发消息
        Message message= new Message(msg.getBytes());
        rabbitTemplate.convertAndSend("exchange.fanout","",message);
        log.info("消息发送完毕,发送时间为:{}", new Date());
    }
}

2.2.3 接受扇形交换机信息

3.Direct Exchange

根据路由路由键(Routing Key)精确匹配(一模一样)进行路由消息队列;

与扇形交换机不同的是,直连交换机必须要绑定key

3.2代码示例

java 复制代码
@Configuration
public class RabbitConfig {
    //rabbitmq三部曲
    //1.定义交换机
    @Bean
    public DirectExchange directExchange(){
        return ExchangeBuilder.directExchange("exchange.direct").build();
    }
    //2.定义队列
    //此处定义两个队列
    @Bean
    public Queue queueA(){
        return new Queue("queue.direct.a");
    }
    @Bean
    public Queue queueB(){
        return new Queue("queue.direct.b");
    }
    //3.绑定交换机
    @Bean
    public Binding bindingA(DirectExchange directExchange,Queue queueA){
        //将队列A绑定到扇形交换机
        return BindingBuilder.bind(queueA).to(directExchange).with("error");
    }
    @Bean
    public Binding bindingB1(DirectExchange directExchange,Queue queueB){
        //将队列A绑定到扇形交换机
        return BindingBuilder.bind(queueB).to(directExchange).with("error");
    }
    @Bean
    public Binding bindingB2(DirectExchange directExchange,Queue queueB){
        //将队列A绑定到扇形交换机
        return BindingBuilder.bind(queueB).to(directExchange).with("info");
    }
    @Bean
    public Binding bindingB3(DirectExchange directExchange,Queue queueB){
        //将队列A绑定到扇形交换机
        return BindingBuilder.bind(queueB).to(directExchange).with("warning");
    }
}

4. Topic Exchange

通配符匹配,相当于模糊匹配;

#匹配多个单词,用来表示任意数量(零个或多个)单词

*匹配一个单词(必须有一个,而且只有一个),用.隔开的为一个单词

5.Headers Exchange

基于消息内容中的headers属性进行匹配;

用的比较少

相关推荐
用户128526116021 小时前
我把祖传Java项目重构后,接口响应从3s砍到了200ms,只改了这几行代码
java
Linsk2 小时前
组件 = 模板 + 业务逻辑
java·前端·vue.js
星沉远浦2 小时前
用Gemini高效解决Java代码报错难以定位的问题
java
用户298698530146 小时前
Word 文档字符级格式化:Java 实现方案详解
java·后端
笨鸟飞不快6 小时前
从单个服务到集群:一次完整的性能排查复盘
java·前端
荣码7 小时前
用Streamlit给AI应用套个界面,10行代码出Web页面
java·python
SamDeepThinking7 小时前
Java微服务练习方式
java·后端·微服务
朦胧之17 小时前
AI 编程-老项目改造篇
java·前端·后端
程序猿大帅1 天前
别再只当调包侠了:用 Spring AI 落地 Function Calling,我被大模型硬生生砸出了三个大坑
java
程序员晓琪1 天前
约定大于配置:基于 Java 包名自动生成 API 版本路由的最佳实践
java·spring boot·后端