一、环境要求
(项目源码在最下方有链接)
操作系统:win10
JDK:1.7以上
kafka版本:kafka_2.12-1.0.0
Kafka官网下载:下载
选择:kafka_2.12-1.0.0.tgz
zookeeper下载:下载
选择:zookeeper-3.4.11.tar.gz(二进制)
二、环境搭建
1.JDK环境(过程略)
2.zookeeper环境
与jdk环境配置类似
a.解压zookeeper压缩包,找到该项目下conf目录中的“zoo_sample.cfg”重 命名为“zoo.cfg”,我的是在E:\Java\Tool\zookeeper-3.4.11\zookeeper-3.4.11
b.在任意文本编辑器(如notepad++,记事本)中打开zoo.cfg,
c. 找到并编辑dataDir=e://Java//Tool//tmp
添加环境变量:
ZOOKEEPER_HOME
变量值 E:\Java\Tool\zookeeper-3.4.11\zookeeper-3.4.11
Path:在末尾添加%ZOOKEEPER_HOME%\bin
测试:打开cmd,输入zkServer(保持打开,以下还要用)
控制台能打印出端口号,说明配置成功
三、kafka安装与运行
1.解压kafka的压缩包,进入解压包目录,E:\Java\Tool\kafka_2.12-1.0.0\kafka_2.12-1.0.0,打开并修改config下的server.properties,修改log.dirs=E:/Java/tmp/kafka-log
2.在kafka安装目录即:E:\Java\Tool\kafka_2.12-1.0.0\kafka_2.12-1.0.0打开命令执行窗口,输入
.\bin\windows\kafka-server-start.bat.\config\server.properties
(保持打开,以下还要用)
日志中有个javax.management.MalformedObjectNameException: Invalid character ‘:’ in value part of property错误,可以忽略。
3.创建主题:
在kafka安装目录即:E:\Java\Tool\kafka_2.12-1.0.0\kafka_2.12-1.0.0打开命令执行窗口,输入
.\bin\windows\kafka-topics.bat --create --zookeeper localhost:2181--replication-factor 1 --partitions 1 --topic topic1
(最后键名为主题的名字)
(保持打开,以下还要用)
创建生产者
在kafka安装目录即:E:\Java\Tool\kafka_2.12-1.0.0\kafka_2.12-1.0.0打开命令执行窗口,输入
.\bin\windows\kafka-console-producer.bat--broker-list localhost:9092 --topic topic1
(最后键名为主题的名字)
(保持打开,以下还要用)
创建消费者
在kafka安装目录即:E:\Java\Tool\kafka_2.12-1.0.0\kafka_2.12-1.0.0打开命令执行窗口,输入
.\bin\windows\kafka-console-consumer.bat--zookeeper localhost:2181 --topic topic1
(最后键名为主题的名字)
在生产者中输入任何信息,消费者能接收即为部署成功。
总结步骤:
1.启动zookeeper服务
zkServer
2.启动kafka服务(kafka安装目录)
.\bin\windows\kafka-server-start.bat .\config\server.properties
3.创建主题(kafka安装目录)
.\bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic topic1
4.创建生产者(kafka安装目录)
.\bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic topic1
5.创建消费者(kafka安装目录)
.\bin\windows\kafka-console-consumer.bat --zookeeper localhost:2181 --topic topic1
四、JAVA项目整合实现
1.创建maven项目,修改pom文件
添加kafka的jar
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.11</artifactId>
<version>0.8.2.1</version>
</dependency>
2.创建一个类充当配置文件
package com.chenx.demo.conf;
public interface KafkaProperties {
final static String zkConnect = "127.0.0.1:2181";
final static String groupId = "group1";
final static String topic = "topic1";
final static String kafkaServerURL = "127.0.0.1";
final static int kafkaServerPort = 9092;
final static int kafkaProducerBufferSize = 64 * 1024;
final static int connectionTimeOut = 20000;
final static int reconnectInterval = 10000;
final static String topic2 = "topic2";
final static String topic3 = "topic3";
final static String clientId = "SimpleConsumerDemoClient";
}
3.创建生产者
package com.chenx.demo.producer;
import java.util.Properties;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
public class KafkaProducer extends Thread {
private final kafka.javaapi.producer.Producer<Integer, String> producer;
private final String topic;
private final Properties props = new Properties();
public KafkaProducer(String topic)
{
props.put("serializer.class", "kafka.serializer.StringEncoder");
props.put("metadata.broker.list", "127.0.0.1:9092");
producer = new kafka.javaapi.producer.Producer<Integer, String>(new ProducerConfig(props));
this.topic = topic;
}
@Override
public void run() {
int messageNo = 1;
while (true)
{
String messageStr = new String("helloWord->" + messageNo);
System.out.println("Send:" + messageStr);
producer.send(new KeyedMessage<Integer, String>(topic, messageStr));
// System.out.println("topic:"+topic);
messageNo++;
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
4.创建生产者
package com.chenx.demo.consumer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import com.chenx.demo.conf.KafkaProperties;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;
public class KafkaConsumer extends Thread {
private final ConsumerConnector consumer;
private final String topic;
public KafkaConsumer(String topic)
{
consumer = kafka.consumer.Consumer.createJavaConsumerConnector(
createConsumerConfig());
this.topic = topic;
}
private static ConsumerConfig createConsumerConfig()
{
Properties props = new Properties();
props.put("zookeeper.connect", KafkaProperties.zkConnect);
props.put("group.id", KafkaProperties.groupId);
props.put("zookeeper.session.timeout.ms", "40000");
props.put("zookeeper.sync.time.ms", "200");
props.put("auto.commit.interval.ms", "1000");
return new ConsumerConfig(props);
}
@Override
public void run() {
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put(topic, new Integer(1));
Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
KafkaStream<byte[], byte[]> stream = consumerMap.get(topic).get(0);
ConsumerIterator<byte[], byte[]> it = stream.iterator();
while (it.hasNext()) {
System.out.println("receive:" + new String(it.next().message()));
try {
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
5.创建客户端启动程序
package com.chenx.demo.client;
import com.chenx.demo.conf.KafkaProperties;
import com.chenx.demo.consumer.KafkaConsumer;
import com.chenx.demo.producer.KafkaProducer;
public class KafkaClient {
public static void main(String[] args) {
KafkaProducer producerThread = new KafkaProducer(KafkaProperties.topic);
producerThread.start();
KafkaConsumer consumerThread = new KafkaConsumer(KafkaProperties.topic);
consumerThread.start();
}
}
6.启动zookeeper和kafka服务
cmd输入zkServer,
在kafka安装目录即:E:\Java\Tool\kafka_2.12-1.0.0\kafka_2.12-1.0.0打开命令执行窗口,输入
.\bin\windows\kafka-server-start.bat.\config\server.properties
同上
运行KafkaClient类
控制台正常打印出内容
项目结构和最后结果图
为了能更直接的体现消费者和生产者模型,可以通过windows的控制台启动消费者,java项目不启动消费者。
下面修改代码,注解掉 consumerThread.start();
package com.chenx.demo.client;
import com.chenx.demo.conf.KafkaProperties;
import com.chenx.demo.consumer.KafkaConsumer;
import com.chenx.demo.producer.KafkaProducer;
public class KafkaClient {
public static void main(String[] args) {
KafkaProducer producerThread = new KafkaProducer(KafkaProperties.topic);
producerThread.start();
// KafkaConsumer consumerThread = new KafkaConsumer(KafkaProperties.topic);
// consumerThread.start();
}
}
启动消费者服务
在kafka安装目录即:E:\Java\Tool\kafka_2.12-1.0.0\kafka_2.12-1.0.0打开命令执行窗口,输入
.\bin\windows\kafka-console-producer.bat--broker-list localhost:9092 --topic topic1
项目源码
tip:关掉kafka服务,重启之后再创建主题topic1时会显示已存在的消息,这个时候可以直接跳过进入创建生产者的步骤,或者删除kafka日志目录下的东西、tmp/kafka-log,重启kafka服务,就能重新创建topic1,或者用其他方法删除topic,我还在学习。
欢迎在下方留言