当前位置: 代码网 > it编程>操作系统>苹果Mac > Kafka学习之:mac 上安装 kafka

Kafka学习之:mac 上安装 kafka

2024年08月02日 苹果Mac 我要评论
kafka 在 mac 上的配置和安装

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
  zookeeper:
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"
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

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的线索。
[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,建议去 这个网址 去看视频

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com