企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
初始化 ~~~ package RabbitMQ import ( "fmt" "github.com/streadway/amqp" "log" ) // url 格式 amqp://账户:秘密@rabbitmq服务器地址:端口号/host const MQURL = "amqp://guest:guest@127.0.0.1:5672/" type RabbitMQ struct { conn *amqp.Connection channel *amqp.Channel // 队列名称 QueueName string // 交换机 Exchange string // key Key string // 连接信息 Mqurl string } // 创建RabbitMQ结构体实例 func NewRabbitMQ(queueName,exchange,key string) *RabbitMQ { mq := &RabbitMQ{ QueueName: queueName, Exchange: exchange, Key: key, Mqurl: MQURL, } connection, e := amqp.Dial(mq.Mqurl) mq.failOnErr(e,"创建连接失败") channel, e := connection.Channel() mq.failOnErr(e,"获取channel失败") mq.conn = connection mq.channel = channel return mq } // 断开channel和connection func (r *RabbitMQ) Destroy() { r.channel.Close() r.conn.Close() } // 错误处理函数 func (r *RabbitMQ) failOnErr (err error,message string) { if err != nil { log.Fatal("%s:%s",message,err) panic(fmt.Sprintf("%s:%s",message,err)) } } ~~~