-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.go
227 lines (211 loc) · 5.52 KB
/
core.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package CoreNats
import (
"encoding/json"
"errors"
"fmt"
CoreLog "github.com/fotomxq/weeekj_core/v5/core/log"
"github.com/nats-io/nats.go"
"time"
)
var (
//是否启动nats
openNC = false
//NatsURL 链接的地址
serverURL = ""
//连接句柄
nc *nats.Conn
//subPrefix 全局订阅地址前缀
subPrefix = ""
)
func Init(url string) (err error) {
//赋予值
serverURL = url
//链接服务
nc, err = nats.Connect(serverURL, nats.ReconnectWait(10*time.Second))
if err != nil {
err = errors.New("connect nats failed, " + err.Error())
return
}
//启动NC
openNC = true
//反馈
return
}
// SetSubPrefix 设置全局订阅前缀
func SetSubPrefix(prefix string) {
subPrefix = prefix
}
// 是否启动了NC
func checkNC() bool {
return openNC && nc.IsConnected()
}
// Sub 订阅消息
func Sub(serviceCode string, topic string, cb func(msg *nats.Msg)) (err error) {
if !checkNC() {
return
}
//var sub nats.Subscription
if subPrefix != "" {
topic = fmt.Sprint(subPrefix, topic)
}
_, err = nc.Subscribe(topic, cb)
if err != nil {
return
}
CoreLog.Info("nats sub, ", topic)
//推送统计
if serviceCode != "base_service_request" {
pushRequest(serviceCode, "sub")
}
return
}
// SubData 订阅数据包
func SubData(serviceCode string, topic string, cb func(msg *nats.Msg, action string, id int64, mark string, data interface{})) (err error) {
if !checkNC() {
return
}
if subPrefix != "" {
topic = fmt.Sprint(subPrefix, topic)
}
if err = Sub(serviceCode, topic, func(msg *nats.Msg) {
var newData dataType
err = json.Unmarshal(msg.Data, &newData)
if err != nil {
//抛弃异常数据
CoreLog.Error("nats sub load error json data, ", topic, ", err: ", err)
return
}
//CoreLog.Info("nats load sub topic: ", topic, ", action: ", newData.Action, ", id: ", newData.ID, ", mark: ", newData.Mark)
cb(msg, newData.Action, newData.ID, newData.Mark, newData.Data)
}); err != nil {
return
}
//CoreLog.Info("nats sub data, ", topic)
return
}
// SubDataByte 订阅数据包
func SubDataByte(serviceCode string, topic string, cb func(msg *nats.Msg, action string, id int64, mark string, data []byte)) (err error) {
if !checkNC() {
return
}
if subPrefix != "" {
topic = fmt.Sprint(subPrefix, topic)
}
if err = Sub(serviceCode, topic, func(msg *nats.Msg) {
var newData dataType
err = json.Unmarshal(msg.Data, &newData)
if err != nil {
//抛弃异常数据
CoreLog.Error("nats sub load error json data, ", topic, ", err: ", err)
return
}
var jsonData []byte
jsonData, err = json.Marshal(newData.Data)
if err != nil {
err = errors.New(fmt.Sprint("nats sub load error json data, err data json reflect: ", err))
return
}
//CoreLog.Info("nats load sub topic: ", topic, ", action: ", newData.Action, ", id: ", newData.ID, ", mark: ", newData.Mark)
cb(msg, newData.Action, newData.ID, newData.Mark, jsonData)
}); err != nil {
return
}
//CoreLog.Info("nats sub byte, ", topic)
return
}
// SubDataByteNoErr 订阅数据包
func SubDataByteNoErr(serviceCode string, topic string, cb func(msg *nats.Msg, action string, id int64, mark string, data []byte)) {
if err := SubDataByte(serviceCode, topic, cb); err != nil {
CoreLog.Error("nats sub failed, topic: ", topic)
}
}
// ReflectDataByte 反射结构体
func ReflectDataByte(mapData []byte, rawData interface{}) (err error) {
err = json.Unmarshal(mapData, rawData)
if err != nil {
err = errors.New(fmt.Sprint("err: ", err, ", json data: ", string(mapData)))
return
}
return
}
// ReflectData 反射结构体
func ReflectData(mapData interface{}, rawData interface{}) (err error) {
var jsonData []byte
jsonData, err = json.Marshal(mapData)
if err != nil {
err = errors.New(fmt.Sprint("nats sub load error json data, err data json reflect: ", err))
return
}
err = json.Unmarshal(jsonData, rawData)
if err != nil {
return
}
return
}
// Push 发布消息
func Push(serviceCode string, topic string, data []byte) (err error) {
if !checkNC() {
return
}
if subPrefix != "" {
topic = fmt.Sprint(subPrefix, topic)
}
err = nc.Publish(topic, data)
if err != nil {
return
}
//推送统计
if serviceCode != "base_service_request" {
pushRequest(serviceCode, "push")
}
//反馈
return
}
// PushJson 发送json数据包
func PushJson(serviceCode string, topic string, data interface{}) (err error) {
if !checkNC() {
return
}
var dataByte []byte
dataByte, err = json.Marshal(data)
if err != nil {
err = errors.New("json error, " + err.Error())
return
}
return Push(serviceCode, topic, dataByte)
}
func PushJsonNoErr(serviceCode string, topic string, data interface{}) {
if !checkNC() {
return
}
if err := PushJson(serviceCode, topic, data); err != nil {
CoreLog.Error("nats push json topic: ", topic, ", data: ", data)
}
}
// PushData 发送数据包
func PushData(serviceCode string, topic string, action string, id int64, mark string, data interface{}) (err error) {
if !checkNC() {
return
}
var dataByte []byte
dataByte, err = json.Marshal(dataType{
Action: action,
ID: id,
Mark: mark,
Data: data,
})
if err != nil {
err = errors.New("json error, " + err.Error())
return
}
return Push(serviceCode, topic, dataByte)
}
// PushDataNoErr 发送数据包无错误
func PushDataNoErr(serviceCode string, topic string, action string, id int64, mark string, data interface{}) {
if !checkNC() {
return
}
if err := PushData(serviceCode, topic, action, id, mark, data); err != nil {
CoreLog.Error("nats push data topic: ", topic, ", action: ", action, ", id: ", id, ", mark: ", mark, ", data: ", data)
}
}