初识kafka(kafka的使用)
作者:互联网
目录
kafka的使用:
修改配置文件
zookeeper.properties文件
server.properties文件
启动zookeeper
在kafka文件下启动命令窗口
输入:
.\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties
启动kafka
输入:
bin\windows\kafka-server-start.bat config\server.properties
启动类:
配置kafka
package main
import (
"fmt"
"github.com/Shopify/sarama"
)
func main() {
//1. 生产者配置
config := sarama.NewConfig()
config.Producer.RequiredAcks = sarama.WaitForAll //ACK
config.Producer.Partitioner = sarama.NewRandomPartitioner //分区
config.Producer.Return.Successes = true //确认
//2.连接kafka
client, err := sarama.NewSyncProducer([]string{"127.0.0.1:9092"}, config)
if err != nil {
fmt.Println("producer closed,err:", err)
return
}
defer client.Close()
//3. 封装消息
msg := &sarama.ProducerMessage{}
msg.Topic = "shopping"
msg.Value = sarama.StringEncoder("happy aaaa")
//4.发送消息
pid, offset, err := client.SendMessage(msg)
if err != nil {
fmt.Println("send msg failed,err:", err)
return
}
fmt.Printf("pid : %v offset : %v", pid, offset)
}
启动消费者
bin\windows\kafka-console-consumer.bat --bootstrap-server 127.0.0.1:9092 --topic shopping --from-beginning
shopping是Topic的值
标签:err,sarama,zookeeper,kafka,初识,使用,config,properties 来源: https://blog.csdn.net/qq_53267860/article/details/123075893