首先:代码版本是基于 Spring2.1.12 + Rabbit 整合实现

rabbit:pom

<!-- rabbitmq -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

rabbit:yml配置

server:
  port: 8600
spring:
  application:
    name: rabbitmq-provider
  rabbitmq:
    host: 192.168.189.131
    port: 5673
    username: admin
    password: admin
    #确认消息已发送到交换机(Exchange)
    publisher-confirms: true
    #确认消息已发送到队列(Queue)
    publisher-returns: true

 


 


1:Direct Exchange 直连交换机

定义:直连类型的交换机与一个队列绑定时,需要指定一个明确的binding key。

路由规则:发现消息到直连类型的交换机时,只有routing key和binding key完全匹配时,绑定的队列才能收到消息。

spring boot rabbitMq创建队列 配置交换机_Data


spring boot rabbitMq创建队列 配置交换机_Data_02

转存失败重新上传取消spring boot rabbitMq创建队列 配置交换机_Data_03

DirectRabbitConfig


@Configuration
public class DirectRabbitConfig {
    //队列 起名:TestDirectQueue
    @Bean
    public Queue TestDirectQueue() {
        return new Queue("TestDirectQueue",true);
    }

    //Direct交换机 起名:TestDirectExchange
    @Bean
    DirectExchange TestDirectExchange() {
        return new DirectExchange("TestDirectExchange");
    }

    //绑定  将队列和交换机绑定, 并设置用于匹配键:TestDirectRouting
    @Bean
    Binding bindingDirect() {
        return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting");
    }

}

spring boot rabbitMq创建队列 配置交换机_Data_02

转存失败重新上传取消 spring boot rabbitMq创建队列 配置交换机_Data_03

DirectRabbitController
@RestController
public class DirectRabbitController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendDirectMessage/{message}")
    public void sendDirectMessage(@PathVariable("message") String message){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = message;
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
    }

    @RabbitListener(queues = "TestDirectQueue")
    public void consumerDirectMessage(Map map){
        System.out.println("DirectReceiver消费者收到消息  : " + map.toString());
    }

}


 


2、Topic Exchange 主题交换机

定义:主题类型的交换机与一个队列绑定时,可以指定按模式匹配的routing key。

通配符有两个,*代表匹配一个单词。#代表匹配零个或者多个单词。单词与单词之间用.隔开。

路由规则:发送消息到主题类型的交换机时,routing key符合binding key的模式时,绑定的队列才能收到消息。

spring boot rabbitMq创建队列 配置交换机_System_06


spring boot rabbitMq创建队列 配置交换机_Data_02

转存失败重新上传取消spring boot rabbitMq创建队列 配置交换机_Data_03

TopicRabbitConfig
@Configuration
public class TopicRabbitConfig {
    public final static String man = "topic.man";
    public final static String woman = "topic.woman";

    @Bean
    public Queue firstQueue() {
        return new Queue(TopicRabbitConfig.man);
    }

    @Bean
    public Queue secondQueue() {
        return new Queue(TopicRabbitConfig.woman);
    }

    @Bean
    public TopicExchange exchange() {
        return new TopicExchange("topicExchange");
    }

    //将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
    //这样只要是消息携带的路由键是topic.man,才会分发到该队列
    @Bean
    Binding bindingExchangeMessage() {
        return BindingBuilder.bind(firstQueue()).to(exchange()).with(man);
    }

    //将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
    // 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
    @Bean
    Binding bindingExchangeMessage2() {
        return BindingBuilder.bind(secondQueue()).to(exchange()).with("topic.#");
    }

}

 

TopicRabbitController
@RestController
public class TopicRabbitController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendTopicFirst")
    public String sendTopicMessage1() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: M A N ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> manMap = new HashMap<>();
        manMap.put("messageId", messageId);
        manMap.put("messageData", messageData);
        manMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend("topicExchange", "topic.man", manMap);
        return "ok";
    }

    @GetMapping("/sendTopicSecond")
    public String sendTopicSecond() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: woman is all ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> womanMap = new HashMap<>();
        womanMap.put("messageId", messageId);
        womanMap.put("messageData", messageData);
        womanMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend("topicExchange", "topic.woman", womanMap);
        return "ok";
    }

    @RabbitListener(queues = "topic.man")
    public void topicManReceiver(Map map){
        System.out.println("topicManReceiver消费者收到消息  : " + map.toString());
    }

    @RabbitListener(queues = "topic.woman")
    public void topicWomenReceiver(Map map){
        System.out.println("topicWomenReceiver消费者收到消息  : " + map.toString());
    }

}

3、Fanout Exchange 广播交换机

定义:广播类型的交换机与一个队列 绑定时,不需要指定binding key。

路由规则:当消息发送到广播类型的交换机时,不需要指定routing key,所有与之绑定的队列都能收到消息。

spring boot rabbitMq创建队列 配置交换机_上传_09


spring boot rabbitMq创建队列 配置交换机_Data_02

转存失败重新上传取消spring boot rabbitMq创建队列 配置交换机_Data_03

 

FanoutRabbitConfig
@Configuration
public class FanoutRabbitConfig {
    /**
     *  创建三个队列 :fanout.A   fanout.B  fanout.C
     *  将三个队列都绑定在交换机 fanoutExchange 上
     *  因为是扇型交换机, 路由键无需配置,配置也不起作用
     */
    @Bean
    public Queue queueA() {
        return new Queue("fanout.A");
    }

    @Bean
    public Queue queueB() {
        return new Queue("fanout.B");
    }

    @Bean
    public Queue queueC() {
        return new Queue("fanout.C");
    }

    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    @Bean
    Binding bindingExchangeA() {
        return BindingBuilder.bind(queueA()).to(fanoutExchange());
    }

    @Bean
    Binding bindingExchangeB() {
        return BindingBuilder.bind(queueB()).to(fanoutExchange());
    }

    @Bean
    Binding bindingExchangeC() {
        return BindingBuilder.bind(queueC()).to(fanoutExchange());
    }

}
FanoutRabbitController
@RestController
public class FanoutRabbitController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendFanoutMessage")
    public String sendFanoutMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: testFanoutMessage ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("fanoutExchange", null, map);
        return "ok";
    }

    @RabbitListener(queues = "fanout.A")
    public void FanoutReceiverA(Map testMessage){
        System.out.println("FanoutReceiverA消费者收到消息  : " +testMessage.toString());
    }

    @RabbitListener(queues = "fanout.B")
    public void FanoutReceiverB(Map testMessage){
        System.out.println("FanoutReceiverB消费者收到消息  : " +testMessage.toString());
    }

    @RabbitListener(queues = "fanout.C")
    public void FanoutReceiverC(Map testMessage){
        System.out.println("FanoutReceiverC消费者收到消息  : " +testMessage.toString());
    }

}