Rabbit介面操作
- 新建佇列:輸入Name -> 新增佇列
- 新建交換機:輸入Name -> 選擇Type -> 新增交換機
- 交換機繫結佇列:選擇交換機 -> 選擇佇列 -> 輸入Routing Key -> 繫結
SpringBoot中:
首先要明確:生產者只負責給交換機傳送資料,消費者只負責從佇列中獲取資料。生產者程式碼:
@Controller @RequestMapping("/publisher") @RequiredArgsConstructor public class rabbitMQPublisher { final RabbitTemplate rabbitTemplate; @PostMapping("/send") public Result sendMessage(@RequestParam String routingKey,@RequestParam String message) { String exchangeName = "Rabbit.topic"; rabbitTemplate.convertAndSend(exchangeName,routingKey, message); return Result.ok(); } }
消費者程式碼:
@Component @Slf4j public class rabbitMQCosumer { @RabbitListener(queues = "topic.queue1") public void consume1(String msg) { log.debug("consume1接收到訊息:{}",msg); } @RabbitListener(queues = "topic.queue2") public void consume2(String msg) { log.debug("consume2接收到訊息:{}",msg); } }