码迷,mamicode.com
首页 > 其他好文 > 详细

go中简单使用kafka

时间:2019-05-01 21:14:07      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:net   lock   连接   []   tmp   ret   配置   安装   github   

windows上kafka的安装

1.安装jdk

下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html

下载需要注册oracle

添加环境变量JAVA_HOME=C:\Program Files\Java\jre1.8.0_211

2.安装Zookeeper

下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/

①进入zookeeper的相关设置所在的文件目录,例如本文的:D:\zookeeper-3.4.14\conf

②将"zoo_sample.cfg"重命名为"zoo.cfg"

③打开zoo.cfg 找到并编辑:

dataDir=/tmp/zookeeper to D:/zookeeper-3.4.14/data或 D:/zookeeper-3.4.14/data(路径仅为示例,具体可根据需要配置)

④与配置jre类似,在系统环境变量中添加:

a.系统变量中添加ZOOKEEPER_HOME=D:\zookeeper-3.4.14

b.编辑系统变量中的path变量,增加%ZOOKEEPER_HOME%\bin

⑤在zoo.cfg文件中修改默认的Zookeeper端口(默认端口2181)

⑥打开cmd窗口,输入zkserver,运行Zookeeper

3.安装kafka

下载地址:https://www.apache.org/dyn/closer.cgi?path=/kafka/2.2.0/kafka_2.11-2.2.0.tgz

①进入kafka配置文件所在目录,D:\kafka_2.11-2.2.0\config

②编辑文件"server.properties",找到并编辑:

log.dirs=/tmp/kafka-logs to log.dirs=D:/kafka_2.11-2.2.0/kafka-logs

③在server.properties文件中,zookeeper.connect=localhost:2181代表kafka所连接的zookeeper所在的服务器IP以及端口,可根据需要更改

修改advertised.host.name=服务器ip

④进入kafka安装目录D:\kafka_2.11-2.2.0,打开cmd启动

.\bin\windows\kafka-server-start.bat .\config\server.properties

go运行代码

package main

import (
    "fmt"
    "time"
    "github.com/Shopify/sarama"
)

func main() {

    config := sarama.NewConfig()
    config.Producer.RequiredAcks = sarama.WaitForAll
    config.Producer.Partitioner = sarama.NewRandomPartitioner
    config.Producer.Return.Successes = true

    client, err := sarama.NewSyncProducer([]string{"192.168.3.118:9092"}, config)
    if err != nil {
        fmt.Println("producer close, err:", err)
        return
    }

    defer client.Close()
    for {
        msg := &sarama.ProducerMessage{}
        msg.Topic = "nginx_log"
        msg.Value = sarama.StringEncoder("this is a good test, my message is good")

        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)
        time.Sleep(10 * time.Millisecond)
    }
}

 

go中简单使用kafka

标签:net   lock   连接   []   tmp   ret   配置   安装   github   

原文地址:https://www.cnblogs.com/angelyan/p/10800739.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!