【Kafka】Kafka-2.7 源码编译踩坑整体

【Kafka】Kafka-2.7 源码编译

一、环境准备

​ 本文是Kafka-2.7 源码编译,所需要的环境,JAVA-8, gradle-6.6,Scala-2.13.3,kafka-2.7.0-src

二、源码编译

1. gradle配置

需要在安装目录下配置gradle-6.6\init.d,创建文件init.gradle,(用于快速下载)

buildscript { 
	repositories { 
		maven { url 'https://maven.aliyun.com/repository/gradle-plugin' } 
		maven { url 'https://plugins.gradle.org/m2/' }
	}
}
allprojects {
    repositories {
        def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/public/'
        all { ArtifactRepository repo ->
            if(repo instanceof MavenArtifactRepository){
                def url = repo.url.toString()
                if (url.startsWith('https://repo1.maven.org/maven2')||url.startsWith('https://jcenter.bintray.com')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
                    remove repo
                }
            }
        }
        maven { 
			url ALIYUN_REPOSITORY_URL
		}
    }
}

2. idea配置

 

就可以使用gradle正常编译kafka源码

三、源码启动

1. server启动

可以查看启动脚本kafka-server-start.sh,找到启动类scala\kafka\Kafka.scala

配置如下:

 

2. topic demo

可以查看启动脚本kafka-topics.sh,找到启动类kafka.admin.TopicCommand

  创建topic:bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
  查看topic:bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test 
            bin/kafka-topics.sh --list --zookeeper localhost:2181
  删除topic:bin/kafka-topics.sh --delete --topic test --zookeeper localhost:2181
  分区扩容:bin/kafka-topics.sh -alter --partitions 3 --zookeeper localhost:2181 --topic test (暂时不支持减区)

 以topic为例,源码配置

  

以下是zk 的查到的topic情况

 

3. 发送消息

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test

4. 消费消息  默认消费最新消息:

  默认消费最新消息: bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test  
  消费之前消息: bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic test 
  消费多主题:bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --whitelist "test|test-2" 
  单播、多播消费: bin/kafka-console-consumer.sh --bootstrap-server localhost:9092  --consumer-property group.id=testGroup --topic test

5. 消费组

所有消费组:bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
消费偏移量:bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group testGroup
 可以在appplication中配置启动参数,也可以在启动类里边手动配置参数
    val param = "--list --zookeeper localhost:2181";
//    val param = "--create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test";
    val args1 = param.split(" ")

    val opts = new TopicCommandOptions(args1)

6. 修改配置文件

对所有topic起作用
log.retention.hours=72
log.cleanup.policy=delete

对指定topic起作用
./kafka-config.sh --zookeeper localhost:2181 --alter --entity-name mytopic --entity_type topics --add-config retention.ms=86400000


手动删除指定topic的过期数据
./kafka-topic.sh --zookeeper localhost:2181 --alter --topic  mytopic --config  cleanup.policy=delete


查看topic配置
./kafka-config --zookeeper localhsot:2181 --describle --entity-name mytopic --entity_type topics

 

四、启动报错问题整理

出现:Gradle sync failed:xxxxx 或 Could not resolve org.gradle:test-retry-gradle-plugin:1.1.6(xxxx)
原因:Gradle版本或设置问题

出现: What went wrong: Execution failed for task ‘:core:compileScala’. > Compila
原因:Scala版本问题

出现:java [options] KafkaServer server.properties [–override property=value]*
原因:需要配置启动参数 config\server.properties

出现:
Execution failed for task ‘:core:Kafka.main()’.
Build cancelled while executing task ‘:core:Kafka.main()’
Failed to load class org.slf4j.impl.StaticLoggerBinder

1.在core->main->新建resources->复制config下的log4j.properties
2.在build.gradle文件中查找到 project(’:core’) ,在 dependencies 中添加 如下:

compile group: 'org.slf4j', name: "slf4j-api", version: "1.7.25"
compile group: 'org.slf4j', name: "slf4j-log4j12", version: "1.7.25"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值