golang kafka小試訊息佇列

來源:互聯網
上載者:User

Kafka 安裝配置、更多資料請參考其官網。

啟動 kafka server

在這之前需要啟動 zookeeper 做服務治理(單機)。

$ bin/zkServer.sh status conf/zoo_sample.cfg

如提示許可權限制加上 sudo

啟動 kafka server

$ bin/kafka-server-start.sh config/server.properties

啟動訊息佇列(本部分僅為測試 server)

建立 Topic

$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test$ bin/kafka-topics.sh --list --zookeeper localhost:2181 (test)

1. 啟動 Producer

$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test

2. 啟動 Consumer

bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning

此時在 Producer 端發送訊息,在 Consumer 就會顯示,如所示。

(中 Consumer 多出了好幾個訊息是我之前測試發出的)

Action

本文使用 sarama 庫作為 kafka 的 go API。sarama 庫沒有給出很具體的文檔,可以參考其源碼。

Producer

package mainimport (    "fmt"    "github.com/Shopify/sarama")func main() {    config := sarama.NewConfig()    config.Producer.RequiredAcks = sarama.WaitForAll    config.Producer.Partitioner = sarama.NewRandomPartitioner    config.Producer.Return.Successes = true    addr := []string{"localhost:9092"}    producer, err := sarama.NewSyncProducer(addr, config)    if err != nil {        panic(err)    }    defer producer.Close()    msg := &sarama.ProducerMessage{        Topic:     "hello",        Partition: int32(-1),        Key:       sarama.StringEncoder("key"),    }    var value string    for {        _, err := fmt.Scanf("%s", &value)        if err != nil {            break        }        msg.Value = sarama.ByteEncoder(value)        fmt.Println(value)        partition, offset, err := producer.SendMessage(msg)        if err != nil {            fmt.Println("Send message Fail")        }        fmt.Printf("Partition = %d, offset=%d\n", partition, offset)    }}

Consumer

package mainimport (    "fmt"    "sync"    "github.com/Shopify/sarama")var (    wg  sync.WaitGroup)func main() {    consumer, err := sarama.NewConsumer([]string{"localhost:9092"}, nil)    if err != nil {        panic(err)    }    partitionList, err := consumer.Partitions("hello")    if err != nil {        panic(err)    }    for partition := range partitionList {        pc, err := consumer.ConsumePartition("hello", int32(partition), sarama.OffsetNewest)        if err != nil {            panic(err)        }        defer pc.AsyncClose()        wg.Add(1)        go func(sarama.PartitionConsumer) {            defer wg.Done()            for msg := range pc.Messages() {                fmt.Printf("Partition:%d, Offset:%d, Key:%s, Value:%s\n", msg.Partition, msg.Offset, string(msg.Key), string(msg.Value))            }        }(pc)    }    wg.Wait()    consumer.Close()}

結果如下:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.