golang kafka client
针对golang的 kafka client 有很多开源package,例如sarama, confluent等等。在使用sarama 包时,高并发中偶尔遇到crash。于是改用confluent-kafka-go,其简单易用,并且表现稳定。
本文主要介绍confluent-kafka-go的使用方法。
confluent-kafka-go,是kafka官网推荐的golang package。
confluent-kafka-go is Confluent's Golang client for Apache Kafka and the Confluent Platform.
编译环境搭建
安装librdkafka
下载
$ git clone https://github.com/edenhill/librdkafka.git
$ cd librdkafka
配置、编译、安装
$ ./configure --prefix /usr
$ make
$ sudo make install
配置PKG_CONFIG_PATH
在文件~/.bashrc 末尾添加
export PKG_CONFIG_PATH=/usr/lib/pkgconfig
下载go client
$ go get -u github.com/confluentinc/confluent-kafka-go/kafka
自动下载到GOPATH目录下,也可到github上自行下载,然后放到GOPATH中。
Example
// Example function-based Apache Kafka producer
package main
/**
* Copyright 2016 Confluent Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import (
"fmt"
"github.com/confluentinc/confluent-kafka-go/kafka"
"os"
)
func main() {
if len(os.Args) != 3 {
fmt.Fprintf(os.Stderr, "Usage: %s <broker> <topic>\n",
os.Args[0])
os.Exit(1)
}
broker := os.Args[1]
topic := os.Args[2]
p, err := kafka.NewProducer(&kafka.ConfigMap{"bootstrap.servers": broker})
if err != nil {
fmt.Printf("Failed to create producer: %s\n", err)
os.Exit(1)
}
fmt.Printf("Created Producer %v\n", p)
// Optional delivery channel, if not specified the Producer object's
// .Events channel is used.
deliveryChan := make(chan kafka.Event)
value := "Hello Go!"
err = p.Produce(&kafka.Message{TopicPartition: kafka.TopicPartition{Topic: &topic, Partition: kafka.PartitionAny}, Value: []byte(value)}, deliveryChan)
e := <-deliveryChan
m := e.(*kafka.Message)
if m.TopicPartition.Error != nil {
fmt.Printf("Delivery failed: %v\n", m.TopicPartition.Error)
} else {
fmt.Printf("Delivered message to topic %s [%d] at offset %v\n",
*m.TopicPartition.Topic, m.TopicPartition.Partition, m.TopicPartition.Offset)
}
close(deliveryChan)
}
注意:
如果需要链接静态库,可删除/usr/lib/下面关于rdkafka的动态库文件(.so文件)。然后,go build编译时加上选项 –tags static
例如:
go build -tags static produer.go
更多example,可参考
https://github.com/confluentinc/confluent-kafka-go/tree/master/examples
参考
https://github.com/confluentinc/confluent-kafka-go
golang kafka client的更多相关文章
- Windbg调优Kafka.Client内存泄露
从来没写过Blog,想想也是,工作十多年了,搞过N多的架构.技术,不与大家分享实在是可惜了.另外,从传统地ERP行业转到互联网,也遇到了很所前所未有的问题,原来知道有一些坑,但是不知道坑太多太深.借着 ...
- golang kafka
golang kafka – hello world https://github.com/Shopify/sarama https://shopify.github.io/sarama/ con ...
- .net Kafka.Client多个Consumer Group对Topic消费不能完全覆盖研究总结(一)
我们知道Kafka支持Consumer Group的功能,但是最近在应用Consumer Group时发现了一个Topic 的Partition不能100%覆盖的问题. 程序部署后,发现Kafka在p ...
- .net Kafka.Client多个Consumer Group对Topic消费不能完全覆盖研究总结(二)
依据Partition和Consumer的Rebalance策略,找到Kafka.Client Rebalance代码块,还原本地环境,跟踪调试,发现自定义Consumer Group 的Consum ...
- Python Kafka Client 性能测试
一.前言 由于工作原因使用到了 Kafka,而现有的代码并不能满足性能需求,所以需要开发高效读写 Kafka 的工具,本文是一个 Python Kafka Client 的性能测试记录,通过本次测试, ...
- [Golang] kafka集群搭建和golang版生产者和消费者
一.kafka集群搭建 至于kafka是什么我都不多做介绍了,网上写的已经非常详尽了. 1. 下载zookeeper https://zookeeper.apache.org/releases.ht ...
- golang http.client 遇到了 Connection reset by peer 问题
最近一个 golang 写的 http.client 的,获取远程服务器数据,有时候会报错,尤其在数量很大的时候,老是收到 Connection reset by peer 这样的 提醒,都有点想用重 ...
- golang kafka clinet 内存泄露问题处理
go 内存泄露 新版本服务跑上一天内存占用20g,显然是内存泄露 内存泄露的问题难在定位 技术上的定位 主要靠 pprof 生成统计文件 之前写web项目 基于net/http/pprof 可以看到运 ...
- 【原创】kafka client源代码分析
该包下只有一个文件:ClientUtils.scala.它是一个object,里面封装了各种client(包括producer,consumer或admin)可能会用到的方法: 1. fetchTop ...
随机推荐
- 使用路径arc-七彩
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...
- memory prefix hypo,hecto,hyper out1
1● hypo 次等 2● hecto 许多,百 3● hyper 超过,许多
- app.jsNodejs启动测试服务
'use strict'; var express = require('express');var app = express('');var fs = require('fs'); app.get ...
- 014PHP文件处理——文件指针控制fseek rewind ftell feof fpassthru
<?php /** * 文件指针控制fseek rewind ftell feof fpassthru */ //feof()判断文件读取是否超出文件长度 /*$file = fopen('a. ...
- Flask初级(九)flash与前台交互get详解
Project name :Flask_Plan templates:templates static:static @app.route('/') def hello_world(): return ...
- Solrj调用Solr API
在MyEclipse下的SSH项目,要调用Solr接口进行操作. 1.先运行solr 2.在已搭建好的SSH项目中用Solrj调用Solr的接口 3.导入如下solr的jar包到搭建好的SSH项目的W ...
- Python3 urllib抓取指定URL的内容
最近在研究Python,熟悉了一些基本语法和模块的使用:现在打算研究一下Python爬虫.学习主要是通过别人的博客和自己下载的一下文档进行的,自己也写一下博客作为记录学习自己过程吧.Python代码写 ...
- L1-027 出租
下面是新浪微博上曾经很火的一张图: 一时间网上一片求救声,急问这个怎么破.其实这段代码很简单,index数组就是arr数组的下标,index[0]=2 对应 arr[2]=1,index[1]=0 对 ...
- python 爬虫系列教程方法总结及推荐
爬虫,是我学习的比较多的,也是比较了解的.打算写一个系列教程,网上搜罗一下,感觉别人写的已经很好了,我没必要重复造轮子了. 爬虫不过就是访问一个页面然后用一些匹配方式把自己需要的东西摘出来. 而访问页 ...
- 【笔记】《深入浅出MFC》第5章 总观Application Framework
凝聚性强.组织化强的类库就是Application Framework.一组合作无间的对象,彼此藉消息的流动而沟通,并且互相调用对方的函数以求完成任务,这就是Application Framework ...