49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
package initalize
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"fusenapi/constants"
|
|
"github.com/streadway/amqp"
|
|
"log"
|
|
)
|
|
|
|
// handle
|
|
type QueueItem struct {
|
|
Ch *amqp.Channel
|
|
Queue amqp.Queue
|
|
}
|
|
|
|
func InitRabbitMq(url string, config *tls.Config) map[string]*QueueItem {
|
|
conn, err := amqp.DialTLS(url, config)
|
|
if err != nil {
|
|
log.Fatalf("Failed to connect to RabbitMQ: %v", err)
|
|
}
|
|
// 创建一个通道
|
|
ch, err := conn.Channel()
|
|
if err != nil {
|
|
log.Fatalf("Failed to open a channel: %v", err)
|
|
}
|
|
mapMq := make(map[string]*QueueItem)
|
|
//声明队列
|
|
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)
|
|
}
|
|
mapMq[string(queueName)] = &QueueItem{
|
|
Ch: ch,
|
|
Queue: q,
|
|
}
|
|
}
|
|
return mapMq
|
|
}
|