Kafka学习之:mac 上安装 kafka

这篇具有很好参考价值的文章主要介绍了Kafka学习之:mac 上安装 kafka。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Brew 安装

  • 打开终端

1. xcode-select --install

  • 首先要安装这个东西,否则安不了 kafka
    xcode-select --install
    
  • 安装完成后,你可能需要同意Xcode和相关工具的许可协议。这可以通过以下命令完成:
    sudo xcodebuild -license
    

2. brew install kafka

  • 安装 kafka
    brew install kafka
    

Docker 安装

  • 如果你已经安装了Docker,可以使用Docker来运行Kafka和ZooKeeper。

1. 构建 docker-compose.yml

  • 在终端中创建一个docker-compose.yml文件,内容如下:
version: '2'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"
  kafka:
    image: wurstmeister/kafka
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

docker 文件解析

zookeeper
  • 首先通过 services 定义了容器使用的所有服务,由于 kafka 启动需要借助于 zookeeper,所以我们要同时在 docker 容器中具备 kafkazookeeper 两种服务
  zookeeper:
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"
  • zookeeper: 这是服务的名称,在这个配置中表示 Zookeeper 服务。
  • image: 使用的Docker镜像,此处是 wurstmeister/zookeeper。这是一个预配置的 Zookeeper 镜像,适合与Kafka协同工作。
  • ports:"2181:2181" 将容器内的 2181 端口映射到宿主机的 2181 端口。Zookeeper 默认监听 2181 端口,用于 kafka 客户端连接。
kafka
  kafka:
    image: wurstmeister/kafka
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
  • kafka 这是另一个服务,表示Kafka服务器。
  • image 使用的是 wurstmeister/kafka 镜像,这是一个预配置的Kafka镜像。
  • ports: "9092:9092" 将容器内的9092端口映射到宿主机的9092端口。Kafka默认监听9092端口,用于客户端连接。
  • environment 设置环境变量来配置Kafka。
  • KAFKA_ADVERTISED_HOST_NAME 告知Kafka如何将自己广告给客户端。在这里设置为127.0.0.1,意味着Kafka将只能从宿主机本地访问。
  • KAFKA_ZOOKEEPER_CONNECT 指定Kafka如何连接到Zookeeper。zookeeper:2181 表示使用此Docker Compose配置中定义的zookeeper服务及其2181端口。
  • volumes: /var/run/docker.sock:/var/run/docker.sock 将宿主机的Docker套接字文件挂载到容器中。这允许Kafka容器管理其他Docker容器,通常用于动态创建和管理Kafka集群的Broker。

2. 通过 docker 构建容器并对外提供服务

  • 选择适合你的需求的安装方法。安装完成后,你就可以开始使用Kafka进行消息队列的开发和测试了。
docker-compose up

MAC 本机 Kafka 启动(使用 默认设置)

  • 首先启动 zookeeper
brew services start zookeeper
  • 如果是本地安装的 kafka,则直接可以用下列命令启动,默认情况下Kafka会监听在9092端口上。这个默认行为是由Kafka的配置文件决定的,该配置文件通常位于/usr/local/etc/kafka/server.properties(如果你是通过Homebrew安装的Kafka)而我是 M1 芯片的 macbook,我的默认安装地址是 /opt/homebrew/etc/kafka/server.properties
brew services start kafka
  • 如果更改了 server.properties 则需要重新运行 kafka 来生效设置
brew services restart kafka

检测 kafka 是否正确启动

方法1: 使用lsof命令

lsof -i :9092
  • 这个命令会列出所有监听在9092端口的进程。如果Kafka已经启动并且在此端口上监听,你应该能看到它的进程信息。如果这个命令返回了关于Kafka进程的信息,那么就意味着Kafka已经在9092端口上正确启动了。

方法 2:使用Kafka命令行工具

  • 执行以下命令来创建一个新主题:
kafka-topics --create --topic test --partitions 1 --replication-factor 1 --bootstrap-server localhost:9092
  • 如果这个命令成功执行并没有报错,那么就意味着Kafka服务已经在监听9092端口并且可以正常工作。 如果出现错误,错误信息可能会提供为何无法连接到Kafka的线索。

我在运行上述内容的时候创建失败,原因是我没有事先启动 zookeeper,错误信息如下:

[2024-03-29 21:54:03,707] WARN [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Node may not be available. (org.apache.kafka.clients.NetworkClient)
[2024-03-29 21:54:03,811] WARN [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Node may not be available. (org.apache.kafka.clients.NetworkClient)
[2024-03-29 21:54:03,913] WARN [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Node may not be available. (org.apache.kafka.clients.NetworkClient)
[2024-03-29 21:54:04,117] WARN [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Node may not be available. (org.apache.kafka.clients.NetworkClient)
[2024-03-29 21:54:04,522] WARN [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Node may not be available. (org.apache.kafka.clients.NetworkClient)
  • 然后查看了日志 /opt/homebrew/var/log/kafka/kafka_output.log 内容:
java.net.ConnectException: Connection refused
        at java.base/sun.nio.ch.Net.pollConnect(Native Method)
        at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:682)
        at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:973)
        at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:344)
        at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1289)
[2024-03-29 21:26:32,648] INFO Opening socket connection to server localhost/[0:0:0:0:0:0:0:1]:2181. (org.apache.zookeeper.ClientCnxn)
[2024-03-29 21:26:32,650] WARN Session 0x0 for server localhost/[0:0:0:0:0:0:0:1]:2181, Closing socket connection. Attempting reconnect except it is a SessionExpiredException. (org.apache.zookeeper.ClientCnxn)
java.net.ConnectException: Connection refused
        at java.base/sun.nio.ch.Net.pollConnect(Native Method)
        at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:682)
        at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:973)
        at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:344)
        at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1289)
[2024-03-29 21:26:33,220] INFO [ZooKeeperClient Kafka server] Closing. (kafka.zookeeper.ZooKeeperClient)
  • 可以看到是没有启动 zookeeper,所以手动启动一下:
brew services start zookeeper
  • 再重新启动一下 kafka
brew services restart kafka
  • 然后就可以成功运行了,再次测试创建 topic 就成功了

server.properties 一览

  • 这里展示一下整体的 kafka 的 property 文档,让大家可以看清楚一些:
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.

#
# This configuration file is intended for use in ZK-based mode, where Apache ZooKeeper is required.
# See kafka.server.KafkaConfig for additional details and defaults
#

############################# Server Basics #############################

# The id of the broker. This must be set to a unique integer for each broker.
broker.id=0

############################# Socket Server Settings #############################

# The address the socket server listens on. If not configured, the host name will be equal to the value of
# java.net.InetAddress.getCanonicalHostName(), with PLAINTEXT listener name, and port 9092.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
#listeners=PLAINTEXT://:9092

# Listener name, hostname and port the broker will advertise to clients.
# If not set, it uses the value for "listeners".
#advertised.listeners=PLAINTEXT://your.host.name:9092

# Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details
#listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL

# The number of threads that the server uses for receiving requests from the network and sending responses to the network
num.network.threads=3

# The number of threads that the server uses for processing requests, which may include disk I/O
num.io.threads=8

# The send buffer (SO_SNDBUF) used by the socket server
socket.send.buffer.bytes=102400

# The receive buffer (SO_RCVBUF) used by the socket server
socket.receive.buffer.bytes=102400

# The maximum size of a request that the socket server will accept (protection against OOM)
socket.request.max.bytes=104857600


############################# Log Basics #############################

# A comma separated list of directories under which to store log files
log.dirs=/opt/homebrew/var/lib/kafka-logs

# The default number of log partitions per topic. More partitions allow greater
# parallelism for consumption, but this will also result in more files across
# the brokers.
num.partitions=1

# The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
# This value is recommended to be increased for installations with data dirs located in RAID array.
num.recovery.threads.per.data.dir=1

############################# Internal Topic Settings  #############################
# The replication factor for the group metadata internal topics "__consumer_offsets" and "__transaction_state"
# For anything other than development testing, a value greater than 1 is recommended to ensure availability such as 3.
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1

############################# Log Flush Policy #############################

# Messages are immediately written to the filesystem but by default we only fsync() to sync
# the OS cache lazily. The following configurations control the flush of data to disk.
# There are a few important trade-offs here:
#    1. Durability: Unflushed data may be lost if you are not using replication.
#    2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush.
#    3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to excessive seeks.
# The settings below allow one to configure the flush policy to flush data after a period of time or
# every N messages (or both). This can be done globally and overridden on a per-topic basis.

# The number of messages to accept before forcing a flush of data to disk
#log.flush.interval.messages=10000

# The maximum amount of time a message can sit in a log before we force a flush
#log.flush.interval.ms=1000

############################# Log Retention Policy #############################

# The following configurations control the disposal of log segments. The policy can
# be set to delete segments after a period of time, or after a given size has accumulated.
# A segment will be deleted whenever *either* of these criteria are met. Deletion always happens
# from the end of the log.

# The minimum age of a log file to be eligible for deletion due to age
log.retention.hours=168

# A size-based retention policy for logs. Segments are pruned from the log unless the remaining
# segments drop below log.retention.bytes. Functions independently of log.retention.hours.
#log.retention.bytes=1073741824

# The maximum size of a log segment file. When this size is reached a new log segment will be created.
#log.segment.bytes=1073741824

# The interval at which log segments are checked to see if they can be deleted according
# to the retention policies
log.retention.check.interval.ms=300000

############################# Zookeeper #############################

# Zookeeper connection string (see zookeeper docs for details).
# This is a comma separated host:port pairs, each corresponding to a zk
# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
# You can also append an optional chroot string to the urls to specify the
# root directory for all kafka znodes.
zookeeper.connect=localhost:2181

# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=18000


############################# Group Coordinator Settings #############################

# The following configuration specifies the time, in milliseconds, that the GroupCoordinator will delay the initial consumer rebalance.
# The rebalance will be further delayed by the value of group.initial.rebalance.delay.ms as new members join the group, up to a maximum of max.poll.interval.ms.
# The default value for this is 3 seconds.
# We override this to 0 here as it makes for a better out-of-the-box experience for development and testing.
# However, in production environments the default value of 3 seconds is more suitable as this will help to avoid unnecessary, and potentially expensive, rebalances during application startup.
group.initial.rebalance.delay.ms=0

  • 其中最重要的是下面几个(一般需要改动的):

    • broker.id,如果你设置了一个运算集群,其中有多个 kafka 的节点,那么每个节点都是一个 broker,而每个 broker 的 id 都不能相同
    • #listeners=PLAINTEXT://:9092 这个一般是注释掉的(当默认只有一个节点的时候就默认使用 9092 端口启动这个 broker)但是如果有多个 broker 的时候,请把这里进行修改,使得每个 broker 的监听端口不同,修改的方式就是去掉注释变成 listeners=PLAINTEXT://:xxxx 其中 xxxx 是你自己想用的端口号
    • log.dirs=/opt/homebrew/var/lib/kafka-logs kafka 会默认将日志文件放到这个位置
    • zookeeper.connect=localhost:2181 这个是连接 zookeeper 的端口,也要配置正确
  • 还有更多设置的意义,如果大家想深入了解 kafka,建议去 这个网址 去看视频文章来源地址https://www.toymoban.com/news/detail-856250.html

到了这里,关于Kafka学习之:mac 上安装 kafka的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Kafka基本概念及其配置--docker desktop下的配置(尚未更新完成)

    Apache Kafka 是一款开源分布式流处理平台。可以用来发布和订阅数据以及对数据进行实时或者离线处理。 高吞吐量、低延迟 :kafka每秒可以处理几十万条消息,它的延迟最低只有几毫秒,每个topic可以分多个partition, consumer group 对partition进行consume操作; 扩展性强 :支持分布式

    2024年03月11日
    浏览(41)
  • Mac m1 docker安装 elasticsearch+kibana、zookeeper+kafka(不指定ip方式,验证无误)

    🌸 🌸 重点写在前面——注意docker本机地址请使用 host.docker.internal 🌸 🌸 由于 macOS 的 docker 底层实现的不同,主要原因是 macOS 的 docker 在容器和宿主之间无法通过 ip 直接通信。因此在安装的时候需要特殊注意与 ip 相关的设置, 当容器需要访问宿主ip时 ,需要使用 docker.f

    2024年02月11日
    浏览(57)
  • mac 安装kafka教程

     kafka使用命令“brew install kafka ”安装,可参考:mac kafka安装教程(自动安装)_北漂人Java的博客-CSDN博客 1、下载kafka安装包 Apache Download Mirrors Home page of The Apache Software Foundation https://www.apache.org/dyn/closer.cgi?path=/kafka/3.1.0/kafka_2.12-3.1.0.tgz 将压缩文件移动到自己的software  mv kafka_2

    2024年01月22日
    浏览(38)
  • Mac下Docker Desktop安装命令行工具、开启本地远程访问

    Mac系统下,为了方便在terminal和idea里使用docker,需要安装docker命令行工具,和开启Docker Desktop本地远程访问。 具体方法是在 设置-高级 下, 1.将勾选的User调整为System,这样不用手动配置PATH即可使用docker命令 2.勾选“Allow the default Docker socket to be used (requires password)”,开启Do

    2024年02月03日
    浏览(47)
  • Mac 安装ZooKeeper+kafka基本使用

    下面ZooKeeper基本介绍: 1、基本功能 ZooKeeper 为分布式系统提供了一种配置管理的服务:集中管理配置,即将全局配置信息保存在 ZooKeeper 服务中,方便进行修改和管理,省去了手动拷贝配置的过程,同时还保证了可靠和一致性。 2、 命名服务 在分布式系统中,经常需要对应用

    2024年02月09日
    浏览(34)
  • 【Kafka】Docker安装kafka&java kafka api

    docker依赖于zookeeper,首先安装zookeeper 1 拉取镜像 2 创建network 在启动之前,先指定一个网络 3 启动容器 启动zookeeper容器 测试是否成功 进入zookeeper 执行代码 1 拉取kafka镜像 2 启动kafka容器 进入kafka 3 创建topic -- 创建topic 查看topic -- 分区topic 4 创建生产者 -- 生产者 5 创建消费者

    2024年02月13日
    浏览(53)
  • 【Kafka】Kafka安装:Linux本地和Docker

    1、上传jdk-8u261-linux-x64.rpm到服务器并安装: 2、配置环境变量: 1、上传zookeeper-3.4.14.tar.gz到服务器 2、解压到/opt: 3、修改Zookeeper保存数据的目录,dataDir: 4、编辑/etc/profile: 设置环境变量ZOO_LOG_DIR,指定Zookeeper保存日志的位置; ZOOKEEPER_PREFIX指向Zookeeper的解压目录; 将Zook

    2024年02月22日
    浏览(36)
  • 【Kafka】Linux本地和Docker安装Kafka

    1、上传jdk-8u261-linux-x64.rpm到服务器并安装: 2、配置环境变量: 1、上传zookeeper-3.4.14.tar.gz到服务器 2、解压到/opt: 3、修改Zookeeper保存数据的目录,dataDir: 4、编辑/etc/profile: 设置环境变量ZOO_LOG_DIR,指定Zookeeper保存日志的位置; ZOOKEEPER_PREFIX指向Zookeeper的解压目录; 将Zook

    2024年01月21日
    浏览(42)
  • Mac安装Docker提示Another application changed your Desktop configuration解决方案

    Mac安装Docker后,提示Another application changed your Desktop configuration,Re-apply configurations无效 在终端执行下述命令即可解决: 参考方案链接:https://github.com/docker/for-mac/issues/6898

    2024年04月11日
    浏览(42)
  • Kafka(一)使用Docker Compose安装单机Kafka以及Kafka UI

    对于Kafka中经常用到的术语,可参考confluent的官方文档,这里不再赘述。 镜像选择Docker Hub上使用最多的bitnami Kafka,主要注意的点是环境变量和Kafka配置的映射关系 Additionally, any environment variable beginning with KAFKA_CFG_ will be mapped to its corresponding Apache Kafka key. For example, use KAFKA_CFG

    2024年02月08日
    浏览(56)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包