work hard work smart

专注于Java后端开发。 不断总结,举一反三。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

这里使用spring-cloud-starter-bus-amqp集成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());
    }