golang基础-WaitGroup、kafka消费者
来源:互联网 发布:mysql offset limit 编辑:程序博客网 时间:2024/05/21 07:08
- WaitGroup
- kafka消费者
WaitGroup
WaitGroup在go语言中,用于线程同步,单从字面意思理解,wait等待的意思,group组、团队的意思,WaitGroup就是指等待一组,等待一个系列执行完成后才会继续向下执行。
package mainimport ( "fmt" "sync" "time")func main() { wg := sync.WaitGroup{} for i := 0; i < 10; i++ { wg.Add(1) go calc(&wg, i) } wg.Wait() fmt.Println("all goroutine finish")}func calc(w *sync.WaitGroup, i int) { fmt.Println("calc:", i) time.Sleep(time.Second) w.Done()}
输出如下:
PS E:\golang\go_pro\src\safly> go run waitGroup.gocalc: 0calc: 1calc: 4calc: 2calc: 3calc: 9calc: 6calc: 7calc: 5calc: 8all goroutine finishPS E:\golang\go_pro\src\safly>
kafka消费者
以下博客是通过生产者创建、发送消息至kafka
博客链接
现在我们站在消费者的角度,来进行收取消息
package mainimport ( "fmt" "strings" "sync" "github.com/Shopify/sarama")var ( wg sync.WaitGroup)func main() { //创建消费者 consumer, err := sarama.NewConsumer(strings.Split("192.168.11.48:9092", ","), nil) if err != nil { fmt.Println("Failed to start consumer: %s", err) return } //设置分区 partitionList, err := consumer.Partitions("nginx_log") if err != nil { fmt.Println("Failed to get the list of partitions: ", err) return } fmt.Println(partitionList) //循环分区 for partition := range partitionList { pc, err := consumer.ConsumePartition("nginx_log", int32(partition), sarama.OffsetNewest) if err != nil { fmt.Printf("Failed to start consumer for partition %d: %s\n", partition, err) return } defer pc.AsyncClose() go func(pc sarama.PartitionConsumer) { wg.Add(1) for msg := range pc.Messages() { fmt.Printf("Partition:%d, Offset:%d, Key:%s, Value:%s", msg.Partition, msg.Offset, string(msg.Key), string(msg.Value)) fmt.Println() } wg.Done() }(pc) } //time.Sleep(time.Hour) wg.Wait() consumer.Close()}
接下来我们测试上面的消费者示例代码,在进行测试前我们需要如下的准备工作
1、启动zookeeper
2、启动kafka
3、创立生产者topic
PS E:\develop\kafka\kafka_2.12-1.0.0> .\bin\windows\kafka-console-consumer.bat --topic nginx_log --zookeeper 127.0.0.1 2181
4、执行生产者发送消息至kafka代码
5、执行消费者代码程序
第4步的代码如下:
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 msg := &sarama.ProducerMessage{} msg.Topic = "nginx_log" msg.Value = sarama.StringEncoder("this is a good test, my message is good") client, err := sarama.NewSyncProducer([]string{"192.168.11.28:9092"}, config) if err != nil { fmt.Println("producer close, err:", err) return } defer client.Close() pid, offset, err := client.SendMessage(msg) if err != nil { fmt.Println("send message failed,", err) return } fmt.Printf("pid:%v offset:%v\n", pid, offset)}
然后最后看效果图如下:
阅读全文
0 0
- golang基础-WaitGroup、kafka消费者
- golang sync WaitGroup
- golang sync WaitGroup
- GoLang WaitGroup使用
- golang积累-WaitGroup包装
- GOLANG sync.WaitGroup讲解
- golang中的sync.WaitGroup
- golang sync WaitGroup
- golang中协程同步sync.waitGroup
- golang线程同步WaitGroup简介
- golang waitgroup 及 sync.Once 示例
- golang kafka
- kafka消费者
- golang 中sync.WaitGroup以及select简单实用
- Golang 1.7.3 Context 简单用法.类似sync.WaitGroup
- golang生产者与消费者
- golang生产者与消费者
- golang基础-kafka、zookeeper搭建、go终端发送数据给kafka
- bpmn业务流图注意事项,常见报错问题
- 大数据日知录:架构与算法.pdf 免费下载
- 欢迎使用CSDN-markdown编辑器
- 路由传值
- unity日常——EventTrigger
- golang基础-WaitGroup、kafka消费者
- 链表的倒置
- Spring MVC pom.xml
- 动态SQL.绑定变量.静态SQL的性能对比
- Git服务器搭建
- 文章标题 第十二周学习报告
- Angular4 变更检测和Docheck钩子
- 实现领域驱动设计 (美)弗农著.pdf 免费下载
- C++ typedef typename 作用