github.com/gogf/gkafka@v1.0.1-0.20190702070843-033a14468069/main/consumer.go (about)

     1  package main
     2  
     3  import (
     4      "fmt"
     5      "github.com/gogf/gkafka"
     6  )
     7  
     8  // newKafkaClientConsumer creates and returns a new kafka producer client.
     9  func newKafkaClientConsumer(topic, group string) *gkafka.Client {
    10      kafkaConfig               := gkafka.NewConfig()
    11      kafkaConfig.Servers        = "localhost:9092"
    12      kafkaConfig.AutoMarkOffset = false
    13      kafkaConfig.Topics         = topic
    14      kafkaConfig.GroupId        = group
    15      return gkafka.NewClient(kafkaConfig)
    16  }
    17  
    18  func main()  {
    19      group  := "test-group"
    20      topic  := "test"
    21      client := newKafkaClientConsumer(topic, group)
    22      defer client.Close()
    23  
    24  	// Mark the offset from reading.
    25      //client.MarkOffset(topic, 0, 6)
    26      for {
    27          if msg, err := client.Receive(); err != nil {
    28              fmt.Println(err)
    29              break
    30          } else {
    31              fmt.Println("consume:", msg.Partition, msg.Offset, string(msg.Value))
    32              msg.MarkOffset()
    33          }
    34      }
    35  }