package consume import ( "applet/app/cfg" "applet/app/utils" "applet/app/utils/logx" "applet/consume/md" "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles" "encoding/json" "errors" "fmt" "github.com/streadway/amqp" "time" ) func WithdrawConsume(queue md.MqQueue) { fmt.Println(">>>>>>>>>>>>WithdrawConsume>>>>>>>>>>>>") ch, err := rabbit.Cfg.Pool.GetChannel() if err != nil { logx.Error(err) return } defer ch.Release() //1、将自己绑定到交换机上 ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey) //2、取出数据进行消费 ch.Qos(1) delivery := ch.Consume(queue.Name, false) one_circles.Init(cfg.RedisAddr) var res amqp.Delivery var ok bool for { res, ok = <-delivery if ok == true { err = handleWithdrawConsume(res.Body) fmt.Println("err ::: ", err) if err != nil { fmt.Println("WithdrawConsume_ERR:::::", err.Error()) //_ = res.Reject(true) _ = res.Reject(false) var msg interface{} json.Unmarshal(res.Body, &msg) if err.Error() == "Connection timed out" { //TODO::重新推回队列末尾,避免造成队列堵塞 ch.Publish(queue.ExchangeName, msg, queue.RoutKey) } else { //TODO::推入新的队列中备份 utils.FilePutContents("WithdrawConsume_ERR", utils.SerializeStr(err.Error())) ch.Publish("zhios.app.user.withdraw.apply.exception.exchange", msg, "queues_one") } } else { err = res.Ack(true) } } else { panic(errors.New("error getting message")) } } fmt.Println("get msg done") } func handleWithdrawConsume(msgData []byte) error { time.Sleep(time.Microsecond * 200) // 等待200毫秒 //1、解析mq中queue的数据结构体 var msg interface{} err := json.Unmarshal(msgData, &msg) if err != nil { return err } fmt.Println("message:::::::::::>>>>>>>>>") fmt.Println(msg) var url = "http://admin.99813608.zhiyingos.com/index/transfer" if cfg.Prd { url = "http://zhios-admin/index/transfer" } post, err := utils.CurlPost(url, msg, nil) if err != nil { return err } fmt.Println("transfer:::::::::::<<<<<<<<<") fmt.Println(string(post), "\n========================================\n\n") var postResult struct { Code int `json:"code"` Msg string `json:"msg"` Data struct { IsOk bool `json:"isOk"` Msg string `json:"msg"` } `json:"data"` } err = json.Unmarshal(post, &postResult) if err != nil { return err } if !postResult.Data.IsOk { return errors.New(postResult.Data.Msg) } return nil }