java多线程写kafka的生产者和消费者

java多线程写kafka生产者

         //创建properties配置信息
        Properties prop = new Properties();
        //生产者的服务配置
         prop.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.30.144:9092");
         //配置序列化
        prop.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        prop.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class);
        //ack应答机制
        prop.put(ProducerConfig.ACKS_CONFIG,"1");
        //创建kafka的生产者对象
        KafkaProducer producer = new KafkaProducer<>(prop);
        for (int i = 0; i <1 ; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j <10000 ; j++) {
                        System.out.println(Thread.currentThread().getName()+" "+j);
                        ProducerRecord record = new ProducerRecord
                                ("topic1",Thread.currentThread().getName()+" "+j);
                        producer.send(record);
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        }

java多线程写kafka消费者

        for (int i = 0; i <1 ; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //设置properties
                    Properties prop = new Properties();
                    //消费者的信息
                    prop.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.30.151:9092");
                    prop.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
                    prop.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class);
                    //设置自动提交offset的位置
                    //配置自动提交offset 位置之后,我们不必关心消息消费到了什么位置,当程序重启后,消息也不会重复消费
                    prop.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"true");
                    //每隔多久自动提交一次
                    prop.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,"100");
                    //配置消费三种情况 earliest  latest  none
                    prop.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
                    //消费者组的名字
                    prop.put(ConsumerConfig.GROUP_ID_CONFIG,"g1");
                    //创建kafka消费者对象
                    KafkaConsumer consumer = new KafkaConsumer<>(prop);
                    //订阅消费的主题
                    consumer.subscribe(Collections.singleton("test1"));
                    long num=0;
                    while (true){
                        ConsumerRecords records = consumer.poll(Duration.ofMillis(100));
                        for (ConsumerRecord record:records){
                            num++;
                            System.out.println(num);
                            System.out.println(Thread.currentThread().getName()+"topic content"
                                    +record.offset()+" "+record.value()+" "+record.partition());
                        }
                    }
                }
            }).start();

        }

你可能感兴趣的:(java,kafka,开发语言)