-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublisher.go
173 lines (159 loc) · 4.79 KB
/
publisher.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
package kafclient
import (
"encoding/json"
"errors"
"log"
"strings"
"github.com/IBM/sarama"
)
func makeKafkaConfigPublisher(partitioner sarama.PartitionerConstructor) *sarama.Config {
config := sarama.NewConfig()
config.Producer.Retry.Max = 5
config.Producer.RequiredAcks = sarama.WaitForAll
config.Producer.Return.Successes = true
// config.Producer.Partitioner = sarama.NewManualPartitioner()
config.Producer.Partitioner = partitioner
// publisherConfig = config
return config
}
func newPublisher(topic string, brokerURLs ...string) (sarama.SyncProducer, error) {
config := makeKafkaConfigPublisher(sarama.NewRandomPartitioner)
prd, err := sarama.NewSyncProducer(brokerURLs, config)
if err != nil {
return nil, err
}
return prd, nil
}
func newAsyncPublisher(topic string, brokerURLs ...string) (sarama.AsyncProducer, error) {
config := makeKafkaConfigPublisher(sarama.NewRandomPartitioner)
prd, err := sarama.NewAsyncProducer(brokerURLs, config)
if err != nil {
return nil, err
}
return prd, nil
}
func newPublisherWithConfigPartitioner(topic *Topic, brokerURLs ...string) (sarama.SyncProducer, error) {
var config *sarama.Config
if topic.Partition == nil {
config = makeKafkaConfigPublisher(sarama.NewRandomPartitioner)
} else if ToInt32(topic.Partition) >= 0 {
config = makeKafkaConfigPublisher(sarama.NewManualPartitioner)
}
prd, err := sarama.NewSyncProducer(brokerURLs, config)
if err != nil {
return nil, err
}
return prd, nil
}
// InitPublisher init with addr is url of lookupd
func (ps *Client) InitPublisher(brokerURLs ...string) {
ps.brokerURLs = brokerURLs
// ps.producers = make(map[string]sarama.SyncProducer)
}
// Publish sync publish message
func (p *Client) Publish(topic string, messages ...interface{}) error {
if strings.Contains(topic, "__consumer_offsets") {
return errors.New("topic fail")
}
if _, ok := p.mProducer.Load(topic); !ok {
producer, err := newPublisher(topic, p.brokerURLs...)
if err != nil {
log.Print("[sarama]:", err, "]: topic", topic)
return err
}
p.mProducer.Store(topic, producer)
}
listMsg := make([]*sarama.ProducerMessage, 0)
for _, msg := range messages {
bin, err := json.Marshal(msg)
if err != nil {
log.Printf("[sarama] error: %v[sarama] msg: %v", err, msg)
}
msg := &sarama.ProducerMessage{
Topic: topic,
Value: sarama.StringEncoder(bin),
Partition: -1,
}
// asyncProducer.(sarama.AsyncProducer).Input() <- msg
listMsg = append(listMsg, msg)
}
syncProducer, ok := p.mProducer.Load(topic)
if !ok {
log.Print("not found any sync Producer")
}
err := syncProducer.(sarama.SyncProducer).SendMessages(listMsg)
return err
}
// AsyncPublish async publish message
func (p *Client) AsyncPublish(topic string, messages ...interface{}) error {
if strings.Contains(topic, "__consumer_offsets") {
return errors.New("topic fail")
}
if _, ok := p.mProducer.Load(topic); !ok {
producer, err := newAsyncPublisher(topic, p.brokerURLs...)
if err != nil {
log.Print("[sarama]:", err, "]: topic", topic)
return err
}
p.mProducer.Store(topic, producer)
}
asyncProducer, _ := p.mProducer.Load(topic)
for _, msg := range messages {
bin, err := json.Marshal(msg)
if err != nil {
log.Printf("[sarama] error: %v[sarama] msg: %v", err, msg)
}
msg := &sarama.ProducerMessage{
Topic: topic,
Value: sarama.StringEncoder(bin),
Partition: -1,
}
asyncProducer.(sarama.AsyncProducer).Input() <- msg
}
return nil
}
// PublishWithConfig sync publish message with select config
// Sender config help config producerMessage
func (p *Client) PublishWithConfig(topic *Topic, config *SenderConfig, messages ...interface{}) error {
if strings.Contains(topic.Name, "__consumer_offsets") {
return errors.New("topic fail")
}
if _, ok := p.mProducer.Load(topic); !ok {
producer, err := newPublisherWithConfigPartitioner(topic, p.brokerURLs...)
if err != nil {
log.Print("[sarama]:", err, "]: topic", topic)
return err
}
p.mProducer.Store(topic, producer)
}
listMsg := make([]*sarama.ProducerMessage, 0)
for _, msg := range messages {
bin, err := json.Marshal(msg)
if err != nil {
log.Printf("[sarama] error: %v[sarama] msg: %v", err, msg)
}
pmsg := &sarama.ProducerMessage{
Topic: topic.Name,
Value: sarama.StringEncoder(bin),
Partition: -1,
}
if topic.Partition != nil {
pmsg.Partition = ToInt32(topic.Partition)
}
if config != nil {
pmsg.Metadata = config.Metadata
if len(config.Headers) > 0 {
for k, v := range config.Headers {
pmsg.Headers = append(pmsg.Headers, sarama.RecordHeader{Key: []byte(k), Value: []byte(v)})
}
}
}
listMsg = append(listMsg, pmsg)
}
syncProducer, ok := p.mProducer.Load(topic)
if !ok {
log.Print("not found any sync Producer")
}
err := syncProducer.(sarama.SyncProducer).SendMessages(listMsg)
return err
}