RabbitMQ交换机的讲解

一、交换机类型

1.扇形交换机

扇形交换机是最基本的交换机,它的作用是广播消息,把能接收到的消息全部发送给绑定在自己身上的队列,因为广播不需要思考,所以扇形交换机处理消息的速度也是所有交换机类型里面最快的

2.直连交换机

将消息推送到binding key与该消息的routing key相同的队列

RabbitMQ交换机的讲解_第1张图片

直连交换机x上绑定了两个队列,第一个列队绑定了绑定键orange,第二个队列有两个绑定键:black和green。在这种情况下,一个消息在布时指定了路由键为orange将会只被路由到列队q1,路由键为black和green的消息都将被路由到队列q2,其他的消息都将被丢失

3.主题交换机

RabbitMQ交换机的讲解_第2张图片二、交换机代码

1.直连交换机 

①创建配置类DirectConfig(创建交换机,队列,进行绑定)

package com.example.provider.mq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author 小宝的宝
 */
@Configuration
@SuppressWarnings("all")
public class DirectConfig {
    /**
     * 创建队列
     * @return
     */
    @Bean
    public Queue directQueueA(){
        return new Queue("directQueueA",true);
    }
    @Bean
    public Queue directQueueB(){
        return new Queue("directQueueB",true);
    }
    @Bean
    public Queue directQueueC(){
        return new Queue("directQueueC",true);
    }

    /**
     * 创建交换机
     */
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("directExchange");
    }

    @Bean
    public Binding bindingA(){
        return BindingBuilder.bind(directQueueA()).to(directExchange()).with("AA");
    }
    @Bean
    public Binding bindingB(){
        return BindingBuilder.bind(directQueueB()).to(directExchange()).with("BB");
    }
    @Bean
    public Binding bindingC(){
        return BindingBuilder.bind(directQueueC()).to(directExchange()).with("CC");
    }
}

②创建controller层模拟发送消息

package com.example.provider;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 小宝的宝
 */
@RestController
public class providerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/sendDirect")
    public String sendDirect(String routingKey){
        rabbitTemplate.convertAndSend("directExchange",routingKey,"Hello");
        return "yes";
    }
}

③运行结果 出现三个列队

RabbitMQ交换机的讲解_第3张图片 ④在消费者中创建接收者类,需要一直运行,表示一直处于接收状态

分别监听队列ABC

RabbitMQ交换机的讲解_第4张图片

 ⑤启动消费者就可以接收消息啦

RabbitMQ交换机的讲解_第5张图片

2.主题交换机

①创建主题交换机的配置类

定义key的规则并与交换机进行绑定

package com.example.provider.mq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 小宝的宝
 */
@Configuration
@SuppressWarnings("all")
public class TopicConfig {
    public final static String KEY_A="*.black.*";
    public final static String KEY_B="*.ribbit.#";
    public final static String KEY_C="#.lazy";
    /**
     * 创建队列
     * @return
     */
    @Bean
    public Queue topicQueueA(){
        return new Queue("topicQueueA",true);
    }
    @Bean
    public Queue topicQueueB(){
        return new Queue("topicQueueB",true);
    }
    @Bean
    public Queue topicQueueC(){
        return new Queue("topicQueueC",true);
    }

    /**
     * 创建交换机
     */
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("topicExchange");
    }

    @Bean
    public Binding topicbindingA(){
        return BindingBuilder.bind(topicQueueA()).to(topicExchange()).with(KEY_A);
    }
    @Bean
    public Binding topicbindingB(){
        return BindingBuilder.bind(topicQueueB()).to(topicExchange()).with(KEY_B);
    }
    @Bean
    public Binding topicbindingC(){
        return BindingBuilder.bind(topicQueueC()).to(topicExchange()).with(KEY_C);
    }
}

②在消费者中创建主题队列的接收者并监听队列

RabbitMQ交换机的讲解_第6张图片

③在controller层中定义发消息的方法

@RequestMapping("/sendTopic")
public String sendTopic(String routingKey){
    rabbitTemplate.convertAndSend("topicExchange",routingKey,"Hello");
    return "yes";
}

 ④运行方法

RabbitMQ交换机的讲解_第7张图片

3.扇形交换机

交换机绑定队列时不需要绑定键

①.配置类(新建队列和交换机并将他们绑定)

package com.example.provider.mq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 小宝的宝
 */
@Configuration
@SuppressWarnings("all")
public class FanoutConfig {
    /**
     * 创建队列
     * @return
     */
    @Bean
    public Queue fanoutQueueA(){
        return new Queue("fanoutQueueA",true);
    }
    @Bean
    public Queue fanoutQueueB(){
        return new Queue("fanoutQueueB",true);
    }
    @Bean
    public Queue fanoutQueueC(){
        return new Queue("fanoutQueueC",true);
    }

    /**
     * 创建交换机
     */
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanoutExchange");
    }

    @Bean
    public Binding fanoutbindingA(){
        return BindingBuilder.bind(fanoutQueueA()).to(fanoutExchange());
    }
    @Bean
    public Binding fanoutbindingB(){
        return BindingBuilder.bind(fanoutQueueB()).to(fanoutExchange());
    }
    @Bean
    public Binding fanoutbindingC(){
        return BindingBuilder.bind(fanoutQueueC()).to(fanoutExchange());
    }
}

注意:交换机与列队绑定时,方法名不能与其他的重复 

②controller层方法

@RequestMapping("/sendFanout")
public String sendFanout(){
    rabbitTemplate.convertAndSend("fanoutExchange",null,"Hello");
    return "yes";
}

③重新启动运行controller方法

RabbitMQ交换机的讲解_第8张图片 

可看到与三个队列绑定 

 RabbitMQ交换机的讲解_第9张图片

 ④消费者模块创建接收类

RabbitMQ交换机的讲解_第10张图片

⑤启动消费者

接收到消息

RabbitMQ交换机的讲解_第11张图片

你可能感兴趣的:(rabbitmq,分布式,java,spring,boot,linux)