github.com/OrigamiWang/msd/micro@v0.0.0-20240229032328-b62246268db9/mq/kafka/consumer.go (about)

     1  package kafka
     2  
     3  import (
     4  	"github.com/IBM/sarama"
     5  	"github.com/OrigamiWang/msd/micro/const/mq"
     6  	logutil "github.com/OrigamiWang/msd/micro/util/log"
     7  )
     8  
     9  // consume message
    10  var (
    11  	kafkaConsumer        sarama.Consumer
    12  	martitionConsumerMap map[string]sarama.PartitionConsumer
    13  )
    14  
    15  func init() {
    16  	var err error
    17  
    18  	kafkaConsumer, err = sarama.NewConsumer([]string{"localhost:9092"}, nil)
    19  	if err != nil {
    20  		logutil.Error("Failed to start consumer, err: %v", err)
    21  	}
    22  	martitionConsumerMap = make(map[string]sarama.PartitionConsumer)
    23  }
    24  
    25  func ConsumeMsg(topic string, handler func(...string)) error {
    26  	if martitionConsumerMap[topic] == nil {
    27  		partitionConsumer, err := kafkaConsumer.ConsumePartition(topic, mq.PARTITION_NUM, sarama.OffsetNewest)
    28  		if err != nil {
    29  			logutil.Error("Failed to start partition consumer, err: %v", err)
    30  			return err
    31  		}
    32  		martitionConsumerMap[topic] = partitionConsumer
    33  	}
    34  	partitionConsumer := martitionConsumerMap[topic]
    35  	defer func() {
    36  		if err := partitionConsumer.Close(); err != nil {
    37  			logutil.Error("Failed to close partition consumer, err: %v", err)
    38  		}
    39  	}()
    40  
    41  	for message := range partitionConsumer.Messages() {
    42  		logutil.Info("Received message, value: %s, key: %s", string(message.Value), string(message.Key))
    43  		handler(string(message.Key), string(message.Value))
    44  	}
    45  	return nil
    46  }