Golang Kafka small test message queue

Source: Internet
Author: User

Kafka installation configuration, more information please refer to its official website.

Start Kafka Server

Before this, you need to start zookeeper for service governance (standalone).

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

If you are prompted for permission restrictions plus sudo .

Start Kafka Server

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

Start Message Queuing (This section is test server only)

New 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. Start Producer

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

2. Start Consumer

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

At this point, the message is sent on the Producer side, which is displayed in Consumer, as shown in.

(Consumer a few more messages that I sent before the test)

Action

This article uses the Sarama library as the Go API for Kafka. Sarama Library does not give a very specific document, you can refer to its source code.

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()}

The results are as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.