一、獲取鏡像
#指定版本,該版本包含了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
三、訪問(wèn)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的方法步驟的文章就介紹到這了,更多相關(guān)docker安裝rabbitmq內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!