-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
326 lines (288 loc) · 8.92 KB
/
main.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package main
import (
"errors"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/urfave/cli"
ui "github.com/wupeaking/kafkainfo/uishow"
log "github.com/wupeaking/logrus"
kafka "github.com/Shopify/sarama"
kafkaCluster "github.com/bsm/sarama-cluster"
)
// 获取kafka的所有topic
func topicCommand(c *cli.Context) error {
// 获取kafka地址
addr := c.String("addr")
// 获取第一个参数
arags := c.Args().Get(0)
if addr == "" {
log.Error(`必须传递kafka地址 使用"kakfainfo topic -h" 查看帮助信息`)
return errors.New("kafka地址为空")
}
// 创建一个kafka客户端
kafkaCli, err := kafka.NewClient(strings.Split(addr, ","), nil)
if err != nil {
log.Error("连接kafka失败: ", err)
return err
}
defer kafkaCli.Close()
switch arags {
case "list":
topics, err := kafkaCli.Topics()
if err != nil {
log.Error("获取kafka的topics失败: ", err)
return err
}
log.Info("所有的topics: ", topics)
return nil
case "leader":
topic := c.String("t")
if topic == "" {
log.Error("topic 参数必须指定")
}
part := c.Int("p")
broker, err := kafkaCli.Leader(topic, int32(part))
if err != nil {
log.Error("获取分区leader失败", "part:", part, "topic: ", topic, "err:", err)
return errors.New("获取leader失败")
} else {
log.Info("topic:", topic, " part:", part, " addr: ", broker.Addr(), " id:", broker.ID())
return nil
}
case "":
log.Info("kafkainfo topic --addr localhost:9092 list 显示所有存在的topic")
log.Info("kafkainfo topic --addr localhost:9092 -t beats -p 0 leader 显示指定topic的某个分区的leader")
}
return nil
}
// 获取kafka的所有分区
func partitionsCommand(c *cli.Context) error {
addr := c.String("addr")
if addr == "" {
log.Error(`必须传递kafka地址 使用"kakfainfo partitions -h" 查看帮助信息`)
return errors.New("kafka地址为空")
}
//指定topic的分区
topic := c.String("topic")
if topic == "" {
log.Error(`topic 是必须参数 使用"kakfainfo partitions -h" 查看帮助信息`)
return errors.New("need topic ")
}
// 创建一个kafka客户端
kafkaCli, err := kafka.NewClient(strings.Split(addr, ","), nil)
if err != nil {
log.Error("连接kafka失败: ", err)
return err
}
defer kafkaCli.Close()
parts, err := kafkaCli.Partitions(topic)
if err != nil {
log.Error("获取kafka的分区失败: ", err)
return err
}
log.Info(topic, "的分区情况: ", parts)
return nil
}
// 获取所有的broker
func brokerCommand(c *cli.Context) error {
addr := c.String("addr")
if addr == "" {
log.Error(`必须传递kafka地址 使用"kakfainfo partitions -h" 查看帮助信息`)
return errors.New("kafka地址为空")
}
// 创建一个kafka客户端
kafkaCli, err := kafka.NewClient(strings.Split(addr, ","), nil)
if err != nil {
log.Error("连接kafka失败: ", err)
return err
}
defer kafkaCli.Close()
brokers := kafkaCli.Brokers()
for i, broker := range brokers {
log.WithField("index:", i).Info("id: ", broker.ID, " addr: ", broker.Addr())
}
return nil
}
// 生产消息命令
func produceCommand(c *cli.Context) error {
addr := c.String("addr")
topic := c.String("topic")
count := c.Int("c")
msg := c.String("m")
if addr == "" || topic == "" {
log.Error("addr, topic参数必须设置")
return errors.New("addr, topic参数必须设置")
}
if msg == "" {
msg = "this is kakfainfo test "
}
if count <= 0 {
count = 1
}
config := kafka.NewConfig()
config.ClientID = "kafkainfo"
// 同步模式的生产者 必须设置为TRUE
config.Producer.Return.Successes = true
// 生产者发往哪一个分区 默认是通过key的hash值 现在改为随机
config.Producer.Partitioner = kafka.NewRandomPartitioner
produce, e := kafka.NewSyncProducer(strings.Split(addr, ","), config)
if e != nil {
log.Error("生成消息失败", e)
return errors.New("生成消息失败")
}
defer produce.Close()
sumCount := 0
for count > 0 {
count--
proMsg := &kafka.ProducerMessage{
Topic: topic,
Partition: int32(-1),
Key: kafka.StringEncoder(time.Now().String()),
}
proMsg.Value = kafka.ByteEncoder(msg)
part, offset, err := produce.SendMessage(proMsg)
if err != nil {
log.Error("生产消息失败")
break
} else {
sumCount++
log.Info("生产消息信息: ", "part: ", part, " offset:", offset)
}
}
log.Info("生产消息成功个数: ", sumCount)
return nil
}
// 消费kafka的内容
func consumCommand(c *cli.Context) error {
addr := c.String("addr")
topic := c.String("topic")
count := c.Int("c")
forever := c.Bool("f")
if addr == "" || topic == "" {
log.Error("addr, topic参数必须设置")
return errors.New("addr, topic参数必须设置")
}
if count <= 0 && !forever {
log.Error("count或者forever必须设置其中一个", "count表示消费count个内容后退出",
"forever 表示一直消费直到按下Ctrl-C后退出")
return errors.New("参数错误")
}
config := kafkaCluster.NewConfig()
// 一秒同步一下偏移值
config.Consumer.Offsets.CommitInterval = 1 * time.Second
config.Consumer.Offsets.Initial = kafka.OffsetOldest
config.ClientID = "kafkainfo"
//
config.Consumer.Return.Errors = true // 是否返回错误通知
config.Group.Return.Notifications = false
// 创建一个消费者
consumer, err := kafkaCluster.NewConsumer(strings.Split(addr, ","), "kafkainfo",
[]string{topic}, config)
if err != nil {
log.Error("新建消费者失败", err)
return errors.New("新建消费者失败")
}
defer consumer.Close()
go func() {
for err := range consumer.Errors() {
log.Error("消费消息出现异常: ", err)
}
}()
sig := make(chan os.Signal)
// 监控ctrl-c
signal.Notify(sig, syscall.SIGINT)
sumSuc := 0
if count > 0 {
goto countLabel
} else {
goto foreverLabel
}
countLabel:
// 开始消费消息
for {
select {
case msg := <-consumer.Messages():
sumSuc++
log.Info("主题: ", msg.Topic, " 分区: ", msg.Partition, " 偏移量: ",
msg.Offset, " key: ", msg.Key, " value:", string(msg.Value))
consumer.MarkOffset(msg, "") // mark message as processed
if sumSuc >= count {
break
}
case <-sig:
log.Info("总共消费消息数量:", sumSuc)
return nil
}
}
foreverLabel:
for {
select {
case msg := <-consumer.Messages():
sumSuc++
log.Info("主题: ", msg.Topic, " 分区: ", msg.Partition, " 偏移量: ",
msg.Offset, " key: ", string(msg.Key), " value: ", string(msg.Value))
consumer.MarkOffset(msg, "") // mark message as processed
case <-sig:
log.Info("总共消费消息数量:", sumSuc)
return nil
}
}
}
func main() {
log.SetLevel(log.DebugLevel)
// 获取kafka存在的所有topic
app := cli.NewApp()
app.Name = "kafkainfo"
app.Usage = "kafkainfo是一个简单的调试kafka的命令行工具"
app.Version = "0.0.1"
//app.Action = func(c *cli.Context) error {
// println("当没有匹配的子命令时,执行这个函数")
// return nil
//}
// 初始化几个子命令
// 1. 获取kaka的topics
topic := cli.Command{Name: "topics", Usage: "kafka的topic信息", Action: topicCommand,
Flags: []cli.Flag{
cli.StringFlag{Name: "addr, ip", Usage: "kafka集群的任意一个地址"},
cli.StringFlag{Name: "t, topic", Usage: "kafka集群的指定topic"},
cli.IntFlag{Name: "p", Usage: "kakfa某个topic的分区"},
},
}
// 2. 分区
partitions := cli.Command{Name: "partitions", Usage: "kafka的指定topic的分区", Action: partitionsCommand,
Flags: []cli.Flag{
cli.StringFlag{Name: "addr, ip", Usage: "kafka集群的任意一个地址"},
cli.StringFlag{Name: "t,topic", Usage: "指定kafka的topic"},
}}
// brokers
brokers := cli.Command{Name: "brokers", Usage: "kafka的指定所有brokers", Action: brokerCommand,
Flags: []cli.Flag{
cli.StringFlag{Name: "addr, ip", Usage: "kafka集群的任意一个地址"},
}}
// produce 生产消息
produce := cli.Command{Name: "produce", Usage: "向kafka生成若干消息", Action: produceCommand,
Flags: []cli.Flag{
cli.StringFlag{Name: "addr, ip", Usage: "kafka集群的任意一个地址"},
cli.StringFlag{Name: "t, topic", Usage: "指定topic"},
cli.StringFlag{Name: "m, message", Usage: "消息内容"},
cli.IntFlag{Name: "c, count", Usage: "生产几份消息"},
}}
// 消费消息
consum := cli.Command{Name: "consum", Usage: "消费kafka消息", Action: consumCommand,
Flags: []cli.Flag{
cli.StringFlag{Name: "addr, ip", Usage: "kafka集群的任意一个地址"},
cli.StringFlag{Name: "t, topic", Usage: "指定topic"},
cli.IntFlag{Name: "c, count", Usage: "消费几份消息"},
cli.BoolFlag{Name: "f, forerver", Usage: "一直消费 直到按下Ctrl-C"},
}}
// 图形显示kafka信息
uishow := cli.Command{Name: "uishow", Usage: "在shell上图形化显示kafka概述信息", Action: ui.UIshowCommand,
Flags: []cli.Flag{
cli.StringFlag{Name: "addr, ip", Usage: "kafka集群的任意一个地址"},
}}
app.Commands = []cli.Command{topic, partitions, brokers, produce, consum, uishow}
app.Run(os.Args)
}