master v1.0.2
杨赟 2 years ago
parent 4ea67a7836
commit 339940b810

@ -3,22 +3,17 @@ package rabbitmq
import (
"fmt"
"github.com/streadway/amqp"
"log"
)
var Rabbitmq = &rabbitmq{}
type rabbitmq struct {
conn *amqp.Connection
Conn *amqp.Connection
}
// InitRabbitmq @Title 初始化rabbitmq
func InitRabbitmq(host string, port int, user, password string) {
var err error
Rabbitmq.conn, err = amqp.Dial(fmt.Sprintf("amqp://%s:%s@%s:%d/", user, password, host, port))
if err != nil {
log.Fatal("rabbitmq链接失败err:", err)
}
func InitRabbitmq(host string, port int, user, password string) (err error) {
Rabbitmq.Conn, err = amqp.Dial(fmt.Sprintf("amqp://%s:%s@%s:%d/", user, password, host, port))
return
}
@ -27,7 +22,7 @@ func InitRabbitmq(host string, port int, user, password string) {
// @Param msg string true "发送消息内容"
// @Param delay int64 false "延时发送秒数"
func (r *rabbitmq) Publish(exchangeName, msg string, delay ...int64) error {
ch, err := r.conn.Channel()
ch, err := r.Conn.Channel()
if err != nil {
return err
}
@ -53,7 +48,7 @@ func (r *rabbitmq) Publish(exchangeName, msg string, delay ...int64) error {
// @Param autoAck bool true "是否自动确认"
// @Param callback func(amqp.Delivery)) true "回调处理"
func (r *rabbitmq) Consume(queueName string, autoAck bool, callback func(amqp.Delivery)) error {
ch, err := r.conn.Channel()
ch, err := r.Conn.Channel()
if err != nil {
return err
}
@ -83,5 +78,5 @@ func (r *rabbitmq) Consume(queueName string, autoAck bool, callback func(amqp.De
// Close @Title 关闭资源
func (r *rabbitmq) Close() {
defer Rabbitmq.conn.Close()
defer Rabbitmq.Conn.Close()
}

Loading…
Cancel
Save