kafka producer java API的实现

1.开发环境的构建

使用IDEA+MAVEN构建开发环境,可以采用Maven中scala-archetype-simple模板。

2.pom.xml的配置

这里scala版本为2.11.8,kafka版本为0.9.0.0


    2.11.8
    0.9.0.0
  

  
    
      org.scala-lang
      scala-library
      ${scala.version}
    
    
      org.apache.kafka
      kafka_2.11
      ${kafka.version}
    

  
3.Kafka配置类KafkaProperties

public static final String ZK = "192.168.254.128:2181";

public static final String TOPIC = "hello_topic";

public static final String BROKER_LIST = "192.168.254.128:9092";
4.生产者类

import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;

import java.util.Properties;

/**
 * Kafka生产者
 */
public class KafkaProducer extends Thread{
    private String topic;

    private Producer producer;

    public KafkaProducer(String topic){
        this.topic = topic;

        Properties properties = new Properties();

        properties.put("metadata.broker.list",KafkaProperties.BROKER_LIST);
        properties.put("serializer.class","kafka.serializer.StringEncoder");
        properties.put("request.required.acks","1");

        producer = new Producer(new ProducerConfig(properties));
    }

    @Override
    public void run() {
        int messageNo = 1;
        while (true){
            String message = "message_"+ messageNo;
            producer.send(new KeyedMessage(topic,message));
            System.out.println("Send:" + message);

            messageNo++;

            try {
                Thread.sleep(2000);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
5.测试类

public class KafkaClientsApp {
    public static void main(String[] args) {
        new KafkaProducer(KafkaProperties.TOPIC).start();
    }
}
6.测试结果

测试之前要开启zookeeper,kafka,和kafka consumer

本地生产的数据被服务器上的消费者消费

kafka producer java API的实现_第1张图片kafka producer java API的实现_第2张图片

你可能感兴趣的:(Kafka)