rabbitmq.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package initialize
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "mtp20access/global"
  6. rsp "mtp20access/model/mq/response"
  7. "mtp20access/packet"
  8. "mtp20access/res/pb"
  9. "mtp20access/utils"
  10. "github.com/golang/protobuf/proto"
  11. "github.com/streadway/amqp"
  12. "go.uber.org/zap"
  13. "google.golang.org/protobuf/encoding/protojson"
  14. )
  15. func RabbitMQ() *global.RabbitMQ {
  16. url := global.M2A_CONFIG.Rabbitmq.Url
  17. connection, err := amqp.Dial(url)
  18. if err != nil {
  19. global.M2A_LOG.Error("rabbitmq connect failed, err:", zap.Error(err))
  20. return nil
  21. }
  22. channel, err := connection.Channel()
  23. if err != nil {
  24. global.M2A_LOG.Error("rabbitmq open channel failed, err:", zap.Error(err))
  25. return nil
  26. }
  27. global.M2A_LOG.Info("rabbitmq connect successed.")
  28. return &global.RabbitMQ{
  29. Connection: connection,
  30. Channel: channel,
  31. }
  32. }
  33. // MQProc 消息处理对象
  34. type MQProc struct{}
  35. // process 消息处理接口
  36. func (t *MQProc) Process(topic, queuename string, msg *[]byte) {
  37. info := fmt.Sprintf("rabbitmq receive message from: topic[%s] queue[%s] contentLen[%d]",
  38. topic,
  39. queuename,
  40. len(string(*msg)))
  41. global.M2A_LOG.Info(info)
  42. if funcode, sessionId, bytes, serialNumber, err := t.getRspProtobuf(msg); err == nil && bytes != nil {
  43. // 尝试获取对应异步任务
  44. if client, exists := global.M2A_Clients[int(sessionId)]; exists {
  45. key := fmt.Sprintf("%v_%v_%v", sessionId, funcode, serialNumber)
  46. asyncTask := client.GetAsyncTask(key)
  47. if asyncTask != nil {
  48. rspData := string(*bytes)
  49. // 判断是否要加密
  50. if asyncTask.IsEncrypted {
  51. if b, err := packet.Encrypt(*bytes, packet.AESKey, true); err != nil {
  52. global.M2A_LOG.Error("总线回复数据加密失败", zap.Error(err))
  53. } else {
  54. rspData = base64.StdEncoding.EncodeToString(b)
  55. }
  56. }
  57. // 给客户端回调
  58. r := rsp.MQBodyRsp{
  59. FunCode: funcode,
  60. IsEncrypted: asyncTask.IsEncrypted,
  61. Data: rspData,
  62. }
  63. asyncTask.Rsp <- r
  64. // response.OkWithData(r, asyncTask.C)
  65. // 清除异步任务
  66. // asyncTask.Close()
  67. fmt.Println()
  68. }
  69. }
  70. }
  71. }
  72. // getRspProtobuf 将总线回复的数据反序列化为Protobuf
  73. func (t *MQProc) getRspProtobuf(msg *[]byte) (funcode uint32, sessionId uint32, bytes *[]byte, serialNumber uint32, err error) {
  74. // 分解总线包信息
  75. funcode = utils.BytesToUint32((*msg)[0:4])
  76. sessionId = utils.BytesToUint32((*msg)[4:8])
  77. b := (*msg)[8:]
  78. switch int(funcode) {
  79. case global.ZSSellOrderListingRsp: // 钻石卖挂牌接口响应
  80. var p pb.ZSSellOrderListingRsp
  81. if err = proto.Unmarshal(b, &p); err != nil {
  82. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  83. return
  84. }
  85. if bs, e := protojson.Marshal(&p); e != nil {
  86. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  87. return
  88. } else {
  89. bytes = &bs
  90. serialNumber = p.GetHeader().GetRequestID()
  91. }
  92. }
  93. return
  94. }
  95. // RabbitMQSubscribeTopic 订阅主题
  96. func RabbitMQSubscribeTopic() (err error) {
  97. // 订阅需要的总线响应主题
  98. if err = global.SubscribeTopic(global.TOPIC_RSP_WAREHOUSE_TRADE_GZ); err != nil {
  99. global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
  100. return
  101. }
  102. global.M2A_LOG.Info("rabbitmq subscribe topic successed.")
  103. return
  104. }
  105. // StartRabbitMQReceive 开始接收总线消息
  106. func StartRabbitMQReceive() {
  107. t := &MQProc{}
  108. go func() {
  109. for _, subinfo := range global.SubInfos {
  110. global.Receive(subinfo.Topic, subinfo.QueueName, t)
  111. }
  112. }()
  113. }
  114. // InitFuncodeTopic 初始化功能码主题MAP
  115. func InitFuncodeTopic() {
  116. if global.M2A_FuncodeTopic == nil {
  117. global.M2A_FuncodeTopic = make(map[int]string)
  118. }
  119. global.M2A_FuncodeTopic[global.ZSSellOrderListingReq] = global.TOPIC_REQ_WAREHOUSE_TRADE_GZ
  120. }