kafka安装与验证
下载安装包 可在资源中找到并下载
https://download.csdn.net/download/sinat_30162657/10880738
博客操作文章所用版本为kafka_2.12-0.11.0.0 和 zookeeper-3.3.6. 。jdk版本为oracle 8
安装
- 安装zookeeper
首先确保有jdk并且配置成功环境变量
创建工作目录work tar -xzvf zookeeper-3.3.6.tar.gz
进入zookeeper-3.3.6 cp conf/zoo_sample.cfg zoo.cfg
用下图配置文件替代原配置文件的内容(创建好所需要的文件夹)
启动 bin/zkServer.sh start 服务
- 安装kafka
tar -zxvf kafka_2.12-0.11.0.0.tgz
bin/kafka-server-start.sh config/server.properties 启动服务
kalka默认端口是 9092 里面server.properties里面可以设置以及所依赖的zookeeperIP和port
到此为止安装完成
验证
- 使用指令进行验证
bin/kafka-topics.sh --create --zookeeper 127.0.0.1:2181 --replication-factor 1 --partitions 1 --topic CSDN
可以使用 zkCli.sh 进入查看topic
使用命令 bin/kafka-console-producer.sh --broker-list localhost:9092 --topic CSDN 向该topic上发布
使用命令 bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic CSDN 订阅消息内容
java测试类
pom 依赖如下:
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.11.0.0</version>
</dependency>
TEST.JAVA
public static void main(String[] args){
Properties properties = new Properties();
properties.put("bootstrap.servers", "node4:9092");
properties.put("key.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
properties.put("value.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = null;
try {
producer = new KafkaProducer<String, String>(properties);
String msg = "CSDN Message " ;
producer.send(new ProducerRecord<String, String>("CSDN", msg));
} catch (Exception e) {
e.printStackTrace();
} finally {
producer.close();
}
}
执行上述代码看见 bin/kafka-console-consumer.sh 创建的消费者进程中 有msg说明安装验证已经完成