broker.go
1.5 KB
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
package broker
import (
"fmt"
"github.com/linmadan/egglib-go/utils/json"
"github.com/tal-tech/go-queue/kq"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
"strings"
)
var defaultClientMap = make(map[string]*kq.Pusher)
type client struct {
pusher *kq.Pusher
}
func RegisterDefaultPusherClient(host, topic string) {
if _, ok := defaultClientMap[topic]; ok {
panic("duplicate register pusher client")
}
pusher := kq.NewPusher(strings.Split(host, ","), topic)
defaultClientMap[topic] = pusher
}
// 异步推送到队列
func Push(topic string, o interface{}) error {
if !constant.EnableBlockChain {
return nil
}
var pusher *kq.Pusher
var ok bool
if pusher, ok = defaultClientMap[topic]; !ok {
return fmt.Errorf("pusher client [%v] not found ", topic)
}
if err := pusher.Push(json.MarshalToString(o)); err != nil {
log.Logger.Debug(fmt.Sprintf("Topic:%v error:%v", topic, err.Error()), map[string]interface{}{"value": o})
return err
} else {
log.Logger.Debug(fmt.Sprintf("Topic:%v 发送成功", topic), map[string]interface{}{"value": o})
}
return nil
}
// 新建一个推送对象
func NewPushObject(source, primaryId, issueId string, obj interface{}) interface{} {
data := map[string]interface{}{
"source": source,
"primaryId": primaryId,
"issueId": issueId,
"data": json.MarshalToString(obj),
}
return map[string]interface{}{
"topic": "up_block_chain",
"data": data,
}
}