|
| 1 | + |
| 2 | +description = "Kafka Message Broker" |
| 3 | +group = 'com.emprovise.service' |
| 4 | +version = '0.0.1-SNAPSHOT' |
| 5 | + |
| 6 | +buildscript { |
| 7 | + ext { |
| 8 | + zookeeperVersion = '3.4.12' |
| 9 | + scalaVersion = '2.11' |
| 10 | + kafkaVersion = '1.1.0' |
| 11 | + } |
| 12 | + repositories { |
| 13 | + jcenter() |
| 14 | + } |
| 15 | + dependencies { |
| 16 | + classpath("de.undercouch:gradle-download-task:3.4.3") |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +apply plugin: 'de.undercouch.download' |
| 21 | +def pullCommonDir = new File(buildDir, 'repo') |
| 22 | +pullCommonDir.mkdirs() |
| 23 | + |
| 24 | +task downloadFiles(type: Download) { |
| 25 | + src([ |
| 26 | + "http://apache.mirrors.ionfish.org/zookeeper/zookeeper-${zookeeperVersion}/zookeeper-${zookeeperVersion}.tar.gz", |
| 27 | + "http://apache.mirrors.ionfish.org/kafka/${kafkaVersion}/kafka_${scalaVersion}-${kafkaVersion}.tgz" |
| 28 | + ]) |
| 29 | + |
| 30 | + dest "${buildDir}/repo" |
| 31 | + overwrite false |
| 32 | +} |
| 33 | + |
| 34 | +task extractArchives(dependsOn: downloadFiles) { |
| 35 | + doLast { |
| 36 | + copy { |
| 37 | + into new File(buildDir, "libs") |
| 38 | + pullCommonDir.eachFileRecurse { |
| 39 | + if (it.path.endsWith(".tar.gz") || it.path.endsWith(".tgz")) { |
| 40 | + from tarTree(it) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +task copyAndRename(dependsOn: extractArchives) { |
| 48 | + doLast { |
| 49 | + if (!file("${buildDir}/libs/zookeeper-${zookeeperVersion}/conf/zoo.cfg").exists()) { |
| 50 | + println "copying zoo.cfg file..." |
| 51 | + def buildDirPath = "${buildDir}".replace("\\", "/") |
| 52 | + |
| 53 | + copy { |
| 54 | + from "${buildDir}/libs/zookeeper-${zookeeperVersion}/conf/zoo_sample.cfg" |
| 55 | + into "${buildDir}/libs/zookeeper-${zookeeperVersion}/conf" |
| 56 | + filter { line -> line.replaceAll("dataDir=/tmp/zookeeper", "dataDir=${buildDirPath}/libs/zookeeper-${zookeeperVersion}/data") } |
| 57 | + rename { String fileName -> |
| 58 | + fileName.replace("zoo_sample.cfg", "zoo.cfg") |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + def kafkaConfigFile = new File("${buildDir}/libs/kafka_${scalaVersion}-${kafkaVersion}/config/server.properties") |
| 64 | + |
| 65 | + if (kafkaConfigFile.exists()) { |
| 66 | + println "modifying kafka config file..." |
| 67 | + def fileText = kafkaConfigFile.text |
| 68 | + kafkaConfigFile.withWriter { writer -> |
| 69 | + writer.print fileText.replace("log.dirs=/tmp/kafka-logs", "log.dirs=${buildDir}\\libs\\kafka_${scalaVersion}-${kafkaVersion}\\kafka-logs") |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +def KAFKA_HOME = "${buildDir}\\libs\\kafka_${scalaVersion}-${kafkaVersion}" |
| 76 | + |
| 77 | +task runZooKeeper(type: Exec, dependsOn: copyAndRename) { |
| 78 | + workingDir "${buildDir}/libs/zookeeper-${zookeeperVersion}/bin" |
| 79 | + commandLine 'cmd', '/c', 'zkServer' |
| 80 | +} |
| 81 | + |
| 82 | +task runKafka(type: Exec, dependsOn: copyAndRename) { |
| 83 | + workingDir "${KAFKA_HOME}\\bin\\windows" |
| 84 | + commandLine 'cmd', '/c', "kafka-server-start.bat ..\\..\\config\\server.properties" |
| 85 | +} |
| 86 | + |
| 87 | +task createZipkinTopic(type: Exec) { |
| 88 | + workingDir "${KAFKA_HOME}\\bin\\windows" |
| 89 | + commandLine 'cmd', '/c', "kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic zipkin" |
| 90 | +} |
| 91 | + |
| 92 | +task consumeZipkinTopic(type: Exec) { |
| 93 | + workingDir "${KAFKA_HOME}\\bin\\windows" |
| 94 | + commandLine 'cmd', '/c', "kafka-console-consumer.bat --zookeeper localhost:2181 --topic zipkin" |
| 95 | +} |
0 commit comments