kafka_2.11-1.1.0生产者消费者

1.idea新建maven项目,导入配置文件
pom.xml



    4.0.0
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    7
                    7
                
            
        
    
    

    
        org.apache.kafka
        kafka_2.11
        1.1.0

        
            
                jmxtools
                com.sun.jdmk
            
            
                jmxri
                com.sun.jmx
            
            
                jms
                javax.jms
            
            
                org.apache.zookeeper
                zookeeper
            
            
                org.slf4j
                slf4j-log4j12
            
            
                org.slf4j
                slf4j-api
            
        
    
    
        org.apache.kafka
        kafka-clients
        1.1.0
    

    
        org.slf4j
        slf4j-simple
        1.7.25
    
    
        log4j
        log4j
        1.2.17
    



    cn.itcast
    KafakaApi
    1.0-SNAPSHOT



2.生产者:

package kafaka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

public class SimpleKafkaProducer {
    public static  void main(String[] args)
    {
        Properties props=new Properties();
        //broker地址
        props.put("bootstrap.servers", "mini1:9092,mini2:9092,mini3:9092");
        //请求的时候需要验证
        props.put("acks", "all");
        //请求失败需要重试jps

        props.put("retries", "0");
        //内存缓存区大小
        props.put("buffer.memory", 33554432);
        //指定消息key序列化方式
        props.put("key.serializer",
                "org.apache.kafka.common.serialization.StringSerializer");
        //指定消息本身的序列化方式
        props.put("value.serializer",
                "org.apache.kafka.common.serialization.StringSerializer");
        Producer producer = new KafkaProducer<>(props);
        for (int i = 0; i < 100; i++)
            producer.send(new ProducerRecord("test", Integer.toString(i), Integer.toString(i)));

        producer.close();
    }
}

3.消费者

package kafaka;

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;

public class SimpleKafkaConsumer {
    public static void main(String[] args) {

        Properties props=new Properties();

        props.put("bootstrap.servers", "mini1:9092,mini2:9092,mini3:9092");
        //每个消费者分配独立的组号
        props.put("group.id", "test");

        //如果value合法,则自动提交偏移量
        props.put("enable.auto.commit", "true");

        //设置多久一次更新被消费消息的偏移量
        props.put("auto.commit.interval.ms", "1000");

        //设置会话响应的时间,超过这个时间kafka可以选择放弃消费或者消费下一条消息
        props.put("session.timeout.ms", "30000");

        props.put("key.deserializer",
                "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer",
                "org.apache.kafka.common.serialization.StringDeserializer");

        KafkaConsumer  consumer = new KafkaConsumer<>(props);


        consumer.subscribe(Collections.singletonList("test"));

        System.out.println("Subscribed to topic " + "test");
        int i = 0;

        while (true) {
            ConsumerRecords records = consumer.poll(100);
            for (ConsumerRecord record : records)

                // print the offset,key and value for the consumer records.
                System.out.printf("offset = %d, key = %s, value = %s\n",
                        record.offset(), record.key(), record.value());
        }
    }
}

你可能感兴趣的:(kafka)