zoukankan      html  css  js  c++  java
  • RabbitMQ基本使用

    一、RabbitMQ基本使用

    1、在order中server模块的pom.xml增加

    <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-bus-amqp</artifactId>
            </dependency>
    

      

     

    2、增加接收Mq消息

    package com.example.message;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    /**
     * 接收Mq消息
     * Created by Think on 2019/2/14.
     */
    @Slf4j
    @Component
    public class MqReceiver {
    
        @RabbitListener(queues = "myQueue")
        public void process(String message){
            log.info("MqReceiver:{}",message);
        }
    }
    

      

    3、增加发送Mq消息

    /**
     * Mq消息发送方
     * Created by Think on 2019/2/14.
     */
    @Component
    public class MqSenderTest extends OrderApplicationTests {
    
        @Autowired
        private AmqpTemplate amqpTemplate;
    
        @Test
        public void send(){
            //myQueue 队列的名称
            amqpTemplate.convertAndSend("myQueue", "Hello,now " + new Date());
        }
    
    }
    

      

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class OrderApplicationTests {
    
    	@Test
    	public void contextLoads() {
    	}
    
    }
    

      

    4、在MQ管理页面增加myQueue的消息队列

    5、测试结果

    可以看到消息接收到的图

    二、自动创建队列名称

    @Slf4j
    @Component
    public class MqReceiver {
    
        //1. @RabbitListener(queues = "myQueue")
        //2. 自动创建队列
        @RabbitListener(queuesToDeclare = @Queue("myQueue"))
        public void process(String message){
            log.info("MqReceiver:{}",message);
        }
    }
    

      

    三、Exchange和Queue绑定

    public class MqReceiver {
    
        //1. @RabbitListener(queues = "myQueue")
        //2. 自动创建队列   @RabbitListener(queuesToDeclare = @Queue("myQueue"))
        //3. 自动创建,Exchange和Queue绑定
        @RabbitListener(bindings = @QueueBinding(
            value = @Queue("myQueue"),
            exchange = @Exchange("myExchange")
        ))
        public void process(String message){
            log.info("MqReceiver:{}",message);
        }
    }
    

      

    如下图

    四、消息分组发送

    public class MqReceiver {
    
    
        /**
         * 模拟服务1, 接收消息
         * @param message
         */
        @RabbitListener(bindings = @QueueBinding(
                exchange = @Exchange("myOrder"),
                key = "computer",
                value = @Queue("computerOrder")
    
        ))
        public void processComputer(String message){
            log.info("computer MqReceiver:{}",message);
        }
    
        /**
         * 模拟服务2, 接收消息
         * @param message
         */
        @RabbitListener(bindings = @QueueBinding(
                exchange = @Exchange("myOrder"),
                key = "fruit",
                value = @Queue("fruitOrder")
    
        ))
        public void processFruit(String message){
            log.info("Fruit MqReceiver:{}",message);
        }
    }
    

      定义两个消息接收,一个key为computer,一个为fruit

    消息发送方

     @Test
        public void sendOrder(){
            //myQueue 队列的名称
            amqpTemplate.convertAndSend("myOrder","computer", "Hello Computer,now " + new Date());
        }
    
        @Test
        public void sendOrder2(){
            //myQueue 队列的名称
            amqpTemplate.convertAndSend("myOrder","fruit", "Hello Fruit,now " + new Date());
        }
    

      

  • 相关阅读:
    类和对象代码练习1
    搬迁MSSQL出现的问题
    QQ群空间是我见过最烂的系统
    导出表数据脚本的存储过程 [转自CSDN]
    plesk for Linux 没有默认首页的优先级别设置
    传说中的otag开源是假的。BS!!!!!
    FTP关闭mput上传确认
    刚装的vs2008不能生成silverlight,郁闷中
    怎么查域名反向解析是否成功
    现在的黑客太有才了。。。/不喜欢开防火墙的要小心
  • 原文地址:https://www.cnblogs.com/linlf03/p/10374428.html
Copyright © 2011-2022 走看看