You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package kafka
|
|
|
|
import (
|
|
"context"
|
|
"github.com/Shopify/sarama"
|
|
"live-gateway/pkg/logger"
|
|
)
|
|
|
|
type ConsumerGroup struct {
|
|
sarama.ConsumerGroup
|
|
groupId string
|
|
topics []string
|
|
}
|
|
|
|
type ConsumerGroupConfig struct {
|
|
KafkaVersion sarama.KafkaVersion
|
|
OffsetsInitial int64
|
|
IsReturnErr bool
|
|
}
|
|
|
|
func NewConsumerGroup(config *ConsumerGroupConfig, addr, topics []string, groupId string) (*ConsumerGroup, error) {
|
|
c := sarama.NewConfig()
|
|
c.Version = config.KafkaVersion
|
|
c.Consumer.Offsets.Initial = config.OffsetsInitial
|
|
c.Consumer.Return.Errors = config.IsReturnErr
|
|
|
|
client, err := sarama.NewClient(addr, c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
consumerGroup, err := sarama.NewConsumerGroupFromClient(groupId, client)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ConsumerGroup{consumerGroup, groupId, topics}, nil
|
|
}
|
|
|
|
func (cg *ConsumerGroup) RegisterHandlerAndConsumer(handler sarama.ConsumerGroupHandler) {
|
|
ctx := context.Background()
|
|
for {
|
|
err := cg.ConsumerGroup.Consume(ctx, cg.topics, handler)
|
|
if err != nil {
|
|
logger.SLog.Error("RegisterHandlerAndConsumer error: ", err)
|
|
}
|
|
}
|
|
}
|