rabbitmq.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. return
  54. } else {
  55. rspData = base64.StdEncoding.EncodeToString(b)
  56. }
  57. }
  58. // 给客户端回调
  59. r := rsp.MQBodyRsp{
  60. FunCode: funcode,
  61. IsEncrypted: asyncTask.IsEncrypted,
  62. Data: rspData,
  63. }
  64. asyncTask.Rsp <- r
  65. }
  66. }
  67. }
  68. }
  69. // getRspProtobuf 将总线回复的数据反序列化为Protobuf
  70. func (t *MQProc) getRspProtobuf(msg *[]byte) (funcode uint32, sessionId uint32, bytes *[]byte, serialNumber uint32, err error) {
  71. // 分解总线包信息
  72. funcode = utils.BytesToUint32((*msg)[0:4])
  73. sessionId = utils.BytesToUint32((*msg)[4:8])
  74. b := (*msg)[8:]
  75. switch int(funcode) {
  76. case global.ZSSellOrderListingRsp: // 钻石卖挂牌接口响应
  77. var p pb.ZSSellOrderListingRsp
  78. if err = proto.Unmarshal(b, &p); err != nil {
  79. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  80. return
  81. }
  82. if bs, e := protojson.Marshal(&p); e != nil {
  83. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  84. return
  85. } else {
  86. bytes = &bs
  87. serialNumber = p.GetHeader().GetRequestID()
  88. }
  89. }
  90. return
  91. }
  92. // RabbitMQSubscribeTopic 订阅主题
  93. func RabbitMQSubscribeTopic() (err error) {
  94. // 订阅需要的总线响应主题
  95. if err = global.SubscribeTopic(global.TOPIC_RSP_WAREHOUSE_TRADE_GZ); err != nil {
  96. global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
  97. return
  98. }
  99. global.M2A_LOG.Info("rabbitmq subscribe topic successed.")
  100. return
  101. }
  102. // StartRabbitMQReceive 开始接收总线消息
  103. func StartRabbitMQReceive() {
  104. t := &MQProc{}
  105. go func() {
  106. for _, subinfo := range global.SubInfos {
  107. global.Receive(subinfo.Topic, subinfo.QueueName, t)
  108. }
  109. }()
  110. }
  111. // InitFuncodeTopic 初始化功能码主题MAP
  112. func InitFuncodeTopic() {
  113. if global.M2A_FuncodeTopic == nil {
  114. global.M2A_FuncodeTopic = make(map[string][]int)
  115. }
  116. //global.M2A_FuncodeTopic[global.ModifyPwdReq] =
  117. //global.M2A_FuncodeTopic[global.ZSSellOrderListingReq] = global.TOPIC_REQ_WAREHOUSE_TRADE_GZ
  118. }