java連線kafka測試
阿新 • • 發佈:2019-01-22
①進入到kafka資料夾中修改配置檔案:vim config/server.properties
②啟動zookeeper:
bin/zookeeper-server-start.sh config/zookeeper.properties
埠2181是ZooKeeper的預設埠,可以通過編輯檔案config/zookeeper.properties 中的clientPort來修改監聽埠。
③ 啟動Kafka Broker
bin/kafka-server-start.sh config/server.properties
④ 建立一個Topic 名稱為HelloWorld
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 \ > --partitions 1 --topic HelloWorld
⑤校驗Topic是否建立成功
bin/kafka-topics.sh --list --zookeeper localhost:2181
⑥配置pom檔案:
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka --> <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> <version>0.11.0.0</version> </dependency>
⑦編寫一個producer檔案:
package com.winter.kafka; import java.util.Properties; import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.Producer; import org.apache.kafka.clients.producer.ProducerRecord; public class ProducerDemo { public static void main(String[] args){ Properties properties = new Properties(); properties.put("bootstrap.servers", "47.91.214.23:9092"); properties.put("acks", "all"); properties.put("retries", 0); properties.put("batch.size", 16384); properties.put("linger.ms", 1); properties.put("buffer.memory", 33554432); properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); Producer<String, String> producer = null; try { producer = new KafkaProducer<String, String>(properties); // producer.send(new ProducerRecord<String, String>("HelloWorld","aaaaaaaaaaaa")); for (int i = 0; i < 100; i++) { String msg = "This is Message " + i; producer.send(new ProducerRecord<String, String>("HelloWorld", msg)); System.out.println("Sent:" + msg); } } catch (Exception e) { e.printStackTrace(); } finally { producer.close(); } } }
⑧配置一個customer檔案:
package com.winter.kafka; import java.util.Arrays; import java.util.Properties; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.ConsumerRecords; import org.apache.kafka.clients.consumer.KafkaConsumer; public class ConsumerDemo { public static void main(String[] args) throws InterruptedException { Properties properties = new Properties(); properties.put("bootstrap.servers", "47.91.214.23:9092"); properties.put("group.id", "group-1"); properties.put("enable.auto.commit", "true"); properties.put("auto.commit.interval.ms", "1000"); properties.put("auto.offset.reset", "earliest"); properties.put("session.timeout.ms", "30000"); properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<>(properties); kafkaConsumer.subscribe(Arrays.asList("HelloWorld")); while (true) { ConsumerRecords<String, String> records = kafkaConsumer.poll(100); for (ConsumerRecord<String, String> record : records) { System.out.printf("offset = %d, value = %s", record.offset(), record.value()); System.out.println("=====================>"); } } } }
先動customer 檔案,然後啟動producer檔案,然後觀察控制檯: