java--kafka编程实例--producer和consumer

来源:互联网 发布:淘宝手机店铺怎么装修 编辑:程序博客网 时间:2024/05/23 20:21


先修改broker的配置文件:

vim config/server.properties找到Socket Server Settings内容修改listeners的内容即可


producer代码如下:

import org.apache.kafka.clients.producer.Callback;import org.apache.kafka.clients.producer.KafkaProducer;import org.apache.kafka.clients.producer.ProducerRecord;import org.apache.kafka.clients.producer.RecordMetadata;import java.util.Properties;import java.util.concurrent.ExecutionException;class KafkaProperties {    public static final String TOPIC = "chenxun";    public static final String KAFKA_SERVER_URL = "192.168.222.130";    public static final int KAFKA_SERVER_PORT = 9092;    public static final int KAFKA_PRODUCER_BUFFER_SIZE = 64 * 1024;    public static final int CONNECTION_TIMEOUT = 100000;    public static final String TOPIC2 = "topic2";    public static final String TOPIC3 = "topic3";    public static final String CLIENT_ID = "SimpleConsumerDemoClient";    private KafkaProperties() {}}class Producer extends Thread {    private final KafkaProducer<Integer, String> producer;    private final String topic;    private final Boolean isAsync;    public Producer(String topic, Boolean isAsync) {        Properties props = new Properties();        props.put("bootstrap.servers", KafkaProperties.KAFKA_SERVER_URL + ":" + KafkaProperties.KAFKA_SERVER_PORT);        props.put("client.id", "DemoProducer");        props.put("key.serializer", "org.apache.kafka.common.serialization.IntegerSerializer");        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");        producer = new KafkaProducer<>(props);        this.topic = topic;        this.isAsync = isAsync;    }    public void run() {        int messageNo = 1;        while (true) {            String messageStr = "Message_" + messageNo;            long startTime = System.currentTimeMillis();            if (isAsync) { // Send asynchronously                producer.send(new ProducerRecord<>(topic,                        messageNo,                        messageStr), new DemoCallBack(startTime, messageNo, messageStr));            } else { // Send synchronously                try {                    producer.send(new ProducerRecord<>(topic,                            messageNo,                            messageStr)).get();                    System.out.println("Sent message: (" + messageNo + ", " + messageStr + ")");                } catch (InterruptedException | ExecutionException e) {                    e.printStackTrace();                }            }            ++messageNo;        }    }}class DemoCallBack implements Callback {    private final long startTime;    private final int key;    private final String message;    public DemoCallBack(long startTime, int key, String message) {        this.startTime = startTime;        this.key = key;        this.message = message;    }    /**     * A callback method the user can implement to provide asynchronous handling of request completion. This method will     * be called when the record sent to the server has been acknowledged. Exactly one of the arguments will be     * non-null.     *     * @param metadata  The metadata for the record that was sent (i.e. the partition and offset). Null if an error     *                  occurred.     * @param exception The exception thrown during processing of this record. Null if no error occurred.     */    public void onCompletion(RecordMetadata metadata, Exception exception) {        long elapsedTime = System.currentTimeMillis() - startTime;        if (metadata != null) {            System.out.println(                    "message(" + key + ", " + message + ") sent to partition(" + metadata.partition() +                            "), " +                            "offset(" + metadata.offset() + ") in " + elapsedTime + " ms");        } else {            exception.printStackTrace();        }    }}public class hello {    public static void main(String[] args) {        boolean isAsync = args.length == 0 || !args[0].trim().equalsIgnoreCase("sync");        boolean flag = true;        Producer producerThread = new Producer(KafkaProperties.TOPIC, flag);        producerThread.start();        //Consumer consumerThread = new Consumer(KafkaProperties.TOPIC);       // consumerThread.start();    }}

consumer:

import kafka.utils.ShutdownableThread;import org.apache.kafka.clients.consumer.ConsumerConfig;import org.apache.kafka.clients.consumer.ConsumerRecord;import org.apache.kafka.clients.consumer.ConsumerRecords;import org.apache.kafka.clients.consumer.KafkaConsumer;import java.util.Collections;import java.util.Properties;class KafkaProperties {    public static final String TOPIC = "chenxun";    public static final String KAFKA_SERVER_URL = "192.168.222.130";    public static final int KAFKA_SERVER_PORT = 9092;    public static final int KAFKA_PRODUCER_BUFFER_SIZE = 64 * 1024;    public static final int CONNECTION_TIMEOUT = 100000;    public static final String TOPIC2 = "topic2";    public static final String TOPIC3 = "topic3";    public static final String CLIENT_ID = "SimpleConsumerDemoClient";    private KafkaProperties() {}}public class Consumer extends ShutdownableThread {    private final KafkaConsumer<Integer, String> consumer;    private final String topic;    public Consumer(String topic) {        super("KafkaConsumerExample", false);        Properties props = new Properties();        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, KafkaProperties.KAFKA_SERVER_URL + ":" + KafkaProperties.KAFKA_SERVER_PORT);        props.put(ConsumerConfig.GROUP_ID_CONFIG, "DemoConsumer");        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "true");        props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "1000");        props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "30000");        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.IntegerDeserializer");        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");        consumer = new KafkaConsumer<>(props);        this.topic = topic;    }    @Override    public void doWork() {        consumer.subscribe(Collections.singletonList(this.topic));        ConsumerRecords<Integer, String> records = consumer.poll(1000);        for (ConsumerRecord<Integer, String> record : records) {            System.out.println("Received message: (" + record.key() + ", " + record.value() + ") at offset " + record.offset());        }    }    @Override    public String name() {        return null;    }    @Override    public boolean isInterruptible() {        return false;    }}


 
原创粉丝点击