一、获取镜像
1
2
|
#指定版本,该版本包含了web控制页面 docker pull rabbitmq:management |
二、运行镜像
1
2
3
4
5
|
#方式一:默认guest 用户,密码也是 guest docker run -d -- hostname my-rabbit --name rabbit -p 15672:15672 -p 5672:5672 rabbitmq:management #方式二:设置用户名和密码 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页面
http://localhost:15672/
四、golang案例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
#producer生产者代码 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为主机ip //durable amqp exchange name exchangename = "" //durable amqp queue name queuename = "test-queues" //body of message bodymsg string = "hello angel" ) //如果存在错误,则输出 func failonerror(err error, msg string) { if err != nil { log.fatalf("%s: %s", msg, err) panic(fmt.sprintf("%s: %s", msg, err)) } } func main() { //调用发布消息函数 publish(uri, exchangename, queuename, bodymsg) log.printf("published %db ok", len(bodymsg)) } //发布者的方法 //@amqpuri, amqp的地址 //@exchange, exchange的名称 //@queue, queue的名称 //@body, 主体内容 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() //创建一个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) //创建一个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只能发送到exchange,它是不能直接发送到queue的 // 现在我们使用默认的exchange(名字是空字符)这个默认的exchange允许我们发送给指定的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") } |
五、拥有消息确认的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
#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" ) //如果存在错误,则输出 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) //调用发布消息函数 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 } //发布者的方法 //@amqpuri, amqp的地址 //@exchange, exchange的名称 //@queue, queue的名称 //@body, 主体内容 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() //创建一个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) //创建一个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只能发送到exchange,它是不能直接发送到queue的。 // 现在我们使用默认的exchange(名字是空字符)。这个默认的exchange允许我们发送给指定的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") } |
到此这篇关于docker快速安装rabbitmq的方法步骤的文章就介绍到这了,更多相关docker安装rabbitmq内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/angelyan/p/11218260.html