简体   繁体   中英

Start Zookeeper and Kafka through java code

Yesterday I asked this question and I'm trying to follow some other answers I've found but they're leading me nowhere. I really can't understand how to properly set up Zookeeper and then Kafka server from code. What I did so far was this:


Properties prop = new Properties();

prop.setProperty("dataDir","C:\\kafka_2.12-2.2.0\\config\\zookeeper.properties");
prop.setProperty("bootstrap.servers", "localhost:2181");

QuorumPeerConfig quorumConfiguration = new QuorumPeerConfig();

try {
     quorumConfiguration.parseProperties(prop);
} catch(Exception e) {
   throw new RuntimeException(e);
}

ZooKeeperServerMain zookeeper = new ZooKeeperServerMain();

final ServerConfig configuration = new ServerConfig();

configuration.readFrom(quorumConfiguration);

            new Thread() {
                public void run() {
                    try {
                        zookeeper.runFromConfig(configuration);
                    } catch (IOException e) {

                    }
                }
            }.start();


             Properties props = new Properties();
             props.put("bootstrap.servers", "localhost:9092");
             props.put("acks", "all");
             props.put("retries", 0);
             props.put("batch.size", 16384);
             props.put("linger.ms", 1);
             props.put("buffer.memory", 33554432);
             props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
             props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

             KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props);
             for (int i = 0; i < 100; i++)
                 producer.send(new ProducerRecord<String, String>("info", Integer.toString(i), Integer.toString(i)));

producer.close();

I couldn't find anything more than this.

Using the *-server-start commands would be highly recommended... They setup the classpath correctly.

Otherwise, looks like you are trying to write just simple tests, and libraries exist already for Embedded Kafka. eg https://github.com/embeddedkafka/embedded-kafka or using Docker

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM