fusenapi/initalize/rabbitmq.go

52 lines
1.0 KiB
Go
Raw Normal View History

2023-07-28 07:10:06 +00:00
package initalize
import (
"crypto/tls"
"fusenapi/constants"
"github.com/streadway/amqp"
"log"
)
// handle
2023-07-28 07:19:48 +00:00
type QueueItem struct {
2023-07-28 07:10:06 +00:00
Ch *amqp.Channel
Queue amqp.Queue
}
2023-07-28 07:39:15 +00:00
var mapMq = make(map[string]*QueueItem)
func InitRabbitMq(url string, config *tls.Config) {
if url == "" {
return
}
2023-07-28 07:10:06 +00:00
conn, err := amqp.DialTLS(url, config)
if err != nil {
2023-07-28 07:19:48 +00:00
log.Fatalf("Failed to connect to RabbitMQ: %v", err)
2023-07-28 07:10:06 +00:00
}
// 创建一个通道
ch, err := conn.Channel()
if err != nil {
2023-07-28 07:19:48 +00:00
log.Fatalf("Failed to open a channel: %v", err)
2023-07-28 07:10:06 +00:00
}
//声明队列
for _, queueName := range constants.MQ_QUEUE_ARR {
q, err := ch.QueueDeclare(
string(queueName), // 队列名
true, // 是否持久化
false, // 是否自动删除
false, // 是否排他
false, // 是否等待服务器响应
nil, // 其他参数
)
if err != nil {
conn.Close()
ch.Close()
log.Fatalf("Failed to declare a queue: %v", err)
}
2023-07-28 07:19:48 +00:00
mapMq[string(queueName)] = &QueueItem{
2023-07-28 07:10:06 +00:00
Ch: ch,
Queue: q,
}
}
}