kafka的使用:
修改配置文件
zookeeper.properties文件server.properties文件 启动zookeeper启动kafka启动类:
配置kafka 启动消费者 kafka的使用: 修改配置文件 zookeeper.properties文件 server.properties文件 启动zookeeper
在kafka文件下启动命令窗口
输入:
.binwindowszookeeper-server-start.bat .configzookeeper.properties
启动kafka输入:
binwindowskafka-server-start.bat configserver.properties
启动类: 配置kafkapackage mainimport ( "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)}
启动消费者binwindowskafka-console-consumer.bat --bootstrap-server 127.0.0.1:9092 --topic shopping --from-beginning
shopping是Topic的值