您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)docker快速安裝rabbitmq的詳細(xì)流程步驟,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
一、獲取鏡像
#指定版本,該版本包含了web控制頁(yè)面 docker pull rabbitmq:management
二、運(yùn)行鏡像
#方式一:默認(rèn)guest 用戶,密碼也是 guest docker run -d --hostname my-rabbit --name rabbit -p 15672:15672 -p 5672:5672 rabbitmq:management #方式二:設(shè)置用戶名和密碼 docker run -d --hostname my-rabbit --name rabbit -e RABBITMQ_DEFAULT_USER=user -e RABBITMQ_DEFAULT_PASS=password -p 15672:15672 -p 5672:5672 rabbitmq:management
三、訪問ui頁(yè)面
http://localhost:15672/
四、golang案例
#producer生產(chǎn)者代碼 package main import ( "fmt" "log" "github.com/streadway/amqp" ) const ( //AMQP URI uri = "amqp://guest:guest@10.0.0.11:5672/" // 10.0.0.11為主機(jī)ip //Durable AMQP exchange name exchangeName = "" //Durable AMQP queue name queueName = "test-queues" //Body of message bodyMsg string = "hello angel" ) //如果存在錯(cuò)誤,則輸出 func failOnError(err error, msg string) { if err != nil { log.Fatalf("%s: %s", msg, err) panic(fmt.Sprintf("%s: %s", msg, err)) } } func main() { //調(diào)用發(fā)布消息函數(shù) publish(uri, exchangeName, queueName, bodyMsg) log.Printf("published %dB OK", len(bodyMsg)) } //發(fā)布者的方法 //@amqpURI, amqp的地址 //@exchange, exchange的名稱 //@queue, queue的名稱 //@body, 主體內(nèi)容 func publish(amqpURI string, exchange string, queue string, body string) { //建立連接 log.Printf("dialing %q", amqpURI) connection, err := amqp.Dial(amqpURI) failOnError(err, "Failed to connect to RabbitMQ") defer connection.Close() //創(chuàng)建一個(gè)Channel log.Printf("got Connection, getting Channel") channel, err := connection.Channel() failOnError(err, "Failed to open a channel") defer channel.Close() log.Printf("got queue, declaring %q", queue) //創(chuàng)建一個(gè)queue q, err := channel.QueueDeclare( queueName, // name false, // durable false, // delete when unused false, // exclusive false, // no-wait nil, // arguments ) failOnError(err, "Failed to declare a queue") log.Printf("declared queue, publishing %dB body (%q)", len(body), body) // Producer只能發(fā)送到exchange,它是不能直接發(fā)送到queue的 // 現(xiàn)在我們使用默認(rèn)的exchange(名字是空字符)這個(gè)默認(rèn)的exchange允許我們發(fā)送給指定的queue // routing_key就是指定的queue名字 err = channel.Publish( exchange, // exchange q.Name, // routing key false, // mandatory false, // immediate amqp.Publishing{ Headers: amqp.Table{}, ContentType: "text/plain", ContentEncoding: "", Body: []byte(body), }) failOnError(err, "Failed to publish a message") }
五、擁有消息確認(rèn)的代碼
#producer package main import ( "fmt" "github.com/streadway/amqp" "log" "os" "strings" ) const ( //AMQP URI uri = "amqp://guest:guest@10.0.0.11:5672/" //Durable AMQP exchange name exchangeName = "" //Durable AMQP queue name queueName = "test-queues-acknowledgments" ) //如果存在錯(cuò)誤,則輸出 func failOnError(err error, msg string) { if err != nil { log.Fatalf("%s: %s", msg, err) panic(fmt.Sprintf("%s: %s", msg, err)) } } func main() { bodyMsg := bodyFrom(os.Args) //調(diào)用發(fā)布消息函數(shù) publish(uri, exchangeName, queueName, bodyMsg) log.Printf("published %dB OK", len(bodyMsg)) } func bodyFrom(args []string) string { var s string if (len(args) < 2) || os.Args[1] == "" { s = "hello angel" } else { s = strings.Join(args[1:], " ") } return s } //發(fā)布者的方法 //@amqpURI, amqp的地址 //@exchange, exchange的名稱 //@queue, queue的名稱 //@body, 主體內(nèi)容 func publish(amqpURI string, exchange string, queue string, body string) { //建立連接 log.Printf("dialing %q", amqpURI) connection, err := amqp.Dial(amqpURI) failOnError(err, "Failed to connect to RabbitMQ") defer connection.Close() //創(chuàng)建一個(gè)Channel log.Printf("got Connection, getting Channel") channel, err := connection.Channel() failOnError(err, "Failed to open a channel") defer channel.Close() log.Printf("got queue, declaring %q", queue) //創(chuàng)建一個(gè)queue q, err := channel.QueueDeclare( queueName, // name false, // durable false, // delete when unused false, // exclusive false, // no-wait nil, // arguments ) failOnError(err, "Failed to declare a queue") log.Printf("declared queue, publishing %dB body (%q)", len(body), body) // Producer只能發(fā)送到exchange,它是不能直接發(fā)送到queue的。 // 現(xiàn)在我們使用默認(rèn)的exchange(名字是空字符)。這個(gè)默認(rèn)的exchange允許我們發(fā)送給指定的queue。 // routing_key就是指定的queue名字。 err = channel.Publish( exchange, // exchange q.Name, // routing key false, // mandatory false, // immediate amqp.Publishing{ Headers: amqp.Table{}, ContentType: "text/plain", ContentEncoding: "", Body: []byte(body), }) failOnError(err, "Failed to publish a message") }
關(guān)于docker快速安裝rabbitmq的詳細(xì)流程步驟就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。