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.
|
|
|
|
package mq
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/Shopify/sarama"
|
|
|
|
|
"live-gateway/logger"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type msgConsumerGroup struct{}
|
|
|
|
|
|
|
|
|
|
func (msgConsumerGroup) Setup(_ sarama.ConsumerGroupSession) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (msgConsumerGroup) Cleanup(_ sarama.ConsumerGroupSession) error { return nil }
|
|
|
|
|
|
|
|
|
|
func (h msgConsumerGroup) ConsumeClaim(sess sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
|
|
|
|
|
for msg := range claim.Messages() {
|
|
|
|
|
logger.SLog.Debugf("Message topic:%q partition:%d offset:%d value:%s", msg.Topic, msg.Partition, msg.Offset, string(msg.Value))
|
|
|
|
|
|
|
|
|
|
// 标记,sarama会自动进行提交,默认间隔1秒
|
|
|
|
|
sess.MarkMessage(msg, "")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func consumerGroup() sarama.ConsumerGroupHandler {
|
|
|
|
|
return &msgConsumerGroup{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewConsumer() {
|
|
|
|
|
config := sarama.NewConfig()
|
|
|
|
|
config.Version = sarama.V3_1_0_0
|
|
|
|
|
config.Consumer.Return.Errors = false
|
|
|
|
|
config.Consumer.Offsets.Initial = sarama.OffsetNewest
|
|
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
client, err := sarama.NewClient([]string{"127.0.0.1:9093"}, config)
|
|
|
|
|
|
|
|
|
|
cGroup, err := sarama.NewConsumerGroupFromClient("test", client)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer cGroup.Close()
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
err := cGroup.Consume(context.Background(), []string{"danmaku"}, consumerGroup())
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.SLog.Error(err.Error())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|