golang kafka – hello world
https://github.com/Shopify/sarama
https://shopify.github.io/sarama/
consumer.go
"github.com/Shopify/sarama" |
logger = log.New(os.Stderr, "[srama]", log.LstdFlags) |
consumer, err := sarama.NewConsumer(strings.Split("localhost:9092", ","), nil) |
logger.Println("Failed to start consumer: %s", err) |
partitionList, err := consumer.Partitions("hello") |
logger.Println("Failed to get the list of partitions: ", err) |
for partition := range partitionList { |
pc, err := consumer.ConsumePartition("hello", int32(partition), sarama.OffsetNewest) |
logger.Printf("Failed to start consumer for partition %d: %s\n", partition, err) |
go func(sarama.PartitionConsumer) { |
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)) |
logger.Println("Done consuming topic hello") |
producer.go
"github.com/Shopify/sarama" |
logger = log.New(os.Stderr, "[srama]", log.LstdFlags) |
config := sarama.NewConfig() |
config.Producer.RequiredAcks = sarama.WaitForAll |
config.Producer.Partitioner = sarama.NewRandomPartitioner |
msg := &sarama.ProducerMessage{} |
msg.Partition = int32(-1) |
msg.Key = sarama.StringEncoder("key") |
msg.Value = sarama.ByteEncoder("你好, 世界!") |
producer, err := sarama.NewSyncProducer(strings.Split("localhost:9092", ","), config) |
logger.Println("Failed to produce message: %s", err) |
partition, offset, err := producer.SendMessage(msg) |
logger.Println("Failed to produce message: ", err) |
logger.Printf("partition=%d, offset=%d\n", partition, offset) |
- [Golang] kafka集群搭建和golang版生产者和消费者
一.kafka集群搭建 至于kafka是什么我都不多做介绍了,网上写的已经非常详尽了. 1. 下载zookeeper https://zookeeper.apache.org/releases.ht ...
- golang kafka client
针对golang的 kafka client 有很多开源package,例如sarama, confluent等等.在使用sarama 包时,高并发中偶尔遇到crash.于是改用confluent-k ...
- golang kafka clinet 内存泄露问题处理
go 内存泄露 新版本服务跑上一天内存占用20g,显然是内存泄露 内存泄露的问题难在定位 技术上的定位 主要靠 pprof 生成统计文件 之前写web项目 基于net/http/pprof 可以看到运 ...
- panic: interface conversion: interface {} is nil, not chan *sarama.ProducerError
使用golang kafka sarama 包时,遇到如下问题: 高并发情况下使用同步sync producer,偶尔遇到crash: panic: interface conversion: int ...
- Spring Boot(十三)RabbitMQ安装与集成
一.前言 RabbitMQ是一个开源的消息代理软件(面向消息的中间件),它的核心作用就是创建消息队列,异步接收和发送消息,MQ的全程是:Message Queue中文的意思是消息队列. 1.1 使用场 ...
- Shell 字符串处理
字符串处理方式 计算字符串长度 获取子串在字符串中的索引位置 计算子串长度 抽取(截取)字串 1.计算字符串长度,有两种方式 $ ${#string} $ expr length "$str ...
- 基于 MySQL Binlog 的 Elasticsearch 数据同步实践 原
一.背景 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品.订单等数据的多维度检索. 使用 Elasticsearch 存储业务数据可以 ...
- (转载)rabbitmq与springboot的安装与集成
原文地址:https://segmentfault.com/a/1190000016991529 一.前言 RabbitMQ是一个开源的消息代理软件(面向消息的中间件),它的核心作用就是创建消息队列, ...
- shell编程:字符串处理方式
字符串处理方式 计算字符串长度 获取子串在字符串中的索引位置 计算子串长度 抽取(截取)字串 1.计算字符串长度,有两种方式 $ ${#string} $ expr length "$str ...
随机推荐
- 基于Spring-WS的Restful API的集成测试
在很多Java企业级应用中,Spring占据了非常重要的位置,这就导致了基本上的技术选型都是围绕着Spring来, 比方说笔者最近的项目需要开发一个Restful的API接口,选型的时候就说,客户架构 ...
- Atlas-手淘组件化框架的前世今生和未来的路
今天手淘技术团队宣布正式开源它们的容器框架Atlas,项目地址: https://github.com/alibaba/atlas 同时他们还推出了项目官网,上线了技术文档: http://atlas ...
- Spring还使用基于 JSR-250 注释,它包括 @PostConstruct, @PreDestroy 和 @Resource 注释
@PostConstruct 和 @PreDestroy 注释: 你可以使用 @PostConstruct 注释作为初始化回调函数的一个替代,@PreDestroy 注释作为销毁回调函数的一个替代,其 ...
- 一些关于Canny边缘检测算法的改进
传统的Canny边缘检测算法是一种有效而又相对简单的算法,可以得到很好的结果(可以参考上一篇Canny边缘检测算法的实现).但是Canny算法本身也有一些缺陷,可以有改进的地方. 1. Canny边缘 ...
- [译]Selenium Python文档:五、Waits等待
大多数现代web应用都使用了AJAX技术.当浏览器加载一个页面的时候,该页面内的元素可能在不用的时间间隔内进行加载.这使得元素定位变得比较困难:如果一个元素还没有出现在DOM中,定位函数将会抛出一个E ...
- python的tkinter版本不匹配问题:RuntimeError: test:tk.h version (8.4) doesn't match libtk.a version (8.5)
Traceback (most recent call last): File "/root/CodeWorkPace/test/TCPClient.py", line 20, i ...
- iOS开发之instancetype
instancetype和id使用方法类似,但他们还有不同点: (1)instancetype在类型表示上,跟id一样,可以表示任何对象类型 (2)instancetype只能用在返回值类型上,不能像 ...
- MyBatis原始dao开发及问题总结(五)
一.MyBatis原始Dao开发方式 1.原始dao开发需要程序员编写dao接口和dao接口实现类 编写UserDao接口:UserDao.java package codeRose.dao; pub ...
- oracle备份脚本
利用EXP导出全库,必须用SYSTEM或者DBA用户来导出. 具体脚本实现如下 全库导出(fullbackup): #!/bin/bash bname=`date +%Y%m%d` cd /backu ...
- 20155214 2016-2017-2 《Java程序设计》第5周学习总结
20155214 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 1.错误会被包装为可抛出的对象,继承自java.lang.Throwable类. 2.可以利 ...