1.安装zookeeper
Kafka的运行依赖于Zookeeper,所以在运行Kafka之前我们需要安装并运行Zookeeper
1.1 下载安装文件: http://mirror.bit.edu.cn/apache/zookeeper/
1.2 解压文件(本文解压到 D:\zookeeper-3.4.8)
1.3 打开D:\zookeeper-3.4.8\conf,把zoo_sample.cfg重命名成zoo.cfg
1.4 从文本编辑器里打开zoo.cfg
1.5 修改dataDir和dataLogDir保存路径
dataDir=D:\data\logs\zookeeper
dataLogDir=D:\data\logs\zookeeper
1.6 添加如下系统变量:ZOOKEEPER_HOME: D:\zookeeper-3.4.8
Path: 在现有的值后面添加 ;%ZOOKEEPER_HOME%\bin;
2.安装kafka
2.1 下载安装文件: http://kafka.apache.org/downloads.html
2.2 解压文件(本文解压到 D:\kafka_2.11-0.10.2.0)
2.3 打开D:\kafka_2.11-0.10.2.0\config\ server.properties
2.4 把 log.dirs的值改成 log.dirs=D:\data\logs\kafka
2.5 D:\kafka_2.11-0.10.2.0\bin文件夹下的.sh命令脚本是在shell下运行的,此文件夹下还有个 windows文件夹,里面是windows下运行的.bat命令脚本
2.6 在D:\kafka_2.11-0.10.2.0文件夹中”Shift+鼠标右键”点击空白处打开命令提示窗口
2.7 输入并执行一下命令以打开kafka:
.\bin\windows\kafka-server-start.bat .\config\server.properties
3.创建topics
3.1在D:\kafka_2.11-0.10.2.0\bin\windows文件夹中”Shift+鼠标右键”点击空白处打开命令提示窗口
kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
1
4.打开一个Producer
4.1在D:\kafka_2.11-0.10.2.0\bin\windows文件夹中”Shift+鼠标右键”点击空白处打开命令提示窗口
kafka-console-producer.bat --broker-list localhost:9092 --topic test
1
5.打开一个Consumer
5.1在D:\kafka_2.11-0.10.2.0\bin\windows文件夹中”Shift+鼠标右键”点击空白处打开命令提示窗口
kafka-console-consumer.bat --zookeeper localhost:2181 --topic test
1
注意:以上打开的窗口不要关闭
然后就可以在Producer控制台窗口输入消息了。在消息输入过后,很快Consumer窗口就会显示出Producer发送的消息:
3.kafka配置
############################# Server Basics #############################
# 唯一标识一个broker.
broker.id=1
############################# Socket Server Settings #############################
#绑定服务监听的地址和端口,要填写hostname -i 出来的地址,否则可能会绑定到127.0.0.1,producer可能会发不出消息
listeners=PLAINTEXT://172.23.8.144:9092
#broker对producers和consumers服务的地址和端口,如果没有配置,使用listeners的配置,本文没有配置该项
#advertised.listeners=PLAINTEXT://your.host.name:9092
# 处理网络请求的线程数
num.network.threads=3
# 处理磁盘I/O的线程数
num.io.threads=8
# socket server的发送buffer大小 (SO_SNDBUF)
socket.send.buffer.bytes=102400
# socket server的接收buffer大小 (SO_RCVBUF)
socket.receive.buffer.bytes=102400
#一个请求的最大size,用来保护防止oom
socket.request.max.bytes=104857600
############################# Log Basics #############################
#存放日志和消息的目录,可以是用逗号分开的目录,同样不推荐使用/tmp
log.dirs=/usr/local/services/kafka/kafka-logs
#每个topic默认partitions的数量,数量较大表示消费者可以有更大的并行度。
num.partitions=2
# The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
# This value is recommended to be increased for installations with data dirs located in RAID array.
num.recovery.threads.per.data.dir=1
#日志的过期时间,超过后被删除,单位小时
log.retention.hours=168
#一个日志文件最大大小,超过会新建一个文件
log.segment.bytes=1073741824
#根据过期策略检查过期文件的时间间隔,单位毫秒
log.retention.check.interval.ms=300000
############################# Zookeeper #############################
#Zookeeper的连接配置,用逗号隔开,也可以用172.23.8.59:2181/kakfa这样的方式指定kafka数据在zk中的根目录
zookeeper.connect=172.23.8.144:2181,172.23.8.179:2181,172.23.8.59:2181
# 连接zk的超时时间
zookeeper.connection.timeout.ms=6000
3.kafka建立生产者,推送数据到kafka
先根据上述的命令生成topic 然后打开
kafka-console-consumer.bat --zookeeper localhost:2181 --topic test 这是控制台显示消费情况
接着写代码生产一些数据给kafka,以下是生产代码(消费代码可以自行尝试写)
package xiaoyuefei.kafkatest;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.json.JSONObject;
public class mytest {
private static final long serialVersionUID = 1L;
private Map<String,String> topicMap;
private Producer<String, String> producer;
public mytest() {
topicMap = new HashMap<String,String>();
Properties props = new Properties();
props.put("bootstrap.servers", "127.0.0.1:9092");//这个是在kafka的server.properties配置文件配的端口号,不是zookeeper的2181
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("client.id", "xyf");
producer = new KafkaProducer<String, String>(props);
}
public static void main(String[] args) {
mytest my = new mytest();
my.topicMap.put("topic", "test");
try {
my.sendMessage();
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendMessage() throws InterruptedException, ExecutionException, TimeoutException, SQLException {
List<Future<RecordMetadata>> sendMeta = new ArrayList<Future<RecordMetadata>>();
String topic = topicMap.get("topic");
int i = 1 ;
while(i<10) {
JSONObject message = new JSONObject();
message.append("session", i);
message.append("name", "xyf");
message.append("score", 90);
Future<RecordMetadata> send = producer.send(new ProducerRecord<String,String>(topic, message.toString()));
sendMeta.add(send);
i = i + 1 ;
}
for(Future<RecordMetadata> send:sendMeta) {
send.get(5000, TimeUnit.MILLISECONDS);
}
}
}