producer
package main
import (
"fmt"
"github.com/Shopify/sarama"
"log"
"strconv"
)
const (
BROKER = "ip:port"
TOPIC = "xx"
)
func sendMsg(client sarama.SyncProducer, msg *sarama.ProducerMessage) error {
partID, offset, err := client.SendMessage(msg)
if err != nil {
return err
}
fmt.Printf("pid:%v offset:%v\n", partID, offset)
return nil
}
func newMsg(topic, key, content string) *sarama.ProducerMessage {
return &sarama.ProducerMessage{
Topic: topic,
Key: sarama.StringEncoder(key),
Value: sarama.StringEncoder(content),
}
}
func main() {
config := sarama.NewConfig()
config.Producer.RequiredAcks = sarama.WaitForAll
config.Producer.Partitioner = sarama.NewRandomPartitioner
config.Producer.Return.Successes = true
c, err := sarama.NewSyncProducer([]string{BROKER}, config)
if err != nil {
log.Fatalf("[create client failed] err: %v", err)
}
defer c.Close()
msg := newMsg(TOPIC, "ttt", `{"name":"Tim", "age":"18"}`)
if err := sendMsg(c, msg); err != nil {
log.Fatalf("[send msg failed] err: %v", err)
}
for k, word := range []string{"Welcome11", "to", "the", "Confluent", "Kafka", "Golang", "client"} {
msg := newMsg("yy", strconv.Itoa(k), word)
if err := sendMsg(c, msg); err != nil {
fmt.Printf("[send msg failed] err: %v\n", err)
continue
}
}
}
consumer
package main
import (
"fmt"
"github.com/Shopify/sarama"
"log"
)
const (
BROKER = "service-epoch-kafka.epoch-kafka.svc.manager.ucbj.kuber.thc:8000"
TOPIC = "yangkaiyue-test"
)
func main() {
consumer, err := sarama.NewConsumer([]string{BROKER}, nil)
if err != nil {
log.Fatalf("[new consumer failed] err: %v", err)
}
partitionList, err := consumer.Partitions(TOPIC)
if err != nil {
log.Fatalf("[get partition list failed] err: %v", err)
}
for partition := range partitionList {
pc, err := consumer.ConsumePartition(TOPIC, int32(partition), sarama.OffsetNewest)
if err != nil {
fmt.Printf("failed to start consumer for partition %d,err:%v\n", partition, err)
return
}
defer pc.AsyncClose()
go func(sarama.PartitionConsumer) {
for msg := range pc.Messages() {
fmt.Printf("Partition:%d Offset:%d Key:%v Value:%v\n", msg.Partition, msg.Offset, msg.Key, string(msg.Value))
}
}(pc)
}
select{}
}