栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

RabbitMQ.交换机的讲解

RabbitMQ.交换机的讲解

交换机类型:

 

 

交换机的属性:

    Name:交换机名称

    Type:交换机类型,direct、topic、fanout、headers

    Durability:是否需要持久化,如果持久化,则RabbitMQ重启后,交换机还存在

    Auto Delete:当最后一个绑定到Exchange上的队列删除后,自动删除该Exchange

    Internal:当前Exchange是否用于RabbitMQ内部使用,默认为False

    Arguments:扩展参数,用于扩展AMQP协议定制化使用

昨天的基础上编写

一、直连交换机

1、生产者

①、application.yml

server:
    port: 8081
spring:
    application:
        name: xx
    rabbitmq:
        host: 192.168.42.148
        password: 123456
        port: 5672
        username: springboot
        virtual-host: my_vhost

②、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;
@Configuration
public class DirectConfig {


    @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");
    }

}

③、ProviderController

package com.example.provider.mq;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProviderController {
    @Autowired
    private RabbitTemplate template;

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

 ④、application.yml

server:
    port: 8082
spring:
    application:
        name: consumer
    rabbitmq:
        host: 192.168.42.148
        password: 123456
        port: 5672
        username: springboot
        virtual-host: my_vhost

⑤、运行测试

队列新增成功

 

2、消费者 

 ①、DirectReceiverA

package com.example.consumer.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "directQueueA")
@Slf4j
public class DirectReceiverA {
    @RabbitHandler
    public void process(String message){
        log.info("A接收"+message);
    }
}

②、DirectReceiverB 

package com.example.consumer.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "directQueueB")
@Slf4j
public class DirectReceiverB {
    @RabbitHandler
    public void process(String message){
        log.info("B接收"+message);
    }
}

③、DirectReceiverC 

package com.example.consumer.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "directQueueC")
@Slf4j
public class DirectReceiverC {
    @RabbitHandler
    public void process(String message){
        log.info("C接收"+message);
    }
}

④、运行测试

 二、主题交换机

1、生产者

①、TopicConfig 

 package com.example.provider.mq;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TopicConfig {

    private final static String KEY_A="*.orange.*";
    private final static String KEY_B="*.*.rabbit";
    private final static String KEY_C="lazy.#";


    @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);
    }
}

②、ProviderController

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

③、运行测试

符合TopicConfig 中的规则就会增加队列

2、消费者

①、TopicReceiverA 

package com.example.consumer.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "topicQueueA")
@Slf4j
public class TopicReceiverA {
    @RabbitHandler
    public void process(String message){
        log.warn("A接收"+message);
    }
}

②、TopicReceiverB

package com.example.consumer.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "topicQueueB")
@Slf4j
public class TopicReceiverB {
    @RabbitHandler
    public void process(String message){
        log.warn("B接收"+message);
    }
}

③、TopicReceiverC 

package com.example.consumer.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "topicQueueC")
@Slf4j
public class TopicReceiverC {
    @RabbitHandler
    public void process(String message){
        log.warn("C接收"+message);
    }
}

④、运行测试

三、扇形交换机 

1、生产者

①、FanoutConfig 

 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;
 @Configuration
 public class FanoutConfig {

     private final static String KEY_A="*.orange.*";
     private final static String KEY_B="*.*.rabbit";
     private final static String KEY_C="lazy.#";
 
 
     @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());
     }
 }

②、ProviderController

@RequestMapping("/fanoutSend")
public String fanoutSend(){
    template.convertAndSend("fanoutExchange",null,"Hello fanout ");
    return "yes";
}

③、运行测试

2、消费者

①、FanoutReceiverA 

package com.example.consumer.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "fanoutQueueA")
@Slf4j
public class FanoutReceiverA {
    @RabbitHandler
    public void process(String message){
        log.error("A接收"+message);
    }
}

②、FanoutReceiverB 

package com.example.consumer.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "fanoutQueueB")
@Slf4j
public class FanoutReceiverB {
    @RabbitHandler
    public void process(String message){
        log.error("B接收"+message);
    }
}

③、FanoutReceiverC 

package com.example.consumer.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "fanoutQueueC")
@Slf4j
public class FanoutReceiverC {
    @RabbitHandler
    public void process(String message){
        log.error("C接收"+message);
    }
}

④、运行测试

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/744961.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号