-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkafka_patition_consumer.go
149 lines (124 loc) · 3.32 KB
/
kafka_patition_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
package main
import (
"crypto/tls"
"crypto/x509"
"flag"
"github.com/Shopify/sarama"
"io/ioutil"
"log"
"os"
"os/signal"
"sync"
)
var (
broker = flag.String("broker", "kafka.bj.baidubce.com:9091", "The broker address can be found in https://cloud.baidu.com/doc/Kafka/QuickGuide.html")
topic = flag.String("topic", "", "Required, the topic to consume from. You can create a topic from console.")
enableTLS = flag.Bool("enableTLS", true, "TLS is to access Baidu Kafka service.")
clientPemPath = flag.String("client_pem", "client.pem", "File path to client.pem provided in kafka-key.zip from console.")
clientKeyPath = flag.String("client_key", "client.key", "File path to client.key provided in kafka-key.zip from console.")
caPemPath = flag.String("ca_pem", "ca.pem", "File path to ca.pem provided in kafka-key.zip from console.")
)
func main() {
if len(os.Args[1:]) == 0 {
flag.Usage()
os.Exit(1)
}
flag.Parse()
if *topic == "" {
panic("Argument topic is required.")
}
sarama.Logger = log.New(os.Stderr, "[sarama]", log.LstdFlags)
config := sarama.NewConfig()
config.Version = sarama.V0_10_1_0
config.Net.TLS.Enable = *enableTLS
if *enableTLS {
config.Net.TLS.Config = configTLS()
}
consumer, err := sarama.NewConsumer([]string{*broker}, config)
if err != nil {
log.Panic(err)
}
defer func() {
if err := consumer.Close(); err != nil {
log.Panic(err)
}
}()
partitions, err := consumer.Partitions(*topic)
if err != nil {
log.Panic(err)
}
var wg sync.WaitGroup
wg.Add(len(partitions))
//目前消费者没有负载均衡到patition
for _, partition := range partitions {
log.Println("consume partition:", partition)
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt)
go func(partition int32) {
partitionConsumer, err := consumer.ConsumePartition(*topic, partition, sarama.OffsetNewest)
if err != nil {
log.Fatalln(err)
return
}
defer func() {
wg.Done()
if err := partitionConsumer.Close(); err != nil {
log.Fatalln(err)
}
}()
ConsumerLoop:
for {
select {
case err := <-partitionConsumer.Errors():
if err != nil {
log.Fatalln("error:", err)
}
case msg := <-partitionConsumer.Messages():
//消息处理可以加上重试,失败记log
//todo flush?
if msg != nil {
log.Println("topic:", msg.Topic, "partition:", msg.Partition, "offset:", msg.Offset, "message:", string(msg.Value))
}
case <-shutdown:
log.Println("stop consuming partition:", partition)
break ConsumerLoop
}
}
}(partition)
}
wg.Wait()
}
func configTLS() (t *tls.Config) {
checkFile(*clientPemPath)
checkFile(*clientKeyPath)
checkFile(*caPemPath)
clientPem, err := tls.LoadX509KeyPair(*clientPemPath, *clientKeyPath)
if err != nil {
log.Panic(err)
}
caPem, err := ioutil.ReadFile(*caPemPath)
if err != nil {
log.Panic(err)
}
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(caPem)
t = &tls.Config{
Certificates: []tls.Certificate{clientPem},
RootCAs: certPool,
InsecureSkipVerify: true,
}
return t
}
func checkFile(path string) {
file, err := os.Open(path)
if err != nil {
panic(err)
}
stat, err := file.Stat()
if err != nil {
panic(err)
}
if stat.Size() == 0 {
panic("Please replace " + path + " with your own. ")
}
}