1.3 win下Kafka源码环境搭建

本文目的:1 搭建win下kafka调试环境,2,看kafka源码

1 本文环境

  1. win10专业版
  2. JDK 1.8
  3. scala 2.11.11
  4. gradle 4.8.1
  5. kafka 0.11.0.3-rc0 源码

2 前置软件准备

2.1 JDK

安装省略(如果有本地有多个jdk环境,可以参考这里进行配置切换),检查版本如下
在这里插入图片描述


2.2 scala

安装看这里,检查版本如下

当前社区编译 Kafka 支持 3 个 Scala 版本,分别是 2.11、2.12 和 2.13。2.11 应该很快就会不支持了,而 2.13 又是刚刚推出的版本,因此我推荐你安装 Scala 2.11 版本。

也可以先确定kafka版本,然后在kafka目录下的gradle.properties中看scalaVersion

在这里插入图片描述

2.3 gradle

安装看这里(注意最好设置好远程私服地址+本地仓库地址),检查版本如下

建议安装4.x版本的gradle,也可以先确定kafka版本,然后在build.gradle 里找到gradleVersion。即使该版本kafka建议对应的gradle版本

在这里插入图片描述


2.4 kafka源码

  1. 直接从官网下载,我下载的是kafka-1.0.0-src.tgz
  2. 从github上下载,然后切出自己想要的分支版本
//clone 到本地
>git clone https://github.com/bigData-lab/kafka.git
//因为我要1.0.0版本的kafka,所以先看tag或远程的版本
>git tag //查看标签
>git branch -r //查看远程分支名

>git checkout -b v0.11.0.3-rc0 0.11.0.3-rc0

3 win下编译kafka

3.1 scala版本检查

首先确保gradle.properties配置文件中的scalaVersion与安装的一致。gradle.properties配置文件的细节如下:

# 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.

group=org.apache.kafka
# NOTE: When you change this version number, you should also make sure to update
# the version numbers in tests/kafkatest/__init__.py and kafka-merge-pr.py.
version=1.0.0
scalaVersion=2.11.11
task=build
org.gradle.jvmargs=-XX:MaxPermSize=512m -Xmx1024m -Xss2m

3.2 构建idea工程

在kafka的目录下执行

Gradlew是包装器,自动下载包装器里定义好的gradle 版本,保证编译环境统一,gradle 是用本地的gradle版本。

// 1.因为我本地已经配置好了kafka所需的各种环境,所以直接gradle idea
// 2. 我之前已经gradle idea过了,中间会有很多下载耗时较长(分钟级别)。下图展示仅仅是为了演示步骤
>gradle idea

如下图
在这里插入图片描述


4 配置Kafka源码环境

前面几个步骤执行完成后就可以很舒适的阅读Kafka的源码,但是如果需要启动Kafka的服务还需要一些额外的步骤。

4.1 导入idea

  1. 安装scala插件(之前已经安装了scala,所以下图是update)
    在这里插入图片描述

  2. 配置gradle

在这里插入图片描述

  1. 导入编译后的kafka
    在这里插入图片描述

  2. 配置日志
    将config目录下的log4j.properties文件拷贝到core/src/main/scala目录下,这样可以让Kafka在运行时能够输出日志信息,可以参考下图:
    在这里插入图片描述
    修改修改log4j.properties,增加kafka日志路径kafka.logs.dir

kafka.logs.dir=D:\\idea-workspace\\github\\kafka\\logs
  1. 配置server.properties文件
# 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.

# 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. It will get the value returned from 
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
#listeners=PLAINTEXT://:9092

# Hostname and port the broker will advertise to producers and consumers. If not set, 
# it uses the value for "listeners" if configured.  Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
#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 seperated list of directories under which to store log files
log.dirs=D:\\idea-workspace\\github\\kafka\\logs\\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 for 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 exceessive 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=6000


############################# 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


# 是否允许topic被删除,设置为true则topic可以被删除,
# 开启这个功能方便Kafka在运行一段时间之后,能够删除一些不需要的临时topic
delete.topic.enable=true
# 禁用自动创建topic的功能
auto.create.topics.enable=false

漫长的构建过程

在这里插入图片描述

  1. 配置kafka启动参数

这里配置Main class为kafka.Kafka,并制定启动时所需要的配置文件地址,即:config/server.properties。配置JMX_PORT是为了方便搜集Kafka自身的Metrics数据

在这里插入图片描述

在这里插入图片描述


5 Zookeeper的安装、配置及启动

Kafka需要使用Zookeeper来管理元数据,比如记录topic、partitions(分区)以及replica(副本)的分配信息。

所以这里的Zookeeper的安装也以极简为主(详细安装见这里),即采用单机配置。Zookeeper下载地址为:http://zookeeper.apache.org/releases.html,下载之后解压,然后将conf/zoo_sample.cfg重命名为zoo.cfg,其中$ZOOKEEPER_HOME指的是ZooKeeper的根目录。

修改$ZOOKEEPER_HOME/conf/zoo.cfg配置,示例配置如下(其余配置可以不做修改):

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=D:\\ProgramFiles\\zookeeper-3.4.12\\data
dataLogDir=D:\\ProgramFiles\\zookeeper-3.4.12\\logs
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

简要介绍 zoo.cfg 文件

  • tickTime:默认3000ms,作一种基本单元,用它的倍数来表示系统内部的时间间隔配置,比如
  • 2*tickTime 是客户端会话的超时时间
  • 1*tickTime 是客户端 zk 服器端的心跳时
  • dataDir:无默认配置,必配置;用于配置储快照文件的目录,如果没有配置 dataLogDir,那么事务日志也会储在目录
  • clientPort:zk 的运行端口,默认是 2181
    启动
Windows:直接双击 zkServer.cmd
Linux: zkServer.sh start

在这里插入图片描述

如果zk不是默认的,需要修改 kafka的config/server.poroperties 文件的zookeeper.connect属性


最后kafka启动成功

在这里插入图片描述


6 使用kafka

  1. 启动zk
  2. 从idea中启动kafka.scala (相当kafka的server)
  3. 创建topic
--create -topic waterWang-test --zookeeper localhost:2181 --partitions 2 --replication-factor 1

如下图填写配置
在这里插入图片描述
创建topic成功
在这里插入图片描述

相关推荐
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页