1. 先导入相关的Jar包
2.生产者端的代码
package demo1;
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
public class Producer implements Runnable {
private final KafkaProducer<String, String> producer;
private final String topic;
public Producer(String topicName) {
Properties props = new Properties();
props.put("bootstrap.servers", "192.168.0.103:9092");
props.put("acks", "all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("key.serializer", StringSerializer.class.getName());
props.put("value.serializer", StringSerializer.class.getName());
this.producer = new KafkaProducer<String, String>(props);
this.topic = topicName;
}
@Override
public void run() {
int messageNo = 1;
System.out.println("开始生产数据");
try {
for(;;) {
String messageStr="你好,这是第"+messageNo+"条数据";
System.out.println(messageStr);
producer.send(new ProducerRecord<String, String>(topic, "Message", messageStr));
System.out.println("******");
//生产了100条就打印
if(messageNo%100==0){
System.out.println("发送的信息:" + messageStr);
}
//生产1000条就退出
if(messageNo%1000==0){
System.out.println("成功发送了"+messageNo+"条");
break;
}
messageNo++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
producer.close();
}
}
public static void main(String args[]) {
Producer test = new Producer("KAFKA_TEST");
Thread thread = new Thread(test);
thread.start();
}
}
3. 消费者端的代码
package demo1;
import java.util.Arrays;
import java.util.Properties;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
public class Consumer implements Runnable {
private final KafkaConsumer<String, String> consumer;
private ConsumerRecords<String, String> msgList;
private final String topic;
private static final String GROUPID = "groupA";
public Consumer(String topicName) {
Properties props = new Properties();
props.put("bootstrap.servers", "192.168.0.103:9092");
props.put("group.id", GROUPID);
props.put("enable.auto.commit", "true");
props.put("auto.commit.interval.ms", "1000");
props.put("session.timeout.ms", "30000");
props.put("auto.offset.reset", "earliest");
props.put("max.poll.records", 20);//设置最新拉取数据条数
props.put("key.deserializer", StringDeserializer.class.getName());
props.put("value.deserializer", StringDeserializer.class.getName());
this.consumer = new KafkaConsumer<String, String>(props);
this.topic = topicName;
this.consumer.subscribe(Arrays.asList(topic));
}
@Override
public void run() {
int messageNo = 1;
System.out.println("---------开始消费---------");
try {
for (;;) {
msgList = consumer.poll(1000);//阻塞时间
if(null!=msgList&&msgList.count()>0){
for (ConsumerRecord<String, String> record : msgList) {
//消费100条就打印
if(messageNo%100==0){
System.out.println(messageNo+"=======receive: key = " + record.key() + ", value = " + record.value()+" offset==="+record.offset());
}
//当消费了1000条就退出
if(messageNo%1000==0){
break;
}
messageNo++;
}
}else{
Thread.sleep(1000);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
consumer.close();
}
}
public static void main(String args[]) {
Consumer test1 = new Consumer("KAFKA_TEST");
Thread thread1 = new Thread(test1);
thread1.start();
}
}
3. 在windows下面测试Kafka
对于没有linux系统的同学,可以在windows下面安装一个kafka,特别简单,并不需要单独安装zookeeper.下载安装包,直接可以启动。
主要注意下面几个bat文件
3.1 启动zookeeper
E:\soft\kafka_2.11-0.11.0.1\bin\windows\zookeeper-server-start.bat E:\soft\kafka_2.11-0.11.0.1\config\server.properties
3.2 启动Kafka服务
E:\soft\kafka_2.11-0.11.0.1\bin\windows\zookeeper-server-start.bat E:\soft\kafka_2.11-0.11.0.1\config\server.properties
3.3 查看主题
E:\soft\kafka_2.11-0.11.0.1\bin\windows\kafka-topics.bat --zookeeper localhost:2181 --list
3.4 运行程序
把上面的ip地址直接改成localhost即可