RabbitMq簡單模式 工作模式

2021-09-24 23:15:08 字數 4555 閱讀 4614

生產者的訊息被負載均衡到各個消費者去,一條訊息只會被乙個消費者消費,不會產生一條訊息重複消費的問題

amqp://user:pass@ip:5672/virtualhost

type rabbitmq struct
錯誤處理函式failonerr是在有錯誤的時候列印後面的引數和錯誤資訊
func newrabbitmq(queuename, exchange, key string) *rabbitmq 

var err error

//建立rabbitmq連線

rabbitmq.conn, err = amqp.dial(rabbitmq.mqurl)

rabbitmq.failonerr(err, "建立連線錯誤")

rabbitmq.channel, err = rabbitmq.conn.channel()

rabbitmq.failonerr(err, "獲取channel失敗")

return rabbitmq

}

func (r *rabbitmq) destroy()
簡單模式下建立rabbitmq例項,只需要傳入佇列名稱

func newrabbitmq******(queuename string) *rabbitmq
使用channel.queuedeclare申請佇列,然後通過channel.publish中把訊息推送到佇列中

func (r *rabbitmq) publish******(message string) 

//2傳送訊息到佇列中

r.channel.publish(

//交換機

"", //簡單模式為空

//routingkey

r.queuename,

//mandatory如果為true會根據exchange型別和routekey規則,

//如果無法找到符合條件的佇列就把訊息返還給傳送者

false,

//immediate如果為true,當exchage傳送訊息到佇列後,

//如果發現佇列上沒有繫結消費者就把訊息返還給傳送者

false,

//傳送的資訊

amqp.publishing)

}

申請channel佇列,然後通過channel.consume獲得生產者的訊息

func (r *rabbitmq) consume******() 

//2接收訊息

msgs, err := r.channel.consume(

//佇列名稱

r.queuename,

//用來區分多個消費者,為空不區分

"",//autoack是否自動應答,如果把訊息消費了,主動告訴rabbitmq伺服器

true,

//是否具有排他性,只能檢視到我自己賬號建立的訊息,和pulish對應

false,

//nolocal如果設定為true,不能將同乙個connection傳送的訊息,傳遞給同乙個connection中的其他消費者

false,

//是否阻塞,false阻塞

false,

//額外引數

nil)

if err != nil

forever := make(chan bool)

go func()

}()log.printf("[*] waiting for message, exit to press ctrl + c")

<-forever

}

生產者

package main

import (

"fmt"

"rabbitmq/rabbitmq"

)func main()

消費者

package main

import "rabbitmq/rabbitmq"

func main()

生產者把訊息("hello imooc")推送到rabbitmq伺服器

消費者獲取到生產者的訊息,並列印

輸出結果為:

2019/07/04 11:12:35 [*] waiting for message, exit to  press ctrl + c

2019/07/04 11:12:46 received a message:hello imooc

上面的案例是一對一的模式,如果要使用一對多、多對多的模式,就是在簡單模式下多個生產者多個消費者 

生產者

package main

import (

"rabbitmq/rabbitmq"

"strconv"

"time"

)func main()

}***********************************===

package main

import (

"rabbitmq/rabbitmq"

"strconv"

"time"

)func main()

}

消費者

package main

import "rabbitmq/rabbitmq"

func main()

****************************************==

package main

import "rabbitmq/rabbitmq"

func main()

多個生產者往同乙個佇列中推送訊息,消費者平均獲取各個生產者訊息,每個訊息只會被消費一次,不會出現重複模式

消費者1輸出結果

2019/07/04 11:19:58 [*] waiting for message, exit to  press ctrl + c

2019/07/04 11:20:02 received a message:one hello imooc1

2019/07/04 11:20:02 received a message:one hello imooc3

2019/07/04 11:20:02 received a message:one hello imooc5

2019/07/04 11:20:02 received a message:one hello imooc7

2019/07/04 11:20:03 received a message:one hello imooc9

2019/07/04 11:20:04 received a message:two hello imooc1

2019/07/04 11:20:04 received a message:two hello imooc3

2019/07/04 11:20:04 received a message:two hello imooc5

2019/07/04 11:20:05 received a message:two hello imooc7

2019/07/04 11:20:05 received a message:two hello imooc9

消費者2輸出結果

2019/07/04 11:19:55 [*] waiting for message, exit to  press ctrl + c

2019/07/04 11:20:02 received a message:one hello imooc0

2019/07/04 11:20:02 received a message:one hello imooc2

2019/07/04 11:20:02 received a message:one hello imooc4

2019/07/04 11:20:02 received a message:one hello imooc6

2019/07/04 11:20:02 received a message:one hello imooc8

2019/07/04 11:20:04 received a message:two hello imooc0

2019/07/04 11:20:04 received a message:two hello imooc2

2019/07/04 11:20:04 received a message:two hello imooc4

2019/07/04 11:20:05 received a message:two hello imooc6

2019/07/04 11:20:05 received a message:two hello imooc8

簡單模式Hello World

功能 乙個生產者p傳送訊息到佇列q,乙個消費者c接收 生產者實現思路 建立連線工廠connectionfactory,設定服務位址127.0.0.1,埠號5672,設定使用者名稱 密碼 virtual host,從連線工廠中獲取連線connection,使用連線建立通道channel,使用通道cha...

RabbitMQ工作模式

rabbitmq工作模式有六種 工作佇列模式,發布訂閱模式,路由模式,萬用字元模式,header模式和rpc模式。1.工作佇列模式 2.發布訂閱模式 交換機型別為funout。3.路由模式 交換機型別為direct。路由模式和發布訂閱模式區別 1 發布訂閱模式不需要繫結routingkey,訊息傳送...

rabbitmq 工作模式

c1 c2輪流接收訊息 啟動兩個接受者 channel.queuedeclare queue que durable false,exclusive false,autodelete false,arguments null 交換機設定為空字串 p發給乙個交換機,這個交換機繫結兩個佇列,c1 c2各...