Maven
<dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients< /artifactid> <version>0.11.0.0</version> </dependency> <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-streams</artifactId> <version>0.11.0.0</version> </dependency>
Producer Producer
Import java.util.Properties; Import Org.apache.kafka.clients.producer.kafkaproducer;import Org.apache.kafka.clients.producer.ProducerRecord; public class Producerdemo {private Final kafkaproducer<string, string> producer; Public final static String TOPIC = "Test5"; Private Producerdemo () {Properties props = new Properties (); Props.put ("Bootstrap.servers", "xxx:9092,1xxx:9092,xxx:9092");//xxx server IP props.put ("ACKs", "all");// All follower respond only to the thought that the message was submitted successfully, namely "committed" Props.put ("retries", 0);//retries = MAX retries indefinitely until you realize that there is a problem:) Props.put ("b Atch.size ", 16384);//producer will attempt to batch the message records to reduce the number of requests. The default number of batched message bytes//batch.size When the batch data size reaches the set value, it is sent immediately, regardless of the following linger.ms Props.put ("linger.ms", 1);//delay 1ms send, this setting will be done by adding a small delay-that is, not immediately send a record, producer will wait for a given delay time to allow other message records to send, these message records can be batch processing p Rops.put ("Buffer.memory", 33554432);//producer can be used to cache the memory size of the data. Props.put ("Key.serializer", "Org.apache.kafka.common.serialization.IntegerSerializer"); Props.put ("Value.serializer", "Org.apache.kafka.common.serialization.StringSerializer"); Producer = new kafkaproducer<string, string> (props); } public void produce () {int messageno = 1; Final int COUNT = 5; while (Messageno < COUNT) {String key = string.valueof (Messageno); String data = String.Format ("Hello kafkaproducer message%s from Hubo 06291018", key); try {producer.send (new producerrecord<string, string> (TOPIC, data)); } catch (Exception e) {e.printstacktrace (); } messageno++; } producer.close (); public static void Main (string[] args) {new Producerdemo (). produce (); }}
Consumer Consumer
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 Userkafkaconsumer extends Thread {public static void main (string[] args) {Properties prop Erties = new Properties (); Properties.put ("Bootstrap.servers", "xxx:9092,xxx:9092,xxx:9092"),//xxx is the IP properties.put of the server cluster ("Group.id", "JD -group "); Properties.put ("Enable.auto.commit", "true"); Properties.put ("auto.commit.interval.ms", "1000"); Properties.put ("Auto.offset.reset", "latest"); 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 ("Test5")); while (true) {consumerrecords<string, string> records = Kafkaconsumer.poll (100); For (consumerrecord<string, string> record:records) {System.out.println ("-----------------"); System.out.printf ("offset =%d, value =%s", Record.offset (), Record.value ()); System.out.println (); } } }}
Kafka publish a subscription message