golang 的 rabbitmq 消费项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

97 lines
2.5 KiB

  1. package consume
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/utils"
  5. "applet/app/utils/logx"
  6. "applet/consume/md"
  7. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  8. "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "github.com/streadway/amqp"
  13. "time"
  14. )
  15. func WithdrawConsume(queue md.MqQueue) {
  16. fmt.Println(">>>>>>>>>>>>WithdrawConsume>>>>>>>>>>>>")
  17. ch, err := rabbit.Cfg.Pool.GetChannel()
  18. if err != nil {
  19. logx.Error(err)
  20. return
  21. }
  22. defer ch.Release()
  23. //1、将自己绑定到交换机上
  24. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  25. //2、取出数据进行消费
  26. ch.Qos(1)
  27. delivery := ch.Consume(queue.Name, false)
  28. one_circles.Init(cfg.RedisAddr)
  29. var res amqp.Delivery
  30. var ok bool
  31. for {
  32. res, ok = <-delivery
  33. if ok == true {
  34. err = handleWithdrawConsume(res.Body)
  35. fmt.Println("err ::: ", err)
  36. if err != nil {
  37. fmt.Println("WithdrawConsume_ERR:::::", err.Error())
  38. //_ = res.Reject(true)
  39. _ = res.Reject(false)
  40. var msg interface{}
  41. json.Unmarshal(res.Body, &msg)
  42. if err.Error() == "Connection timed out" {
  43. //TODO::重新推回队列末尾,避免造成队列堵塞
  44. ch.Publish(queue.ExchangeName, msg, queue.RoutKey)
  45. } else {
  46. //TODO::推入新的队列中备份
  47. utils.FilePutContents("WithdrawConsume_ERR", utils.SerializeStr(err.Error()))
  48. ch.Publish("zhios.app.user.withdraw.apply.exception.exchange", msg, "queues_one")
  49. }
  50. } else {
  51. err = res.Ack(true)
  52. }
  53. } else {
  54. panic(errors.New("error getting message"))
  55. }
  56. }
  57. fmt.Println("get msg done")
  58. }
  59. func handleWithdrawConsume(msgData []byte) error {
  60. time.Sleep(time.Microsecond * 200) // 等待200毫秒
  61. //1、解析mq中queue的数据结构体
  62. var msg interface{}
  63. err := json.Unmarshal(msgData, &msg)
  64. if err != nil {
  65. return err
  66. }
  67. fmt.Println("message:::::::::::>>>>>>>>>")
  68. fmt.Println(msg)
  69. post, err := utils.CurlPost("http://admin.99813608.zhiyingos.com/index/transfer", msg, nil)
  70. if err != nil {
  71. return err
  72. }
  73. fmt.Println("transfer:::::::::::<<<<<<<<<")
  74. fmt.Println(string(post), "\n========================================\n\n")
  75. var postResult struct {
  76. Code int `json:"code"`
  77. Msg string `json:"msg"`
  78. Data struct {
  79. IsOk bool `json:"isOk"`
  80. Msg string `json:"msg"`
  81. } `json:"data"`
  82. }
  83. err = json.Unmarshal(post, &postResult)
  84. if err != nil {
  85. return err
  86. }
  87. if !postResult.Data.IsOk {
  88. return errors.New(postResult.Data.Msg)
  89. }
  90. return nil
  91. }