消费者:
package main
import (
"fmt"
"github.com/Shopify/sarama"
KafkaCluster "github.com/bsm/sarama-cluster"
"log"
"os"
"os/signal"
"time"
)
func main(){
KafkaConsumerCluster("")
}
func KafkaConsumerCluster(consumerId string) {
brokers := []string{"127.0.0.1:9092"}
topics := []string{"iris"}
config := KafkaCluster.NewConfig()
config.Consumer.Return.Errors = true
config.Consumer.Offsets.CommitInterval=1*time.Second
config.Consumer.Offsets.Initial=sarama.OffsetNewest
config.Group.Return.Notifications = true
consumer, err := KafkaCluster.NewConsumer(brokers, "consumer-group1", topics, config)
if err != nil {
panic(err)
}
defer consumer.Close()
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
go func() {
for err := range consumer.Errors() {
log.Printf("Error: %s\n", err.Error())
}
}()
go func() {
for ntf := range consumer.Notifications() {
log.Printf("Rebalanced: %+v\n", ntf)
}
}()
for {
select {
case msg, ok := <-consumer.Messages():
if ok {
fmt.Fprintf(os.Stdout, "%s : %s/%d/%d\t%s\t%s\n", consumerId,msg.Topic, msg.Partition, msg.Offset, msg.Key, msg.Value)
consumer.MarkOffset(msg, "")
}
case <-signals:
return
}
}
}
生产者:
package main
import (
"fmt"
"github.com/Shopify/sarama"
"time"
)
func SyncProduce() {
config := sarama.NewConfig()
config.Producer.RequiredAcks = sarama.WaitForAll
config.Producer.Partitioner = sarama.NewRandomPartitioner
config.Producer.Return.Successes = true
msg := &sarama.ProducerMessage{}
msg.Topic = "iris"
content := "this is a sync message"
msg.Value = sarama.StringEncoder(content)
client, err := sarama.NewSyncProducer([]string{"127.0.0.1:9092"}, config)
if err != nil {
fmt.Println("producer close, err:", err)
return
}
defer client.Close()
partition, offset, err := client.SendMessage(msg)
if err != nil {
fmt.Println("send message failed,", err)
return
}
fmt.Printf("send msg partition/offset: %d/%d, value is: %s",partition,offset,content)
}
func AsyncProduce() {
config := sarama.NewConfig()
config.Producer.RequiredAcks = sarama.WaitForAll
config.Producer.Partitioner = sarama.NewRandomPartitioner
config.Producer.Return.Successes = true
client, err := sarama.NewAsyncProducer([]string{"127.0.0.1:9092"}, config)
if err != nil {
fmt.Println("error is:", err.Error())
return
}
defer client.AsyncClose()
go func(p sarama.AsyncProducer) {
for {
select {
case msg := <-p.Successes():
value,_ := msg.Value.Encode()
fmt.Printf("send msg partition/offset: %d/%d, value is: %s",msg.Partition,msg.Offset,string(value))
return
case fail := <-p.Errors():
fmt.Println("err: ", fail.Err)
return
}
}
}(client)
msg := &sarama.ProducerMessage{
Topic: "iris",
Value: sarama.ByteEncoder("this is a async message"),
}
client.Input() <- msg
time.Sleep(time.Second * 1)
}
func main() {
AsyncProduce()
}