2019-03-04 09:19:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"github.com/Shopify/sarama"
|
|
|
|
"encoding/json"
|
2019-07-10 12:40:43 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"time"
|
2019-04-29 09:05:15 +00:00
|
|
|
)
|
2019-03-04 09:19:55 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
wg sync.WaitGroup
|
|
|
|
)
|
|
|
|
|
|
|
|
func start(pool *ResourcePool) {
|
2019-04-16 08:59:19 +00:00
|
|
|
consumer, err := sarama.NewConsumer([]string{"kafka-nod21:9092", "kafka-node2:9092", "kafka-node3:9092"}, nil)
|
2019-07-10 12:40:43 +00:00
|
|
|
for {
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Warn(err)
|
|
|
|
time.Sleep(time.Second * 5)
|
|
|
|
consumer, err = sarama.NewConsumer([]string{"kafka-nod21:9092", "kafka-node2:9092", "kafka-node3:9092"}, nil)
|
2019-03-04 09:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
partitionList, err := consumer.Partitions("yao")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for partition := range partitionList {
|
|
|
|
pc, err := consumer.ConsumePartition("yao", int32(partition), sarama.OffsetNewest)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer pc.AsyncClose()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
go func(sarama.PartitionConsumer) {
|
|
|
|
defer wg.Done()
|
|
|
|
for msg := range pc.Messages() {
|
2019-04-16 08:59:19 +00:00
|
|
|
var nodeStatus NodeStatus
|
|
|
|
err = json.Unmarshal([]byte(string(msg.Value)), &nodeStatus)
|
2019-03-04 09:19:55 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
2019-04-16 08:59:19 +00:00
|
|
|
pool.update(nodeStatus)
|
2019-03-04 09:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}(pc)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
consumer.Close()
|
|
|
|
}
|