rabbitMQ的簡單使用

2021-09-03 01:27:36 字數 3792 閱讀 3630

交換機模式:

交換機模式2:fanout模式:

交換機模式3:headers模式,

總結:生產者:製造訊息

消費者:獲取訊息,處理訊息

路由機制:中轉訊息

四種模式:

後三種是交換機模式

direct:不需要交換機,直接傳送到指定佇列,一對一

topic: 需要key

fanout: 廣播模式,不需要key

headers:注意模式比較奇特,需要乙個map,裡面包含需要的header,只有匹配(或者不匹配)其中的header,才能放到對應的佇列中

**demo:

direct模式:

@configuration

public

class

mqconfig

}

@service

@slf4j

public

class

mqsender

,msg:{} "

, mqconfig.queue,msg)

; amqptemplate.

convertandsend

( mqconfig.queue,msg);}

}

@service

@slf4j

public

class

mqreceiver

,data:{}"

,mqconfig.queue,message);}

}

很直接,除了生產者和消費者,就沒有更多的部分

比direct多了個中間商(媒婆),交換機

生產者傳送訊息時需要指定key和交換機,交換機知道key對應的queue,那麼交換機就能將訊息放到適合的佇列中,交換機任務也就完成了。

@configuration

public

class

mqconfig

@bean

public queue topicqueue2()

@bean

public topicexchange topicexchange()

/** *給佇列繫結上key和交換機

*/@bean

public binding topicbinding1()

@bean

public binding topicbinding2()

}

傳送訊息時需要指定交換機和key

@service

@slf4j

public

class

mqsender

",msg)

; amqptemplate.

convertandsend

(mqconfig.topic_exchange,

"topic.key1"

,msg+

" 1");

amqptemplate.

convertandsend

(mqconfig.topic_exchange,

"topic.key2"

,msg+

" 2");

}}

只需要監聽需要的佇列即可

@service

@slf4j

public

class

mqreceiver

",message);}

@rabbitlistener

( queues = mqconfig.topic_queue2)

public

void

receivetopic2

(string message)

",message);}

}

生產者->message+key1->exchange->queue(key1)->消費者

廣播模式,不指定key,通過交換機將訊息發給與該交換機繫結的佇列

@configuration

public

class

mqconfig

/** * 廣播模式繫結

*/@bean

public binding fanoutbinding1()

@bean

public binding fanoutbinding2()

}

@service

@slf4j

public

class

mqsender

",msg)

; amqptemplate.

convertandsend

(mqconfig.fanout_exchange,

"",msg+

" fanout 1");

}}

同topic模式下的消費者

無需key,傳送的是一條訊息,但是接受這條小東西的佇列一般是多個(廣播)

匹配要求很多(通過乙個map的key和value逐個與訊息傳送時設定的properties相比對,有wehereall,whereany,where 等等)

@configuration

public

class

mqconfig

@bean

public queue headerqueue()

@bean

public binding headerbinding()

}

@service

@slf4j

public

class

mqsender

",msg)

; messageproperties properties=

newmessageproperties()

; properties.

setheader

("header1"

,"value1");

properties.

setheader

("header2"

,"value2");

message obj=

newmessage

(msg.

getbytes()

,properties)

; amqptemplate.

convertandsend

(mqconfig.header_exchange,

"",obj);}

}

比較特別,傳送和接收都是byte

@service

@slf4j

public

class

mqreceiver

",msg);}

}

匹配很靈活,相當於是復合的key;訊息是被轉為byte傳輸和接收

direct模式:非交換機模式,直接傳送各某個佇列

topic模式:交換機+key,交換機通過訊息的key去匹配哪個佇列可以被放入此訊息

fanout模式:交換機+廣播 ,不使用key,交換機直接將資訊放入與其繫結的佇列

headers模式:交換機+map,可以看成是復合的key,交換機去將訊息的properties與佇列繫結時設定的header map值取匹配

rabbitmq簡單使用

安裝rabbitmq,進入sbin執行rabbitmq service start 傳送者 package com.zhy.rabbit.01 import com.rabbitmq.client.channel import com.rabbitmq.client.connection impor...

RabbitMQ簡單使用

參考部落格 rabbitmq是乙個在amqp協議標準基礎上完整的,可服用的企業訊息系統。它遵循mozilla public license開源協議,採用 erlang 實現的工業級的訊息佇列 mq 伺服器,rabbit mq 是建立在erlang otp平台上。windows下使用cmd 以管理員身...

rabbitMq安裝和簡單使用

首先要在 etc yum.repos.d 建立乙個檔案檔案的結尾必須以.repo結尾。eg rabbitmq.repo centos7中新增下面的內容 bintray rabbitmq server name bintray rabbitmq rpm baseurl gpgcheck 0 repo ...