javascript
Spring Boot2.x-15 整合RabbitMQ 及RabbitMQ的基本使用
文章目錄
- 概述
- 在Docker CE中安裝RabbitMQ
- 依賴
- 配置
- 基本使用
- 手工創(chuàng)建隊列,發(fā)送消息到指定的隊列
- 自動創(chuàng)建隊列,發(fā)送消息到指定的隊列
- 自動創(chuàng)建隊列,Exchange和隊列綁定
- 自動創(chuàng)建隊列,Exchange和隊列綁定,接收指定key的消息
- 代碼
概述
以 Spring Cloud實戰(zhàn)-06使用/actuator/bus-refresh端點手動刷新配置 + 使用Spring Cloud Bus自動更新配置中使用的幾個微服務(wù)工程為基礎(chǔ),我們梳理下整合RabbitMQ及RabbitMQ的基本用法.
官方教程: https://spring.io/guides/gs/messaging-rabbitmq/
我們這里不是官方的Demo
后續(xù)開篇系統(tǒng)的介紹RabbitMQ,這里直接上Demo了先。
先說下RabbitMQ中的幾個名詞:
- Broker:簡單來說就是消息隊列服務(wù)器實體,可以理解為一個節(jié)點
- Exchange:消息交換機,它指定消息按什么規(guī)則,路由到哪個隊列
- Queue:消息隊列載體,每個消息都會被投入到一個或多個隊列
- Binding:綁定,它的作用就是把exchange和queue按照路由規(guī)則綁定起來
- Routing Key:路由關(guān)鍵字,exchange根據(jù)這個關(guān)鍵字進行消息投遞
- vhost:虛擬主機,一個broker里可以開設(shè)多個vhost,用作不同用戶的權(quán)限分離
- Producer:消息生產(chǎn)者,投遞消息的程序
- Consumer:消息消費者,接受消息的程序
- Channel:消息通道,在客戶端的每個連接里,可建立多個channel,每個channel代表一個會話任務(wù)
在Docker CE中安裝RabbitMQ
Docker CE中安裝RabbitMQ
依賴
依賴必不可少嘛
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>配置
如果是默認的localhost和 5672端口 ,也可以不配。當然了,最好還是配上。
基本使用
工程結(jié)構(gòu) 如下
為了方便,發(fā)送方直接使用了Controller作為發(fā)送發(fā)。 當然了,最好再新建個工程。
主要的注解 @RabbitListener
手工創(chuàng)建隊列,發(fā)送消息到指定的隊列
接收方代碼
package com.artisan.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;/*** 接收RabbitMQ消息的接收方*/ @Slf4j @Component public class MessageReceive {/*** queues指定對列名,需要先手工在RabbitMQ上建立隊列artisanQueue* @param message*/@RabbitListener(queues = "artisanQueue")public void processReceivedMsg(String message){log.info("artisanQueue Received MSG : {}", message);} } 需要提前在RabbitMQ上新建好artisanQueue這個隊列,否則報錯。如下
發(fā)送發(fā)測試
package com.artisan.order.controller;import lombok.extern.slf4j.Slf4j; 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.RestController;@RestController @Slf4j public class MsgController {@Autowiredprivate AmqpTemplate amqpTemplate;@GetMapping("/sendMsg2ArtisanQueue")public void sendMsg(){log.info("begin to send msg to artisanQueue ....");this.amqpTemplate.convertAndSend("artisanQueue","[artisanQueue] I send one msg to u with RabbitMQ");} }主要使用AmqpTemplate ,注入后,調(diào)用convertAndSend即可
啟動微服務(wù),訪問 http://localhost:8081/sendMsg2ArtisanQueue
觀察消息隊列的變化 :
觀察日志:
2019-04-11 23:06:43.004 INFO 33612 --- [nio-8081-exec-1] c.a.order.controller.MsgController : begin to send msg to artisanQueue .... 2019-04-11 23:06:43.011 INFO 33612 --- [cTaskExecutor-1] c.artisan.order.message.MessageReceive : artisanQueue Received MSG : [artisanQueue] I send one msg to u with RabbitMQ自動創(chuàng)建隊列,發(fā)送消息到指定的隊列
上面的例子手工創(chuàng)建隊列,是不是很崩潰,自動創(chuàng)建還是使用@RabbitListener,換個屬性 queuesToDeclare 即可
@RabbitListener(queuesToDeclare = @Queue("artisanQueue2"))我們先刪掉 artisanQueue2 ,目前的消息隊列如下
接收方
MessageReceive 中新加個方法如下
/*** queuesToDeclare自動創(chuàng)建隊列* @param message*/@RabbitListener(queuesToDeclare = @Queue("artisanQueue2"))public void processReceivedMsg2(String message){log.info("artisanQueue2 Received MSG : {}", message);}發(fā)送方 MsgController新建個方法如下
@GetMapping("/sendMsg2ArtisanQueue2")public void sendMsg2(){log.info("begin to send msg to artisanQueue2 ....");this.amqpTemplate.convertAndSend("artisanQueue2","[artisanQueue2] I send one msg to u with RabbitMQ");}RabbitMQ管理頁面查看
日志:
2019-04-11 23:12:20.120 INFO 33612 --- [nio-8081-exec-4] c.a.order.controller.MsgController : begin to send msg to artisanQueue2 .... 2019-04-11 23:12:20.148 INFO 33612 --- [cTaskExecutor-2] c.artisan.order.message.MessageReceive : artisanQueue2 Received MSG : [artisanQueue2] I send one msg to u with RabbitMQ自動創(chuàng)建隊列,Exchange和隊列綁定
確保不影響,先刪掉 artisanQueue3
接收方:
/*** 自動創(chuàng)建隊列,Exchange和隊列綁定* @param message*/@RabbitListener(bindings = @QueueBinding(value = @Queue("artisanQueue3"),exchange = @Exchange("artisanExchange3")))public void processReceivedMsg3(String message){log.info("artisanQueue3 Received MSG : {}", message);}發(fā)送測試
@GetMapping("/sendMsg3ArtisanQueue3")public void sendMsg3(){log.info("begin to send msg to artisanQueue3 ....");this.amqpTemplate.convertAndSend("artisanQueue3","[artisanQueue3] I send one msg to u with RabbitMQ");}點擊自動創(chuàng)建的消息隊列后,查看Bindings
日志:
2019-04-11 23:17:04.772 INFO 33612 --- [nio-8081-exec-7] c.a.order.controller.MsgController : begin to send msg to artisanQueue3 .... 2019-04-11 23:17:04.790 INFO 33612 --- [cTaskExecutor-2] c.artisan.order.message.MessageReceive : artisanQueue3 Received MSG : [artisanQueue3] I send one msg to u with RabbitMQ自動創(chuàng)建隊列,Exchange和隊列綁定,接收指定key的消息
短信的消息,通過key ,僅發(fā)送到短信消息隊列中。 Email僅發(fā)送到Email的消息隊列中
接收方
/*** 自動創(chuàng)建隊列,Exchange和隊列綁定,接收指定key的消息* @param message*/@RabbitListener(bindings = @QueueBinding(value = @Queue("SMSQueue"),exchange = @Exchange("smsExchange"),key = "sms"))public void processSMSMsg(String message){log.info("SMSQueue Received MSG : {}", message);}/*** 自動創(chuàng)建隊列,Exchange和隊列綁定,接收指定key的消息* @param message*/@RabbitListener(bindings = @QueueBinding(value = @Queue("EmailQueue"),exchange = @Exchange("emailExchange"),key = "email"))public void processEmailMsg(String message){log.info("EmailQueue Received MSG : {}", message);}發(fā)送方測試
@GetMapping("/sendSMSMsg")public void sendSMSMsg(){log.info("begin to send msg to sendSMSMsg ....");this.amqpTemplate.convertAndSend("smsExchange","sms","[SMSQueue] I send one msg to u with RabbitMQ");}@GetMapping("/sendEmailMsg")public void sendEmailMsg(){log.info("begin to send msg to sendEmailMsg ....");this.amqpTemplate.convertAndSend("emailExchange","email","[EmailQueue] I send one msg to u with RabbitMQ");}啟動訪問 http://localhost:8081/sendEmailMsg
日志
2019-04-11 23:35:44.631 INFO 33612 --- [nio-8081-exec-7] c.a.order.controller.MsgController : begin to send msg to sendEmailMsg .... 2019-04-11 23:35:44.643 INFO 33612 --- [cTaskExecutor-3] c.artisan.order.message.MessageReceive : EmailQueue Received MSG : [EmailQueue] I send one msg to u with RabbitMQ訪問 http://localhost:8081/sendSMSMsg
日志
2019-04-11 23:37:16.293 INFO 33612 --- [io-8081-exec-10] c.a.order.controller.MsgController : begin to send msg to sendSMSMsg .... 2019-04-11 23:37:16.308 INFO 33612 --- [cTaskExecutor-3] c.artisan.order.message.MessageReceive : SMSQueue Received MSG : [SMSQueue] I send one msg to u with RabbitMQ代碼
https://github.com/yangshangwei/springcloud-o2o/tree/master/artisan_order
總結(jié)
以上是生活随笔為你收集整理的Spring Boot2.x-15 整合RabbitMQ 及RabbitMQ的基本使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Cloud【Finchle
- 下一篇: Spring Cloud【Finchle