github.com/gogf/gkafka@v1.0.1-0.20190702070843-033a14468069/README.MD (about)

     1  # gkafka [![Go Doc](https://godoc.org/github.com/gogf/gkafka?status.svg)](https://godoc.org/github.com/gogf/gkafka) [![Documents](https://img.shields.io/badge/docs-100%25-green.svg)](https://goframe.org) [![License](https://img.shields.io/github/license/gogf/gkafka.svg?style=flat)](https://github.com/gogf/gkafka) [![Language](https://img.shields.io/badge/language-go-blue.svg)](https://github.com/gogf/gkafka) [![Release](https://img.shields.io/github/release/gogf/gkafka.svg?style=flat)](https://github.com/gogf/gkafka/releases)
     2  
     3  Package gkafka provides producer and consumer client for kafka server.
     4  
     5  # Installation
     6  ```
     7  go get -u github.com/gogf/gkafka
     8  ```
     9  or use `go.mod`
    10  ```
    11  require github.com/gogf/gkafka latest
    12  ```
    13  
    14  # Documentation
    15  
    16  * [中文文档](https://goframe.org/database/gkafka/index)
    17  * [godoc.org/github.com/gogf/gkafka](https://godoc.org/github.com/gogf/gkafka)
    18  
    19  # Quick Start
    20  
    21  ## Producer
    22  ```go
    23  package main
    24  
    25  import (
    26      "fmt"
    27      "github.com/gogf/gkafka"
    28      "time"
    29  )
    30  
    31  func newKafkaClientProducer(topic string) *gkafka.Client {
    32      kafkaConfig               := gkafka.NewConfig()
    33      kafkaConfig.Servers        = "localhost:9092"
    34      kafkaConfig.AutoMarkOffset = false
    35      kafkaConfig.Topics         = topic
    36      return gkafka.NewClient(kafkaConfig)
    37  }
    38  
    39  func main()  {
    40      client := newKafkaClientProducer("test")
    41      defer client.Close()
    42      for {
    43          s := time.Now().String()
    44          fmt.Println("produce:", s)
    45          if err := client.SyncSend(&gkafka.Message{Value: []byte(s)}); err != nil {
    46              fmt.Println(err)
    47          }
    48          time.Sleep(time.Second)
    49      }
    50  }
    51  ```
    52  ## Consumer
    53  ```go
    54  package main
    55  
    56  import (
    57      "fmt"
    58      "github.com/gogf/gkafka"
    59  )
    60  
    61  func newKafkaClientConsumer(topic, group string) *gkafka.Client {
    62      kafkaConfig               := gkafka.NewConfig()
    63      kafkaConfig.Servers        = "localhost:9092"
    64      kafkaConfig.AutoMarkOffset = false
    65      kafkaConfig.Topics         = topic
    66      kafkaConfig.GroupId        = group
    67      return gkafka.NewClient(kafkaConfig)
    68  }
    69  
    70  func main()  {
    71      group  := "test-group"
    72      topic  := "test"
    73      client := newKafkaClientConsumer(topic, group)
    74      defer client.Close()
    75      for {
    76          if msg, err := client.Receive(); err != nil {
    77              fmt.Println(err)
    78              break
    79          } else {
    80              fmt.Println("consume:", msg.Partition, msg.Offset, string(msg.Value))
    81              msg.MarkOffset()
    82          }
    83      }
    84  }
    85  ```
    86  
    87  ## Topics
    88  ```go
    89  package main
    90  
    91  import (
    92      "fmt"
    93      "github.com/gogf/gkafka"
    94  )
    95  
    96  func main()  {
    97      config        := gkafka.NewConfig()
    98      config.Servers = "localhost:9092"
    99  
   100      client := gkafka.NewClient(config)
   101      defer client.Close()
   102  
   103      fmt.Println(client.Topics())
   104  }
   105  ```
   106  
   107  
   108  # License
   109  
   110  `gkafka` is licensed under the [MIT License](LICENSE), 100% free and open-source, forever.
   111