安装
mac安装有了brew很方便,windows的可参考 win10 安装rabbitMQ详细步骤
brew install rabbitmq
启动
brew services start rabbitmq
关闭
brew services stop rabbitmq
出了问题之后可以重启一下
brew services restart rabbitmq
some issue
某些库下载超时
比如我安装过程中,发现libpng总是下不来
brew --cache -s libpng
输出如下
/Users/andydennis/Library/Caches/Homebrew/downloads/52af0b46c4798c4335195c9e01af657e58b8cb7f3d0fbcad1a753c3aff1ecc93--libpng-1.6.43.tar.xz
手动装一下
mv libpng-1.6.43.tar.xz "$(brew --cache -s libpng)"
然后
brew install libpng
Failed to connect to raw.githubusercontent.com port 443: Connection refused
报错信息
Fetching openssl@3
==> Downloading https://raw.githubusercontent.com/Homebrew/homebrew-core/32addadcurl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
Error: rabbitmq: Failed to download resource "openssl@3.rb"
可能是ip解析问题,查询一下 https://www.ipaddress.com/ ,
发现地址是 185.199.108.133
, 185.199.109.133
, 185.199.110.133
和185.199.111.133
, 于是打开/etc/hosts
文件
sudo vim /etc/hosts
添加下面语句
185.199.108.133 raw.githubusercontent.com
185.199.109.133 raw.githubusercontent.com
185.199.110.133 raw.githubusercontent.com
185.199.111.133 raw.githubusercontent.com
不得不说,mac比win慢多了,最后终于装成功了
==>rabbitmq
Management UI: http://localhost:15672
Homebrew-specific docs: https://rabbitmq.com/install-homebrew.htmlTo start rabbitmq now and restart at login:brew services start rabbitmq
Or, if you don't want/need a background service you can just run:CONF_ENV_FILE="/opt/homebrew/etc/rabbitmq/rabbitmq-env.conf" /opt/homebrew/opt/rabbitmq/sbin/rabbitmq-server
配置
激活一下组件 rabbitmq-plugins enable rabbitmq_management
andydennis@andydennisdeMBP ~ % rabbitmq-plugins enable rabbitmq_management
Enabling plugins on node rabbit@localhost:
rabbitmq_management
The following plugins have been configured:rabbitmq_amqp1_0rabbitmq_managementrabbitmq_management_agentrabbitmq_mqttrabbitmq_stomprabbitmq_streamrabbitmq_web_dispatch
Applying plugin configuration to rabbit@localhost...
Plugin configuration unchanged.
开启 brew services start rabbitmq
, 访问 http://localhost:15672
初始用户名和密码都是guest
判断有无开启成功可查看端口5672 (默认端口,若修改了则按修改的端口) 有无无监听
andydennis@andydennisdeMBP ~ % lsof -i :5672
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
beam.smp 68981 andydennis 62u IPv4 0x226aed36751b928f 0t0 TCP localhost:amqp (LISTEN)
code
依赖
在一个新文件夹,先 go mod init go-rabbitmq
再 go mod tidy
下更新一下依赖。
编写代码
consumer.go
package mainimport "go-rabbitmq/rabbitmq"func main() {rabbitMQ := rabbitmq.NewSimpleRabbitMQ("test-one")rabbitMQ.Consumer()
}
producer.go
package mainimport ("fmt""strconv""time""go-rabbitmq/rabbitmq"
)func main() {rabbitMQ := rabbitmq.NewSimpleRabbitMQ("test-one")for i := 0; i < 100000; i++ {time.Sleep(1000 * time.Millisecond)rabbitMQ.Publish("新消息 " + strconv.Itoa(i))fmt.Println("发送成功")}
}
rabbitmq.go
package rabbitmqimport ("fmt""log""github.com/streadway/amqp"
)const MQURL = "amqp://guest:guest@localhost:5672"type RabbitMQ struct {conn *amqp.Connectionchannel *amqp.ChannelqueueName stringexchange stringkey stringmqurl string
}func NewRabbitMQ(queueName string, exchange string, key string) *RabbitMQ {rabbitMQ := &RabbitMQ{queueName: queueName,exchange: exchange,key: key,mqurl: MQURL,}dial, err := amqp.Dial(rabbitMQ.mqurl)rabbitMQ.failOnErr(err, "创建连接失败")rabbitMQ.conn = dialrabbitMQ.channel, err = rabbitMQ.conn.Channel()rabbitMQ.failOnErr(err, "获取通道失败")return rabbitMQ
}func (r *RabbitMQ) failOnErr(err error, message string) {if err != nil {log.Fatalf("%s:%s\n", err, message)panic(fmt.Sprintf("%s:%s\n", err, message))}
}func (r *RabbitMQ) destory() {r.channel.Close()r.conn.Close()
}func NewSimpleRabbitMQ(queueName string) *RabbitMQ {return NewRabbitMQ(queueName, "", "")
}func (r *RabbitMQ) Publish(message string) {_, err := r.channel.QueueDeclare(r.queueName,//是否持久化false,//是否为自动删除false,//是否具有排他性false,//是否阻塞false,//额外属性nil,)if err != nil {panic(err)}r.channel.Publish(r.exchange,r.queueName,false,false,amqp.Publishing{ContentType: "text/plain",Body: []byte(message),})}func (r *RabbitMQ) Consumer() {_, err := r.channel.QueueDeclare(r.queueName, false, false, false, false, nil)if err != nil {panic(err)}//2、接收消息msgs, err := r.channel.Consume(r.queueName,//用来区分多个消费者"",//是否自动应答true,//是否具有排他性false,//如果设置为true,表示不能将同一个connection中发送的消息传递给这个connection中的消费者false,//消息队列是否阻塞false,nil,)if err != nil {panic(err)}forever := make(chan bool)go func() {for d := range msgs {log.Printf("Received a message : %s", d.Body)}}()log.Printf("[*] Waiting for messagees,To exit press CTRL+C")<-forever}
运行
(1) cd consumer
进入消费者目录, 把消费者运行起来 go run consumer.go
(2) cd producer
进入生产者目录,让生产者制造消息 go run producer.go