生產者與消費者的Java實現

來源:互聯網
上載者:User

標籤:ali   trie   stat   pid   void   serve   刪除   java實現   org   

首先建立maven工程,需要引入的包:

<dependencies>    <dependency>        <groupId>org.apache.kafka</groupId>        <artifactId>kafka-clients</artifactId>        <version>0.10.2.1</version>    </dependency>    <dependency>          <groupId>org.apache.kafka</groupId>          <artifactId>kafka_2.10</artifactId>          <version>0.10.2.1</version>      </dependency>  </dependencies>

 

然後就可以實現生產者與消費者,在建立topic時,如果需要刪除已經存在的topic,則需要配置delete.topic.enable=true,否則無法刪除對應的topic。

/**
消費者
**/
public
class KafkaConsumerDemo{ private final KafkaConsumer<String, String> consumer; private KafkaConsumerDemo(){ Properties props = new Properties(); props.put("bootstrap.servers", "10.xxx.xxx.149:9092, 10.xxx.xxx.182:9092, 10:xxx.xxx.190:9092"); props.put("group.id", "test"); props.put("enable.auto.commit", "true"); props.put("auto.commit.interval.ms", "1000"); props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); consumer = new KafkaConsumer<String, String>(props); } void consume(){ consumer.subscribe(Arrays.asList(KafkaProducerDemo.TOPIC)); while (true) { ConsumerRecords<String, String> records = consumer.poll(100); for (ConsumerRecord<String, String> record : records) System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value()); } } public static void main(String[] args){ new KafkaConsumerDemo().consume(); }}
/** * 產生者 * */public class KafkaProducerDemo{    private final Producer<String, String> kafkaProducer;        public final static String TOPIC = "JAVA_TOPIC";        private KafkaProducerDemo()    {        kafkaProducer = createKafkaProducer();    }        private Producer<String, String> createKafkaProducer()    {        Properties props = new Properties();        props.put("bootstrap.servers", "10.185.156.149:9092, 10.185.156.182:9092, 10:185.156.190: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");                Producer<String, String> kafkaProducer = new KafkaProducer<String, String>(props);        return kafkaProducer;    }        void produce()    {        for (int i = 1; i < 1000; i++)        {            try            {                Thread.sleep(1000);            }            catch (InterruptedException e)            {                e.printStackTrace();            }            String key = String.valueOf("key" + i);            String data = "hello kafka message:" + key;            kafkaProducer.send(new ProducerRecord<>(TOPIC, key, data), new Callback()            {                @Override                public void onCompletion(RecordMetadata recordMetadata, Exception e)                {                    // do sth                }            });            System.out.println(data);        }    }        public static void main(String[] args)    {        KafkaCreateTopic.createTopic("JAVA_TOPIC", 3, 1);        new KafkaProducerDemo().produce();    }}
/**
建立topic
**/
public
class KafkaCreateTopic{ public static void createTopic(String topic, int partitions, int replicationFactor) { ZkUtils zkUtils = ZkUtils.apply("10.xxx.xxx.149:2181", 30000, 30000, JaasUtils.isZkSecurityEnabled()); if (AdminUtils.topicExists(zkUtils, topic)) { deleteTopic(zkUtils, topic); } AdminUtils.createTopic(zkUtils, topic, partitions, replicationFactor, new Properties(), RackAwareMode.Enforced$.MODULE$); zkUtils.close(); } public static void deleteTopic(ZkUtils zkUtils, String topic) { AdminUtils.deleteTopic(zkUtils, topic); System.out.println("delete the topic " + topic); }}

 

生產者與消費者的Java實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.