main.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package main
  2. import (
  3. "encoding/json"
  4. "github.com/zeromicro/go-zero/core/discov"
  5. "github.com/zeromicro/go-zero/zrpc"
  6. ms "jy_publishing/megaloscope"
  7. nsq "jy_publishing/nsq"
  8. pb "jy_publishing/proto"
  9. "net"
  10. "strings"
  11. "utils"
  12. "utils/elastic"
  13. "utils/log"
  14. "utils/mongodb"
  15. )
  16. var (
  17. Sysconfig map[string]interface{}
  18. MgoBid, MgoExt *mongodb.MongodbSim
  19. BidColl string
  20. ExtColl, ExtColl1 string
  21. Es *elastic.Elastic
  22. Index, IndexAll string
  23. Itype string
  24. JyRpcClient zrpc.Client
  25. ClientAddr string
  26. MCJy, MCAtts *nsq.Consumer
  27. MProducer *nsq.Producer
  28. Ms *ms.Megaloscope //敏感词
  29. UdpClient util.UdpClient //udp对象
  30. )
  31. func init() {
  32. log.InitLogger("./log/All.log", "debug")
  33. util.ReadConfig(&Sysconfig)
  34. bidding := Sysconfig["bidding"].(map[string]interface{})
  35. BidColl = bidding["dbColl"].(string)
  36. MgoBid = &mongodb.MongodbSim{
  37. MongodbAddr: bidding["addr"].(string),
  38. Size: util.IntAllDef(Sysconfig["mgoPoolSize"], 5),
  39. DbName: bidding["dbName"].(string),
  40. UserName: bidding["uname"].(string),
  41. Password: bidding["upwd"].(string),
  42. }
  43. MgoBid.InitPool()
  44. extract := Sysconfig["extract"].(map[string]interface{})
  45. ExtColl = extract["dbColl"].(string)
  46. ExtColl1 = extract["dbColl1"].(string)
  47. MgoExt = &mongodb.MongodbSim{
  48. MongodbAddr: extract["addr"].(string),
  49. Size: util.IntAllDef(Sysconfig["mgoPoolSize"], 5),
  50. DbName: extract["dbName"].(string),
  51. }
  52. MgoExt.InitPool()
  53. es := Sysconfig["es"].(map[string]interface{})
  54. Es = &elastic.Elastic{
  55. S_esurl: util.ObjToString(es["addr"]),
  56. I_size: util.IntAllDef(es["pool"], 10),
  57. }
  58. Index = util.ObjToString(es["index"])
  59. IndexAll = util.ObjToString(es["bidding_all"])
  60. Itype = util.ObjToString(es["itype"])
  61. Es.InitElasticSize()
  62. //加载敏感词文件
  63. Ms = ms.NewMegaloscope("./rules.txt")
  64. jyRpc := Sysconfig["jy_rpc"].(map[string]interface{})
  65. JyRpcClient = zrpc.MustNewClient(zrpc.RpcClientConf{
  66. Etcd: discov.EtcdConf{
  67. Hosts: strings.Split(util.ObjToString(jyRpc["addr"]), ","),
  68. Key: util.ObjToString(jyRpc["key"]),
  69. },
  70. })
  71. initUdp()
  72. initNsq()
  73. }
  74. func initNsq() {
  75. cof := Sysconfig["nsq_jy"].(map[string]interface{})
  76. MCJy, _ = nsq.NewConsumer(&nsq.Cconfig{
  77. IsJsonEncode: true, //与生产者配置对应,设为true会取第1个字节进行类型判断
  78. Addr: util.ObjToString(cof["addr"]),
  79. ConnectType: 0, //默认连接nsqd
  80. Topic: util.ObjToString(cof["topic"]),
  81. Channel: util.ObjToString(cof["channel"]),
  82. Concurrent: util.IntAllDef(cof["concurrent"], 1), //并发数
  83. })
  84. cofAtts := Sysconfig["nsq_attachment"].(map[string]interface{})
  85. MProducer, _ = nsq.NewProducer(util.ObjToString(cofAtts["addr"]), util.ObjToString(cofAtts["topic"]), true)
  86. MCAtts, _ = nsq.NewConsumer(&nsq.Cconfig{
  87. IsJsonEncode: true, //与生产者配置对应,设为true会取第1个字节进行类型判断
  88. Addr: util.ObjToString(cofAtts["addr"]),
  89. ConnectType: 0, //默认连接nsqd
  90. Topic: util.ObjToString(cofAtts["topic-result"]),
  91. Channel: util.ObjToString(cofAtts["channel"]),
  92. Concurrent: util.IntAllDef(cofAtts["concurrent"], 1), //并发数
  93. })
  94. }
  95. func initUdp() {
  96. updport := Sysconfig["udpPort"].(string)
  97. UdpClient = util.UdpClient{Local: updport, BufSize: 1024}
  98. log.Info("Udp 监听 port: " + updport)
  99. UdpClient.Listen(processUdpMsg)
  100. }
  101. func main() {
  102. go jyNsqMethod()
  103. go attsNsqMethod()
  104. attsMap := map[string]interface{}{"1": map[string]interface{}{
  105. "fid": "1e0828202d4269d006f95e10e8f7afea4794a3d066d687f160f7dcafba02f74c.docx",
  106. "filename": "附件202110121614016176.docx",
  107. "ftype": "docx",
  108. }}
  109. other := map[string]interface{}{
  110. "id": "112",
  111. "action": "1",
  112. "msgType": "1",
  113. "title": []string{},
  114. "detail": []string{},
  115. }
  116. otherJson, _ := json.Marshal(other)
  117. var attsArr []*pb.Request
  118. for _, m := range attsMap {
  119. m1 := m.(map[string]interface{})
  120. attsArr = append(attsArr, &pb.Request{
  121. FileUrl: util.ObjToString(m1["fid"]),
  122. FileName: util.ObjToString(m1["filename"]),
  123. FileType: util.ObjToString(m1["ftype"]),
  124. ReturnType: 1,
  125. ExtractType: 0,
  126. })
  127. }
  128. msginfo := &pb.FileRequest{
  129. Message: attsArr,
  130. Other: string(otherJson),
  131. Topic: "data-attachment",
  132. }
  133. //msginfo := map[string]interface{}{
  134. // "meassage": attsArr,
  135. // "other": string(otherJson),
  136. // "topic": "data-attachment",
  137. //}
  138. _ = MProducer.Publish(msginfo)
  139. c := make(chan bool, 1)
  140. <-c
  141. }
  142. // @Description 剑鱼消息队列 按照类型处理消息
  143. // @Author J 2022/4/14 11:42 AM
  144. func jyNsqMethod() {
  145. for {
  146. select {
  147. case obj := <-MCJy.Ch: //从通道读取即可
  148. taskInfo(obj)
  149. }
  150. }
  151. }
  152. // @Description 附件处理完成的数据
  153. // @Author J 2022/4/14 1:18 PM
  154. func attsNsqMethod() {
  155. for {
  156. select {
  157. case obj := <-MCAtts.Ch:
  158. util.Debug(obj)
  159. //taskAtts(obj)
  160. }
  161. }
  162. }
  163. func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
  164. switch act {
  165. case util.OP_TYPE_DATA: //测试接收
  166. var rep map[string]interface{}
  167. err := json.Unmarshal(data, &rep)
  168. log.Info("udp receive...", log.Field("data", rep))
  169. if err != nil {
  170. //go Udpclient.WriteUdp([]byte{}, mu.OP_NOOP, ra) //回应上一个节点
  171. } else {
  172. //by, _ := json.Marshal(map[string]interface{}{
  173. // "taskid": rep["taskid"],
  174. // "stype": rep["stype"],
  175. //})
  176. //go Udpclient.WriteUdp(by, mu.OP_NOOP, ra) //回应上一个节点
  177. }
  178. case util.OP_NOOP: //下个节点回应
  179. log.Info("接收回应:", log.Field("data", string(data)))
  180. }
  181. }