Get source code of Kafka, the version is 2.3
git clone -b 2.3 https://github.com/apache/kafka.gitGenerate Jar File
./gradlew jar
Start the zookeeper
bin/zookeeper-server-start.sh config/zookeeper.properties
Since we are mainly focus on Kafka source code, we won't spend some time to dig into how the zookeeper start by using jars directly.
Start the Kafka Server
Option 1: start with script
bin
/kafka-server-start
.sh config
/server
.properties
Option 2:
If we inspect the real command from kafka-run-class.sh which is called by kafka-start-server.sh, here is the final command to run:
exec /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/bin/java -Xmx1G -Xms1G -server -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+ExplicitGCInvokesConcurrent -Djava.awt.headless=true -Xlog:gc*:file=/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../logs/kafkaServer-gc.log:time,tags:filecount=10,filesize=102400 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dkafka.logs.dir=/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../logs -Dlog4j.configuration=file:bin/../config/log4j.properties -cp /Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../core/build/dependant-libs-2.12.8/*:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../examples/build/libs/kafka-examples-2.3.1-SNAPSHOT-sources.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../examples/build/libs/kafka-examples-2.3.1-SNAPSHOT.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../clients/build/libs/kafka-clients-2.3.1-SNAPSHOT-sources.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../clients/build/libs/kafka-clients-2.3.1-SNAPSHOT.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../streams/build/libs/kafka-streams-2.3.1-SNAPSHOT-sources.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../streams/build/libs/kafka-streams-2.3.1-SNAPSHOT.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../streams/examples/build/libs/kafka-streams-examples-2.3.1-SNAPSHOT.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../streams/build/dependant-libs-2.12.8/rocksdbjni-5.18.3.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../tools/build/libs/kafka-tools-2.3.1-SNAPSHOT-sources.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../tools/build/libs/kafka-tools-2.3.1-SNAPSHOT.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../tools/build/dependant-libs-2.12.8/*:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../connect/api/build/libs/connect-api-2.3.1-SNAPSHOT-sources.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../connect/api/build/libs/connect-api-2.3.1-SNAPSHOT.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../connect/api/build/dependant-libs/*:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../connect/transforms/build/libs/connect-transforms-2.3.1-SNAPSHOT.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../connect/transforms/build/dependant-libs/*:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../connect/runtime/build/libs/connect-runtime-2.3.1-SNAPSHOT.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../connect/runtime/build/dependant-libs/*:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../connect/file/build/libs/connect-file-2.3.1-SNAPSHOT.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../connect/file/build/dependant-libs/*:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../connect/json/build/libs/connect-json-2.3.1-SNAPSHOT.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../connect/json/build/dependant-libs/*:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../connect/basic-auth-extension/build/libs/connect-basic-auth-extension-2.3.1-SNAPSHOT.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../connect/basic-auth-extension/build/dependant-libs/*:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../core/build/libs/kafka_2.12-2.3.1-SNAPSHOT-sources.jar:/Users/leizheng/vcs/github/kafka/kafka-2.3/bin/../core/build/libs/kafka_2.12-2.3.1-SNAPSHOT.jar kafka.Kafka config/server.properties
With the same setting above, we are able to run the Kafka directly from IntelliJ IDEA
Check the Server is running or not:
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
Congratulation, now we have the run Kafka from scratch.
Get the time to enjoy the adventure of Kafka.
Compare the source code between Spring and Kafka, the Kafka's code is more modern way by using stream interfaces and other features introduced by JDK8. And it will be good start entry point for java developer to learn how to write good code.
The another source code I will suggest is Netty which provide the deep inside about how to use JAVA NIO and good practice pattern to code at parrales.
Comments
Post a Comment