第一步:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
第二步yml中配置rabbitMq地址:
spring: rabbitmq: host: 10.10.10.103 port: 5672 username: guest password: guest
第三步 接收方:
package com.example.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; /** * @Title: MqReceiver * @ProjectName order * @date 2019/11/2011:06 */ @Slf4j @Component public class MqReceiver { //第一种方法 , 需要先创建队列 // 1. @RabbitListener(queues = "myQueue") /*@RabbitListener(queues = "myQueue") public void process1(Object message) { log.info("MqReceiver message:{}" , message); }*/ //2. 自动创建队列 @RabbitListener(queuesToDeclare = @Queue("myQueue")) /*@RabbitListener(queuesToDeclare = @Queue("myQueue")) public void process2(Object message) { log.info("MqReceiver message:{}" , message); }*/ //3. 自动创建, Exchange和Queue绑定 @RabbitListener(bindings = @QueueBinding(value = @Queue("myQueue"),exchange = @Exchange("myExchange"))) public void process(String message) { log.info("MqReceiver: {}", message); } /** * 分组 * 数码供应商服务 接收消息 * @param message */ @RabbitListener(bindings = @QueueBinding(exchange = @Exchange("myOrder"),key = "computer",value = @Queue("computerOrder"))) public void processComputer(String message) { log.info("computer MqReceiver: {}", message); } /** * 水果供应商服务 接收消息 * @param message */ @RabbitListener(bindings = @QueueBinding(exchange = @Exchange("myOrder"),key = "fruit",value = @Queue("fruitOrder"))) public void processFruit(String message) { log.info("fruit MqReceiver: {}", message); } }
第四步 发送方:
package com.example.order.controller; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; /** * @Title: MqController * @ProjectName order * @date 2019/11/2011:32 */ @RestController @RequestMapping("mq") public class MqController { @Autowired private AmqpTemplate amqpTemplate; // 第1,2中方式 @GetMapping("/send1") public void send1() { amqpTemplate.convertAndSend("myQueue", "now : " + new Date()); } // 方式3发送 @GetMapping("/send2") public void send2() { amqpTemplate.convertAndSend("myOrder","computer", "now : " + new Date()); } @GetMapping("/send3") public void send3() { amqpTemplate.convertAndSend("myQueue","myExchange", "now : " + new Date()); } @GetMapping("/send4") public void send4() { amqpTemplate.convertAndSend("myOrder","fruit", "now : " + new Date()); } }