/
consumer.go
173 lines (161 loc) · 4.97 KB
/
consumer.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
/**
* @author: yangchangjia
* @email 1320259466@qq.com
* @date: 2023/9/6 6:38 PM
* @desc: about the role of class.
*/
package kafkas
import (
"github.com/IBM/sarama"
"sync"
)
type ConsumerClient struct {
sarama.Consumer
Offset int64
}
func InitConsumer(addrs []string, offsetOldest, isSync, randomPart bool, retryMax int) (*ConsumerClient, error) {
/**
* @author: yangchangjia
* @email 1320259466@qq.com
* @date: 2023/9/7 10:08 AM
* @desc: about the role of function.
* @param addrs, the kafka cluster address, such as: []string{"localhost:9192","localhost:9292","localhost:9392"}
* @param username, the kafka username
* @param password, the kafka password
* @return null
*/
config := sarama.NewConfig()
if isSync {
config.Producer.RequiredAcks = sarama.WaitForAll
}
if randomPart {
config.Producer.Partitioner = sarama.NewRandomPartitioner
} else {
config.Producer.Partitioner = sarama.NewHashPartitioner
}
if retryMax > 0 {
config.Producer.Retry.Max = retryMax
}
config.Consumer.Return.Errors = true
config.Producer.Return.Successes = true
if offsetOldest {
config.Consumer.Offsets.Initial = sarama.OffsetOldest
} else {
config.Consumer.Offsets.Initial = sarama.OffsetNewest
}
consumer, err := sarama.NewConsumer(addrs, config)
return &ConsumerClient{consumer, config.Consumer.Offsets.Initial}, err
}
func InitConsumerPlain(addrs []string, username, password string, offsetOldest, isSync, randomPart bool, retryMax int) (*ConsumerClient, error) {
/**
* @author: yangchangjia
* @email 1320259466@qq.com
* @date: 2023/9/7 10:08 AM
* @desc: about the role of function.
* @param addrs, the kafka cluster address, such as: []string{"localhost:9192","localhost:9292","localhost:9392"}
* @param username, the kafka username
* @param password, the kafka password
* @return null
*/
config := sarama.NewConfig()
if len(username) > 0 {
config.Net.SASL.Enable = true
config.Net.SASL.User = username
config.Net.SASL.Password = password
config.Net.SASL.Mechanism = sarama.SASLTypePlaintext
}
if isSync {
config.Producer.RequiredAcks = sarama.WaitForAll
}
if randomPart {
config.Producer.Partitioner = sarama.NewRandomPartitioner
} else {
config.Producer.Partitioner = sarama.NewHashPartitioner
}
if retryMax > 0 {
config.Producer.Retry.Max = retryMax
}
config.Consumer.Return.Errors = true
config.Producer.Return.Successes = true
if offsetOldest {
config.Consumer.Offsets.Initial = sarama.OffsetOldest
} else {
config.Consumer.Offsets.Initial = sarama.OffsetNewest
}
consumer, err := sarama.NewConsumer(addrs, config)
return &ConsumerClient{consumer, config.Consumer.Offsets.Initial}, err
}
func InitConsumerScram(addrs []string, username, password string, offsetOldest, isSync, randomPart bool, retryMax int) (*ConsumerClient, error) {
/**
* @author: yangchangjia
* @email 1320259466@qq.com
* @date: 2023/9/7 10:08 AM
* @desc: about the role of function.
* @param addrs, the kafka cluster address, such as: []string{"localhost:9192","localhost:9292","localhost:9392"}
* @param username, the kafka username
* @param password, the kafka password
* @return null
*/
config := sarama.NewConfig()
if len(username) > 0 {
config.Net.SASL.Enable = true
config.Net.SASL.User = username
config.Net.SASL.Password = password
config.Net.SASL.Mechanism = sarama.SASLTypeSCRAMSHA256
config.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient {
return &XDGSCRAMClient{HashGeneratorFcn: SHA256}
}
}
if isSync {
config.Producer.RequiredAcks = sarama.WaitForAll
}
if randomPart {
config.Producer.Partitioner = sarama.NewRandomPartitioner
} else {
config.Producer.Partitioner = sarama.NewHashPartitioner
}
if retryMax > 0 {
config.Producer.Retry.Max = retryMax
}
config.Consumer.Return.Errors = true
config.Producer.Return.Successes = true
if offsetOldest {
config.Consumer.Offsets.Initial = sarama.OffsetOldest
} else {
config.Consumer.Offsets.Initial = sarama.OffsetNewest
}
consumer, err := sarama.NewConsumer(addrs, config)
return &ConsumerClient{consumer, config.Consumer.Offsets.Initial}, err
}
func (c *ConsumerClient) ConsumerMessage(topic string, partition int32) (value []byte, err error) {
consumePartition, err := c.ConsumePartition(topic, partition, c.Offset)
if err != nil {
return nil, err
}
defer consumePartition.Close()
msg := <-consumePartition.Messages()
return msg.Value, nil
}
func (c *ConsumerClient) ConsumerMessages(topic string, fn func(message []byte) error) (err error) {
partitionList, err := c.Partitions(topic)
if err != nil {
return err
}
var wg sync.WaitGroup
wg.Add(len(partitionList))
for _, partition := range partitionList {
consumePartition, err := c.ConsumePartition(topic, partition, sarama.OffsetOldest)
if err != nil {
return err
}
defer consumePartition.Close()
go func(consumePartition sarama.PartitionConsumer) {
for item := range consumePartition.Messages() {
fn(item.Value)
}
wg.Done()
}(consumePartition)
}
wg.Wait()
return nil
}