mac 运行 kafka

mac 运行 kafka

安装

brew install kafka

启动 broker

手动启动 kafka, 同时启动一个使用 2181 端口的 zookeeper

/opt/homebrew/opt/kafka/bin/zookeeper-server-start /opt/homebrew/etc/kafka/zookeeper.properties
/opt/homebrew/opt/kafka/bin/kafka-server-start /opt/homebrew/etc/kafka/server.properties

启动生产者

/opt/homebrew/opt/kafka/bin/kafka-console-producer --topic test --bootstrap-server localhost:9092 

启动消费者

/opt/homebrew/opt/kafka/bin/kafka-console-consumer --topic test --bootstrap-server localhost:9092  --from-beginning

创建 topic

kafka-topics --create  --replication-factor 1 --partitions 5 --topic myTopic --bootstrap-server localhost:9092

查看 topic 列表

/opt/homebrew/opt/kafka/bin/kafka-topics --bootstrap-server localhost:9092   --list

数据文件目录

/opt/homebrew/var/lib/kafka-logs/

简易 go 程序

package main

import (
	"fmt"

	"github.com/confluentinc/confluent-kafka-go/v2/kafka"
)

func main() {

	p, err := kafka.NewProducer(&kafka.ConfigMap{"bootstrap.servers": "localhost"})
	if err != nil {
		panic(err)
	}

	defer p.Close()

	// Delivery report handler for produced messages
	go func() {
		for e := range p.Events() {
			switch ev := e.(type) {
			case *kafka.Message:
				if ev.TopicPartition.Error != nil {
					fmt.Printf("Delivery failed: %v\n", ev.TopicPartition)
				} else {
					fmt.Printf("Delivered message to %v\n", ev.TopicPartition)
				}
			}
		}
	}()

	// Produce messages to topic (asynchronously)
	topic := "myTopic"
	for _, word := range []string{"Welcome", "to", "the", "Confluent", "Kafka", "Golang", "client"} {
		p.Produce(&kafka.Message{
			TopicPartition: kafka.TopicPartition{Topic: &topic, Partition: kafka.PartitionAny},
			Value:          []byte(word),
		}, nil)
	}

	// Wait for message deliveries before shutting down
	p.Flush(15 * 1000)
}

《Kafka 权威指南》章节列表

《kafka 权威指南》
章节列表:
第 1 章 初识 kafka
第 2 章 安装 Kafka
第 3 章 Kafka 生产者——向 Kafka 写入数据
第 4 章 Kafka 消费者——从 Kafka 读取数据
第 5 章 深入 Kafka
第 6 章 可靠的数据传递
第 7 章 构建数据管道
第 8 章 跨集群数据镜像
第 9 章 管理 Kafka
第 10 章 监控 Kafka
第 11 章 流式处理

Reference

mac环境下使用brew安装Kafka(详细过程)_brew kafka-CSDN博客

Kafka整体架构、工作流程与文件存储机制 - 细雨骑驴入剑门 - 博客园

扫盲Kafka?看这一篇就够了! - 京东云技术团队 - 博客园

Kafka元数据缓存(metadata cache) - huxihx - 博客园


本文总阅读量