RabbitMQ交換機規則例項

2022-03-28 03:02:02 字數 3837 閱讀 3074

rabbitmq exchange分發訊息時根據型別的不同分發策略有區別,目前共四種型別:direct、fanout、topic、headers 。headers 匹配 amqp 訊息的 header 而不是路由鍵,此外 headers 交換器和 direct 交換器完全一致,但效能差很多,目前幾乎用不到了。下面分別以例項的方式對這幾種exchange進行講解。

首先我們以路由的方式對訊息進行過濾,**如下:

生產者

1

public

class

routingsenddirect ;67

public

static

void

main(string args) throws ioexception, timeoutexception

18channel.close();

19connection.close();20}

21 }

消費者

1

public

class

receivedirect1 ;67

public

static

void

main(string args) throws ioexception, timeoutexception

2021 system.out.println("

receivedirect1 waiting for messages");

22 consumer consumer = new

defaultconsumer(channel)

28};

2930 channel.basicconsume(queuename, true

, consumer);31}

32 }

1

public

class

receivedirect2 ;67

public

static

void

main(string args) throws ioexception, timeoutexception

2021 system.out.println("

receivedirect2 waiting for messages");

22 consumer consumer = new

defaultconsumer(channel)

28};

2930 channel.basicconsume(queuename, true

, consumer);31}

32 }

執行結果如下:

1 routingsenddirect sendinfo':'

routingsenddirect send the message level:info

2 routingsenddirect sendwarning':'

routingsenddirect send the message level:warning

3 routingsenddirect senderror':'

routingsenddirect send the message level:error

45 receivedirect1 exchange:direct_test, queue:amq.gen-hsurzbjzto-k7heigxsefq, bindroutingkey:info

6 receivedirect1 exchange:direct_test, queue:amq.gen-hsurzbjzto-k7heigxsefq, bindroutingkey:warning

7 receivedirect1 waiting for

messages

8 receivedirect1 received '

info

':'routingsenddirect send the message level:info

'9 receivedirect1 received '

warning

':'routingsenddirect send the message level:warning'10

11 receivedirect2 exchange:direct_test, queue:amq.gen-i3ny12l3dqwjgapabocdwq, bindroutingkey:error

12 receivedirect2 waiting for

messages

13 receivedirect2 received '

error

':'routingsenddirect send the message level:error

'

fanout和別的mq的發布/訂閱模式類似,例項如下:

生產者  

1

public

class

pub

16channel.close();

17connection.close();18}

19 }

消費者

public

class

sub

};channel.basicconsume(queuename,

true, consumer);//

佇列會自動刪除

}}

生產者  

1

public

class

topicsend ;

25//

傳送訊息

26for

(string severity :routingkeys)

31 }catch

(exception e)

37 }finally42}

43}44 }

消費者 

1

public

class

receivelogstopic1 ;

15//

繫結路由

16for

(string routingkey : routingkeys)

20 system.out.println("

receivelogstopic1 waiting for messages");

2122 consumer consumer = new

defaultconsumer(channel)

28};

29 channel.basicconsume(queuename, true

, consumer);30}

31 }

1  public class

receivelogstopic2 ;

14//

繫結路由關鍵字

15for

(string bindingkey : routingkeys)

1920 system.out.println("

receivelogstopic2 waiting for messages");

2122 consumer consumer = new

defaultconsumer(channel)

28};

29 channel.basicconsume(queuename, true

, consumer);30}

31 }

RabbitMQ 交換機模式

在說正題之前先解釋一下交換機模式是個籠統的稱呼,它不是乙個單獨的模式 包括了訂閱模式,路由模式和主題模式 交換機模式是乙個比較常用的模式,主要是為了實現資料的同步。首先,說一下訂閱模式,就和字面上的意思差不多主要就是乙個生產者,多個消費者,同乙個訊息被多個消費者獲取,先看一下官網的圖示 整體執行過程...

rabbitmq 理論 主題交換機

儘管直連交換機能夠改善我們的系統,但是它也有它的限制 沒辦法基於多個標準執行路由操作。在我們的日誌系統中,我們不只希望訂閱基於嚴重程度的日誌,同時還希望訂閱基於傳送 的日誌。unix工具syslog就是同時基於嚴重程度 severity info warn crit.和 裝置 facility au...

RabbitMQ之Exchange交換機

rabbitmq中的exchange的作用 訊息佇列,訊息通過傳送和exchange之後最終到達的地方,到達queue的訊息及進入了等待消費的狀態。每個訊息都會被傳送到乙個或多個佇列。佇列的常用屬性name 佇列的名稱 durability 是否需要持久化,true為持久化 auto delete ...