https://blog.csdn.net/qq_36389107/article/details/80404043
1.安装erlang
1).官网下载:官网下载
2).百度云盘下载:点击下载
下载完成后,是一个exe文件,双击傻瓜式安装即可
3).新建系统变量:变量名 ERLANG_HOME 变量值 D:\softInstall\erl8.3(Erlang安装目录)
4.添加到PATH:%ERLANG_HOME%\bin;
2.安装rabbitMQ
1).官网下载:官网下载
2).百度云盘:点击下载
下载完成后,也是傻瓜式安装即可
3.安装RabbitMQ-Plugins
进入rabbitMQ的安装目录下的sbin目录,
打开cmd,输入:rabbitmq-plugins enable rabbitmq_management
打开浏览器:输入:http://localhost:15672/
- 如果能打开登录界面,直接输入账号密码为guest,guest即可进入管理页面
- 打不开的话,进入
C:\Users\Administrator\AppData\Roaming\RabbitMQ\db
目录下,全选删除即可。删除不了的不管跳过即可。(是c盘用户下的目录位置。如果没看见appdata,左上角点击查看》》显示隐藏的文件即可) - 在点击RabbitMQ.exe安装即可。完成后就能打开web管理界面了
视频教程地址:点击传送
RabbitMQ for Go
接收和发送实例(下面是完整代码):
package main
import (
"fmt"
"log"
"bytes"
"time"
"github.com/streadway/amqp"
)
var conn *amqp.Connection
var channel *amqp.Channel
var count = 0
const (
queueName = "push.msg.q"
exchange = "t.msg.ex"
mqurl ="amqp://shi:123@192.168.232.130:5672/test"
)
func main() {
go func() {
for {
push()
time.Sleep(1 * time.Second)
}
}()
receive()
fmt.Println("end")
close()
}
func failOnErr(err error, msg string) {
if err != nil {
log.Fatalf("%s:%s", msg, err)
panic(fmt.Sprintf("%s:%s", msg, err))
}
}
func mqConnect() {
var err error
conn, err = amqp.Dial(mqurl)
failOnErr(err, "failed to connect tp rabbitmq")
channel, err = conn.Channel()
failOnErr(err, "failed to open a channel")
}
func close() {
channel.Close()
conn.Close()
}
//连接rabbitmq server
func push() {
if channel == nil {
mqConnect()
}
msgContent := "hello world!"
channel.Publish(exchange, queueName, false, false, amqp.Publishing{
ContentType: "text/plain",
Body: []byte(msgContent),
})
}
func receive() {
if channel == nil {
mqConnect()
}
msgs, err := channel.Consume(queueName, "", true, false, false, false, nil)
failOnErr(err, "")
forever := make(chan bool)
go func() {
//fmt.Println(*msgs)
for d := range msgs {
s := BytesToString(&(d.Body))
count++
fmt.Printf("receve msg is :%s -- %d\n", *s, count)
}
}()
fmt.Printf(" [*] Waiting for messages. To exit press CTRL+C\n")
<-forever
}
func BytesToString(b *[]byte) *string {
s := bytes.NewBuffer(*b)
r := s.String()
return &r
}
posted on 2018-06-09 15:13
思月行云 阅读(350)
评论(0) 编辑 收藏 引用 所属分类:
Golang 、
分布式\MQ