consumer.go 598 字节
Newer Older
programor_guo's avatar
programor_guo 已提交
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
package kafka

import (
	"github.com/Shopify/sarama"
	"sync"
)

type Consumer struct {
	addr          []string
	WG            sync.WaitGroup
	Topic         string
	PartitionList []int32
	Consumer      sarama.Consumer
}

func NewKafkaConsumer(addr []string, topic string) *Consumer {
	p := Consumer{}
	p.Topic = topic
	p.addr = addr

	consumer, err := sarama.NewConsumer(p.addr, nil)
	if err != nil {
		panic(err)
		return nil
	}
	p.Consumer = consumer

	partitionList, err := consumer.Partitions(p.Topic)
	if err != nil {
		panic(err)
		return nil
	}
	p.PartitionList = partitionList

	return &p
}