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

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

Loading…
Cancel
Save