柚子快報(bào)邀請(qǐng)碼778899分享:分布式 RabbitMQ基礎(chǔ)
柚子快報(bào)邀請(qǐng)碼778899分享:分布式 RabbitMQ基礎(chǔ)
目錄
1同步調(diào)用
2異步調(diào)用
?3RabbitMQ介紹
4入門?
1數(shù)據(jù)隔離
2 spring amqp?
5 hello word 案例
?1引入spring-amgp依賴
?2配置RabbitMQ服務(wù)端信息
3發(fā)送消息
4接收消息
6 Work Queues
7交換機(jī)
1Fanout交換機(jī)
創(chuàng)建隊(duì)列
可以使用默認(rèn)的交換機(jī)也可以自己創(chuàng)建交換機(jī)
?編輯?交換機(jī)與隊(duì)列進(jìn)行綁定
?編輯?2Direct交換機(jī)
3Topic交換機(jī)
4Topic交換機(jī)?
5申明隊(duì)列和交換機(jī)?
6消息轉(zhuǎn)換器
1同步調(diào)用
按照123的順序依次執(zhí)行
?如果每個(gè)業(yè)務(wù)都用50ms,呢么整個(gè)支付服務(wù)完成,則需要的時(shí)間是幾個(gè)小demo的時(shí)間加和
所以同步調(diào)用會(huì)出現(xiàn) 擴(kuò)展性差 性能下降 級(jí)聯(lián)失?。ㄒ粋€(gè)小demo失效了,整個(gè)業(yè)務(wù)都失效了)的問(wèn)題,但是某些業(yè)務(wù)只能使用同步調(diào)用
2異步調(diào)用
這里的消息代理類比于外賣柜,消息發(fā)送者類比于外賣員,消息接受者類比于點(diǎn)外賣的人?
更改為異步調(diào)用就如下
?3RabbitMQ介紹
使用docker安裝
docker run
-e RABBITMQ DEFAULT USER=itheima\ 這里設(shè)置用戶名和密碼
-e RABBITMO DEFAULT PASS=123321\
-v mq-plugins:/plugins
--name mq\
--hostname mq\
-p15672:15672\
-p5672:56721
--network hmall\
-d\
rabbitmq:3.8-management
安裝完成后 訪問(wèn)192.168.150.101:15672
基本模型
4入門?
創(chuàng)建隊(duì)列
交換機(jī)負(fù)責(zé)路由轉(zhuǎn)發(fā)
此時(shí)再看queue里邊 已經(jīng)和交換機(jī)綁定
1數(shù)據(jù)隔離
通過(guò)virtual host
創(chuàng)建一個(gè)用戶
不同的virtual host對(duì)數(shù)據(jù)隔離
2 spring amqp?
5 hello word 案例
沒(méi)有交換機(jī)
?1引入spring-amgp依賴
?2配置RabbitMQ服務(wù)端信息
3發(fā)送消息
@SpringBootTest
public class SpringAmqpTest
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
void testSendMessage2Queue(){
String queueName "simple.queue";
String msg "hello,amqp!";
rabbitTemplate.convertAndSend (queueName,msg);
}
4接收消息
@Slf4j
@Component
public class MqListener
@RabbitListener (queues "simple.queue")
public void listensimpleQueue(string msg){
System.out.println("消費(fèi)者收到了simple.queuel的消息:【"+msg+"】r);
}
?
6 Work Queues
收消息?
@RabbitListener(queues "work.queue")
public void listenworkQueuel(String msg){
System.out.println("消費(fèi)者1收到了work.queue的消息:【"+msg+"】");
}
@RabbitListener (queues "work,queue")
public void listenworkQueue2(String msg){
System.out.println("消費(fèi)者2收到了work.queue的消息:【"+msg+"】");
}
發(fā)消息
@Test
void testworkQueue(){
String queueName "work.queue";
for (int i 1;i <50;i++){
String msg hello,worker,message_+i;
rabbitTemplate.convertAndSend(queueName,msg);
}
}
實(shí)現(xiàn)能者多勞?
7交換機(jī)
1Fanout交換機(jī)
?廣播模式
創(chuàng)建隊(duì)列
可以使用默認(rèn)的交換機(jī)也可以自己創(chuàng)建交換機(jī)
?交換機(jī)與隊(duì)列進(jìn)行綁定
?2Direct交換機(jī)
綁定key
接收
發(fā)送
@Test
void testSendDirect(){
String exchangeName ="hmall direct";
string msg="紅色警報(bào)";
rabbitTemplate.convertAndSend(exchangeName,routingKey:"red",msg);
}
3Topic交換機(jī)
4Topic交換機(jī)?
topic能干的事direct也能干,但是topic更加方便
5申明隊(duì)列和交換機(jī)?
代碼實(shí)現(xiàn) 不使用可視化
基于代碼
基于注解(這個(gè)更好用 更便利 更能適合于場(chǎng)景)
6消息轉(zhuǎn)換器
Spring的對(duì)消息對(duì)象的處理是由org.springframework.amqp.support.converter.MessageConverter來(lái)處理的。而 默認(rèn)實(shí)現(xiàn)是SimpleMessageConverter,基于JDK的ObjectOutputStream完成序列化。 存在下列問(wèn)題: ·JDK的序列化有安全風(fēng)險(xiǎn) ·DK序列化的消息太大 ·JDK序列化的消息可讀性差
解決方案:
柚子快報(bào)邀請(qǐng)碼778899分享:分布式 RabbitMQ基礎(chǔ)
文章鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。