-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.go
217 lines (189 loc) · 4.97 KB
/
interface.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
package queue
import (
"fmt"
"math/rand"
"relation-service/libary/constant"
"relation-service/model/dao/cache/redis"
"strconv"
"sync"
"time"
)
type Queue interface {
Start() error
Stop()
}
type Producer interface {
SendMsg(topic string, body string) (msg Msg, err error)
SendByteMsg(topic string, body []byte) (msg Msg, err error)
SendDelayMsg(topic string, body string, delaySecond int64) (mqMsg Msg, err error)
}
type Consumer interface {
ListenReceiveMsgDo(topic string, receiveDo func(Msg Msg)) (err error)
}
const (
_ = iota
SendMsg
ReceiveMsg
)
type Config struct {
Switch bool `json:"switch"`
Driver string `json:"driver"`
Retry int `json:"retry"`
GroupName string `json:"groupName"`
Redis RedisConf
Rocket RocketConf
Kafka KafkaConf
Pulsar PulsarConf
}
type RedisConf struct {
Timeout int64 `json:"timeout"`
}
type RocketConf struct {
Address []string `json:"address"`
LogLevel string `json:"logLevel"`
}
type PulsarConf struct {
Address []string `json:"address"`
LogLevel string `json:"logLevel"`
Topic string `json:"topic"`
URL string `json:"url"`
Type int `json:"type"`
SubscriptionName string `json:"subscriptionName"`
}
type KafkaConf struct {
Address []string `json:"address"`
Version string `json:"version"`
RandClient bool `json:"randClient"`
MultiConsumer bool `json:"multiConsumer"`
}
type Msg struct {
RunType int `json:"run_type"`
Topic string `json:"topic"`
MsgId string `json:"msg_id"`
Offset int64 `json:"offset"`
Partition int32 `json:"partition"`
Timestamp time.Time `json:"timestamp"`
Body []byte `json:"body"`
}
var (
mutex sync.Mutex
config Config
)
// InstanceConsumer 实例化消费者
func InstanceConsumer() (mqClient Consumer, err error) {
return NewConsumer(config.GroupName)
}
// InstanceProducer 实例化生产者
func InstanceProducer() (client Producer, err error) {
return NewProducer(config.GroupName)
}
// NewProducer 初始化生产者实例
func NewProducer(groupName string) (client Producer, err error) {
if groupName == "" {
err = fmt.Errorf("mq groupName is empty")
return
}
switch config.Driver {
case constant.RocketMqName:
if len(config.Rocket.Address) == 0 {
err = fmt.Errorf("comment rocketmq address is not support")
return
}
client, err = RegisterRocketProducer(config.Rocket.Address, groupName, config.Retry)
case constant.KafkaMqName:
if len(config.Kafka.Address) == 0 {
err = fmt.Errorf("comment kafka address is not support")
return
}
client, err = RegisterKafkaProducer(KafkaConfig{
Brokers: config.Kafka.Address,
GroupID: groupName,
Version: config.Kafka.Version,
})
case constant.RedisMqName:
if _, err = redis.Ping(); err == nil {
client = RegisterRedisMq(RedisOption{
Timeout: config.Redis.Timeout,
}, groupName)
}
case constant.PulsarMqName:
if len(config.Pulsar.Address) == 0 {
err = fmt.Errorf("comment pulsar address is not support")
return
}
client, err = RegisterPulsarProducer(config.Pulsar)
default:
err = fmt.Errorf("comment driver is not support")
}
if err != nil {
return
}
mutex.Lock()
defer mutex.Unlock()
return
}
// NewConsumer 初始化消费者实例
func NewConsumer(groupName string) (client Consumer, err error) {
if groupName == "" {
err = fmt.Errorf("mq groupName is empty")
return
}
switch config.Driver {
case constant.RocketMqName:
if len(config.Rocket.Address) == 0 {
err = fmt.Errorf("comment.rocketmq.address is empty")
return
}
client, err = RegisterRocketConsumer(config.Rocket.Address, groupName)
case constant.KafkaMqName:
if len(config.Kafka.Address) == 0 {
err = fmt.Errorf("comment kafka address is not support")
return
}
randTag := strconv.FormatInt(time.Now().Unix(), 10)
// 是否支持创建多个消费者
if !config.Kafka.MultiConsumer {
randTag = "001"
}
clientId := "HOTGO-Consumer-" + groupName
if config.Kafka.RandClient {
clientId += "-" + randTag
}
client, err = RegisterKafkaConsumer(KafkaConfig{
Brokers: config.Kafka.Address,
GroupID: groupName,
Version: config.Kafka.Version,
ClientId: clientId,
})
case constant.RedisMqName:
if _, err = redis.Ping(); err == nil {
client = RegisterRedisMqConsumer(RedisOption{
Timeout: config.Redis.Timeout,
}, groupName)
}
case constant.PulsarMqName:
if len(config.Pulsar.Address) == 0 {
err = fmt.Errorf("comment pulsar address is not support")
return
}
client, err = RegisterPulsarConsumer(config.Pulsar)
default:
err = fmt.Errorf("comment driver is not support")
}
if err != nil {
return
}
mutex.Lock()
defer mutex.Unlock()
return
}
// BodyString 返回消息体
func (m *Msg) BodyString() string {
return string(m.Body)
}
func getRandMsgId() string {
rand.NewSource(time.Now().UnixNano())
radium := rand.Intn(999) + 1
timeCode := time.Now().UnixNano()
return fmt.Sprintf("%d%.4d", timeCode, radium)
}