2019-03-04 09:19:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"github.com/Shopify/sarama"
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
2019-04-16 08:59:19 +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-03-04 09:19:55 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|