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: