SpringCloud学习笔记-异步-RabbitMQ(简单运用)

  1. 依赖
    
    		
    			org.springframework.boot
    			spring-boot-starter-amqp
    		

     

  2. 修改配置文件
    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        username: root
        password: 123456
        url: jdbc:mysql://127.0.0.1:3306/SpringCloud?characterEncoding=utf-8&useSSL=false
      rabbitmq:
        host: localhost
        port: 5672
        username: guest
        password: guest
    

    SpringCloud学习笔记-异步-RabbitMQ(简单运用)_第1张图片

  3. 创建队列
    SpringCloud学习笔记-异步-RabbitMQ(简单运用)_第2张图片
  4. 接收类
    package com.hx.order.message;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    /**
     * 接收mq消息
     */
    @Slf4j
    @Component
    public class MqReceiver {
    
        @RabbitListener(queues = "myQueue")
        public  void  process(String message) {
            log.info("MqReceiver:{}"+message);
        }
    }
    

     

  5. 发送类
    package com.hx.order;
    
    import org.junit.Test;
    import org.springframework.amqp.core.AmqpTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    
    /**
     * 发送mq消息测试
     */
    @Component
    public class MqSenderTest extends  OrderApplicationTests {
    
        @Autowired
        private AmqpTemplate  amqpTemplate;
    
        @Test
        public void send() {
            amqpTemplate.convertAndSend("myQueue","你是说什么啊,发送时间:"+new Date());
        }
    }
    
    package com.hx.order;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class OrderApplicationTests {
    
    	@Test
    	public void contextLoads() {
    	}
    
    }
    

     

  6. 先启动服务,再启动测试方法,测试消息能否发送

    SpringCloud学习笔记-异步-RabbitMQ(简单运用)_第3张图片
    SpringCloud学习笔记-异步-RabbitMQ(简单运用)_第4张图片
    测试成功。。。。。。
  7. 自动创建队列  @RabbitListener(queuesToDeclare = @Queue("myQueue"))
    package com.hx.order.message;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.amqp.rabbit.annotation.Queue;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    /**
     * 接收mq消息
     */
    @Slf4j
    @Component
    public class MqReceiver {
    
        // 自动创建队列
        @RabbitListener(queuesToDeclare = @Queue("myQueue"))
        public  void  process(String message) {
            log.info("MqReceiver:{}"+message);
        }
    }
    

     

  8. 自动创建 Exchange和Queue
    
    
    package com.hx.order.message;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.amqp.rabbit.annotation.Exchange;
    import org.springframework.amqp.rabbit.annotation.Queue;
    import org.springframework.amqp.rabbit.annotation.QueueBinding;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    /**
     * 接收mq消息
     */
    @Slf4j
    @Component
    public class MqReceiver {
    
        // 自动创建 Exchange和Queue
        @RabbitListener(bindings =@QueueBinding(
              value = @Queue("myQueue"),
              exchange = @Exchange("myExchange") 
        ))
        public  void  process(String message) {
            log.info("MqReceiver:{}"+message);
        }
    }
    

    不仅自动创建这俩个,还把他俩绑定在一起。
    SpringCloud学习笔记-异步-RabbitMQ(简单运用)_第5张图片SpringCloud学习笔记-异步-RabbitMQ(简单运用)_第6张图片SpringCloud学习笔记-异步-RabbitMQ(简单运用)_第7张图片

  9. 测试Exchange 交换器  key  路由   value 队列
    package com.hx.order.message;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.amqp.rabbit.annotation.Exchange;
    import org.springframework.amqp.rabbit.annotation.Queue;
    import org.springframework.amqp.rabbit.annotation.QueueBinding;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    /**
     * 接收mq消息
     */
    @Slf4j
    @Component
    public class MqReceiver {
    
        // 自动创建 Exchange和Queue
        @RabbitListener(bindings =@QueueBinding(
              value = @Queue("myQueue"),
              exchange = @Exchange("myExchange")
        ))
        public  void  process(String message) {
            log.info("MqReceiver:{}"+message);
        }
    
        /**书本
         *   exchange 组(交换器)   key  路由(钥匙)   value  队列
         * @param message
         */
        @RabbitListener(bindings =@QueueBinding(
                exchange = @Exchange("myOrder"),
                key = "book",
                value = @Queue("bookOrder")
    
        ))
        public  void  processBook(String message) {
            log.info("bookMqReceiver:{}"+message);
        }
    
    
        /**
         *
         * @param message
         */
        @RabbitListener(bindings =@QueueBinding(
                exchange = @Exchange("myOrder"),
                key = "fruit",
                value = @Queue("fruitOrder")
    
        ))
        public  void  processFruit(String message) {
            log.info("fruitMqReceiver:{}"+message);
        }
    }
    
    package com.hx.order;
    
    import org.junit.Test;
    import org.springframework.amqp.core.AmqpTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    
    /**
     * 发送mq消息测试
     */
    @Component
    public class MqSenderTest extends  OrderApplicationTests {
    
        @Autowired
        private AmqpTemplate  amqpTemplate;
    
        @Test
        public void send() {
            amqpTemplate.convertAndSend("myQueue","你是说什么啊,发送时间:"+new Date());
        }
    
        @Test
        public void sendOrder() {
            // 第一的参数是交换器  第二个是路由, 第三个是消息
            amqpTemplate.convertAndSend("myOrder","fruit","你是说什么啊,发送时间:"+new Date());
        }
    }
    

     

你可能感兴趣的:(SpringCloud)