goProject/trunk/center/common/rabbitmq/config.go
2025-02-20 17:46:57 +08:00

62 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package rabbitmq
import (
configYaml "common/configsYaml"
"goutil/logUtil"
"strconv"
"github.com/streadway/amqp"
)
var RabbitMQConn *amqp.Connection
var RabbitMQChannel *amqp.Channel
// 初始化rabbitMQ
func init() {
rabbitMQAddress := configYaml.GetRabbitMQAddress()
//是否有mq配置
if rabbitMQAddress == "" {
return
}
// 连接到 RabbitMQ 服务器
var err error
RabbitMQConn, err = amqp.Dial(rabbitMQAddress)
if err != nil {
//抛出一个异常
logUtil.FatalLog("Failed to connect to RabbitMQ: %serr:%s", rabbitMQAddress, err.Error())
return
}
// 打开一个通道
RabbitMQChannel, err = RabbitMQConn.Channel()
if err != nil {
//抛出一个异常
logUtil.FatalLog("Failed to open a channelerr:%s", err.Error())
return
}
//循环数据库数量
for _, index := range configYaml.GetDBNum() {
//队列名称
queueName := configYaml.GetRabbitMQName() + ":" + strconv.Itoa(index)
// 声明一个队列
_, err = RabbitMQChannel.QueueDeclare(
queueName, // 队列名称
true, // 是否持久化
false, // 是否在使用后删除
false, // 是否排他
false, // 是否阻塞
nil, // 其他参数
)
if err != nil {
logUtil.FatalLog("Failed to declare a queuequeueName:%s,err:%s", queueName, err.Error())
}
}
}