栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springBoot集成RabbitMq 五种工作模式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springBoot集成RabbitMq 五种工作模式

RabbitMQ提供了6种模式:简单模式,work模式,Publish/Subscribe发布与订阅模式,Routing路由模式,Topics主题模式

导入依赖
   
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.projectlombok
            lombok
        

        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.9
        

        
            org.springframework.boot
            spring-boot-starter-amqp
        
    
application.yml配置文件
# 配置RabbitMQ的基本信息  ip 端口 username  password..
spring:
  rabbitmq:
    host: 192.168.200.130 # ip
    port: 5672
    username: admin
    password: admin
    virtual-host: /itcast
server:
  port: 8001
简单模式


消息发送者
配置文件

@Configuration
public class RabbitMQConfig1 {
	//声明队列名称
    public static final String QUEUE_NAME = "boot_queue";

}

发送消息

    @Test
    public void testSend(){

        rabbitTemplate.convertAndSend(RabbitMQConfig1.QUEUE_NAME,"这是普通模式");
    }

接收消息

@Component
public class RabbimtMQListener {

    @RabbitListener(queues = "boot_queue")
    public void ListenerQueue(Message message){
        System.out.println("消费者1"+new String(message.getBody()));
    }

}

工作模式


配置文件

@Configuration
public class RabbitMQConfig1 {

    public static final String QUEUE_NAME = "boot_queue";

}

发送消息

 //1个生产者,两个消费者,但是一个消息只能被一个消费者消费
    @Test
    public void testWork(){
        rabbitTemplate.convertAndSend(RabbitMQConfig1.QUEUE_NAME,"这是工作模式");
    }

订阅模式


配置文件

//@Configuration
public class RabbitMQConfig2 {

    public static final String EXCHANGE_NAME = "boot_fanout_exchange";
    public static final String QUEUE_NAME = "boot_queue";
    public static final String QUEUE_NAME2 = "boot_queue2";

    //1.交换机

    
    @Bean("bootExchange")
    public FanoutExchange bootExchange(){
        return new FanoutExchange(EXCHANGE_NAME);
    }


    //2.Queue 队列1
    @Bean("bootQueue")
    public Queue bootQueue(){
        return new Queue(QUEUE_NAME);
    }

    //2.Queue 队列2
    @Bean("bootQueue2")
    public Queue bootQueue2(){
        return new Queue(QUEUE_NAME2);
    }

    //绑定交换机和队列
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") FanoutExchange exchange){
        return BindingBuilder.bind(queue).to(exchange);
    }

    @Bean
    public Binding bindQueueExchange2(@Qualifier("bootQueue2") Queue queue, @Qualifier("bootExchange") FanoutExchange exchange){
        return BindingBuilder.bind(queue).to(exchange);
    }


}

发送消息

   //订阅模式
//     1、1个生产者,多个消费者
//     2、每一个消费者都有自己的一个队列
//     3、生产者没有将消息直接发送到队列,而是发送到了交换机
//     4、每个队列都要绑定到交换机
//     5、生产者发送的消息,经过交换机,到达队列,实现,一个消息被多个消费者获取的目的
//    ""不能省略,否则会发送成功,但是接受不到
    @Test
    public void TestPublist(){
        rabbitTemplate.convertAndSend(RabbitMQConfig2.EXCHANGE_NAME,"","这是订阅者模式");
    }
路由模式

配置文件

//@Configuration
public class RabbitMQConfig3 {

    public static final String EXCHANGE_NAME = "boot_direct_exchange";
    public static final String QUEUE_NAME = "boot_queue";
    public static final String QUEUE_NAME2= "boot_queue2";

    //1.交换机
    @Bean("bootExchange")
    public DirectExchange bootExchange(){
        return new DirectExchange(EXCHANGE_NAME);
    }


    //2.Queue 队列1
    @Bean("bootQueue")
    public Queue bootQueue(){
        return new Queue(QUEUE_NAME);
    }

    //2.Queue 队列2
    @Bean("bootQueue2")
    public Queue bootQueue2(){
        return new Queue(QUEUE_NAME2);
    }

    //3. 队列和交互机绑定关系 Binding
    
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("key1");
    }

    @Bean
    public Binding bindQueueExchange2(@Qualifier("bootQueue2") Queue queue, @Qualifier("bootExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("key2");
    }


}

发送消息

 //路由模式
//    队列与交换机的绑定,不能是任意绑定了,而是要指定一个`RoutingKey`(路由key)
//    消息的发送方在 向 Exchange发送消息时,也必须指定消息的 `RoutingKey`。
//    Exchange不再把消息交给每一个绑定的队列,而是根据消息的`Routing Key`进行判断,
//    只有队列的`Routingkey`与消息的 `Routing key`完全一致,才会接收到消息
    @Test
    public void TestDirect(){
        rabbitTemplate.convertAndSend(RabbitMQConfig3.EXCHANGE_NAME,"key1","这是路由模式");
    }

topic通配符模式

配置文件

//@Configuration
public class RabbitMQConfig4 {

    public static final String EXCHANGE_NAME = "boot_topic_exchange";
    public static final String QUEUE_NAME = "boot_queue";
    public static final String QUEUE_NAME2= "boot_queue2";

    //1.交换机
    @Bean("bootExchange")
    public TopicExchange bootExchange(){
        return new TopicExchange(EXCHANGE_NAME);
    }


    //2.Queue 队列1
    @Bean("bootQueue")
    public Queue bootQueue(){
        return new Queue(QUEUE_NAME);
    }

    //2.Queue 队列2
    @Bean("bootQueue2")
    public Queue bootQueue2(){
        return new Queue(QUEUE_NAME2);
    }

    //3. 队列和交互机绑定关系 Binding
    
    @Bean
    public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") TopicExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("topic1.*");
    }

    @Bean
    public Binding bindQueueExchange2(@Qualifier("bootQueue2") Queue queue, @Qualifier("bootExchange") TopicExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("topic2.*");
    }


}

发送消息

//    //Topics通配符模式
//    `Topic`类型与`Direct`相比,都是可以根据`RoutingKey`把消息路由到不同的队列。
//    只不过`Topic`类型`Exchange`可以让队列在绑定`Routing key` 的时候**使用通配符**!
//            `Routingkey` 一般都是有一个或多个单词组成,多个单词之间以”.”分割,例如: `item.insert`
//    通配符规则:
//            #:匹配一个或多个词
//            *:匹配不多不少恰好1个词
    @Test
    public void TestTopic(){
        rabbitTemplate.convertAndSend(RabbitMQConfig4.EXCHANGE_NAME,"topic1.aa","这是topic模式");
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/301972.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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