-
Notifications
You must be signed in to change notification settings - Fork 140
/
sarama_comsumer_group.go
56 lines (49 loc) · 1.39 KB
/
sarama_comsumer_group.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
package main
import (
"context"
"fmt"
"github.com/Shopify/sarama"
)
type ConsumerGroupHandler struct {
}
func (ConsumerGroupHandler) Setup(_ sarama.ConsumerGroupSession) error {
return nil
}
func (ConsumerGroupHandler) Cleanup(_ sarama.ConsumerGroupSession) error {
return nil
}
func (ConsumerGroupHandler) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
for msg := range claim.Messages() {
fmt.Printf("topic: %s, patitions: %d, msg: %s, offset: %d \n", msg.Topic, msg.Partition, msg.Value, msg.Offset)
session.MarkMessage(msg, "")
}
return nil
}
func main() {
config := sarama.NewConfig()
config.Version = sarama.V1_0_0_0
config.Consumer.Return.Errors = true
config.Consumer.Group.Rebalance.Strategy = sarama.BalanceStrategyRoundRobin
client, err := sarama.NewClient([]string{"localhost:9092"}, config)
if err != nil {
panic(err)
}
consumerGroup, errC := sarama.NewConsumerGroupFromClient("group_test13", client)
if errC != nil {
fmt.Println("this is consumerGroup error: ", errC.Error())
}
defer consumerGroup.Close()
handle := new(ConsumerGroupHandler)
// track errors
go func() {
for err := range consumerGroup.Errors() {
fmt.Println("group ERROR: ", err.Error())
}
}()
for {
err := consumerGroup.Consume(context.Background(), []string{"test13"}, handle)
if err != nil {
fmt.Println("consume Error: ", err.Error())
}
}
}