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

     1  package main
     2  
     3  import (
     4      "fmt"
     5      "github.com/gogf/gkafka"
     6      "time"
     7  )
     8  
     9  // newKafkaClientProducer creates and returns a new kafka producer client.
    10  func newKafkaClientProducer(topic string) *gkafka.Client {
    11      kafkaConfig               := gkafka.NewConfig()
    12      kafkaConfig.Servers        = "localhost:9092"
    13      kafkaConfig.AutoMarkOffset = false
    14      kafkaConfig.Topics         = topic
    15      return gkafka.NewClient(kafkaConfig)
    16  }
    17  
    18  func main()  {
    19      client := newKafkaClientProducer("test")
    20      defer client.Close()
    21      for {
    22          s := time.Now().String()
    23          fmt.Println("produce:", s)
    24          if err := client.SyncSend(&gkafka.Message{Value: []byte(s)}); err != nil {
    25              fmt.Println(err)
    26          }
    27          time.Sleep(time.Second)
    28      }
    29  }