安装
mac安装有了brew很方便,windows的可参考 win10 安装rabbitMQ详细步骤
bash
brew install rabbitmq
启动
bash
brew services start rabbitmq
关闭
bash
brew services stop rabbitmq
出了问题之后可以重启一下
bash
brew services restart rabbitmq
some issue
某些库下载超时
比如我安装过程中,发现libpng总是下不来
bash
brew --cache -s libpng
输出如下
bash
/Users/andydennis/Library/Caches/Homebrew/downloads/52af0b46c4798c4335195c9e01af657e58b8cb7f3d0fbcad1a753c3aff1ecc93--libpng-1.6.43.tar.xz
手动装一下
bash
mv libpng-1.6.43.tar.xz "$(brew --cache -s libpng)"
然后
bash
brew install libpng
Failed to connect to raw.githubusercontent.com port 443: Connection refused
报错信息
bash
Fetching openssl@3
==> Downloading https://raw.githubusercontent.com/Homebrew/homebrew-core/32addad
curl: (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
文件
bash
sudo vim /etc/hosts
添加下面语句
bash
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慢多了,最后终于装成功了
bash
==>rabbitmq
Management UI: http://localhost:15672
Homebrew-specific docs: https://rabbitmq.com/install-homebrew.html
To 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
bash
andydennis@andydennisdeMBP ~ % rabbitmq-plugins enable rabbitmq_management
Enabling plugins on node rabbit@localhost:
rabbitmq_management
The following plugins have been configured:
rabbitmq_amqp1_0
rabbitmq_management
rabbitmq_management_agent
rabbitmq_mqtt
rabbitmq_stomp
rabbitmq_stream
rabbitmq_web_dispatch
Applying plugin configuration to rabbit@localhost...
Plugin configuration unchanged.
开启 brew services start rabbitmq
, 访问 http://localhost:15672
初始用户名和密码都是guest
判断有无开启成功可查看端口5672 (默认端口,若修改了则按修改的端口) 有无无监听
bash
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
go
package main
import "go-rabbitmq/rabbitmq"
func main() {
rabbitMQ := rabbitmq.NewSimpleRabbitMQ("test-one")
rabbitMQ.Consumer()
}
producer.go
go
package main
import (
"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
go
package rabbitmq
import (
"fmt"
"log"
"github.com/streadway/amqp"
)
const MQURL = "amqp://guest:guest@localhost:5672"
type RabbitMQ struct {
conn *amqp.Connection
channel *amqp.Channel
queueName string
exchange string
key string
mqurl 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 = dial
rabbitMQ.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