golang kafka sarama示例

本文档展示了如何使用Go语言中的Sarama和Sarama-Cluster库来实现Kafka的同步与异步生产者以及消费者。消费者部分创建了一个名为'consumer-group1'的消费者组,监听'iris'主题,并处理消费过程中的错误和rebalance通知。生产者部分则提供了同步和异步两种方式发送消息到'iris'主题,确保消息被确认已送达。
摘要由CSDN通过智能技术生成

消费者:

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.AutoCommit.Interval=1*time.Second
	config.Consumer.Offsets.CommitInterval=1*time.Second
	config.Consumer.Offsets.Initial=sarama.OffsetNewest
	config.Group.Return.Notifications = true

	//第二个参数是groupId
	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())
		}
	}()

	// 打印一些rebalance的信息
	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, "")   // 提交offset
			}
		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()
	// fmt.Println()
	// SyncProduce()
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值