kafka介绍:Kafka是最初由Linkedin公司工程师用Java和Scala语言开发,是一个分布式、支持分区的(partition)、多副本的(replica),基于zookeeper协调的分布式消息系统,能够理解为一个消息队列或者实时消息流系统。java
本篇将介绍在Linux服务器运行一个简单的单机实例,让读者对kafka有个基本的认识。apache
ssh到Linux开始操做bootstrap
官网下载地址vim
若是下载很慢,能够点击此处查看Apache镜像服务器地址,找到国内镜像地址进行下载。bash
#从清华镜像下载kafka包 wget https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.0.0/kafka_2.12-2.0.0.tgz #解压 tar -zxvf kafka_2.12-2.0.0.tgz #进入kafka目录 cd kafka_2.12-2.0.0
kafka依赖zookeeper注册中心作协调,须要安装zookeeper,不过kafka已经自带了一个zookeeper服务,为了演示方便直接使用kafka自带的zookeeper服务。服务器
启动zookeeper服务ssh
bin/zookeeper-server-start.sh config/zookeeper.properties
启动新的终端,在新的终端启动kafka服务分布式
bin/kafka-server-start.sh config/server.properties
能够使用jps / jps -m 命令查看启动的kafka进程spa
因为是单机实例,因此咱们使用一个分区一个副本建立一个名称为“test”的topic。.net
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
使用列出topic命令查看当前的topic
bin/kafka-topics.sh --list --zookeeper localhost:2181 test
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test hello world This is a message
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning hello world This is a message
注意:上述命令若是在0.9一下的kafka版本应该改成:
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
在生成者终端发送消息,消费者终端将实时打印。
至此第一个kafka实例已经运行起来了。
服务方式启动:
vim /usr/lib/systemd/system/kafka.service
[Unit] Description=kafka After=network.target [Service] Environment=JAVA_HOME=/opt/soft/jdk1.8.0_141 Type=simple ExecStart=/data/server/kafka/bin/kafka-server-start.sh /data/server/kafka/config/server.properties Restart=on-failure [Install] WantedBy=multi-user.target
kafka2.0.0 配置文件:
#服务监听 本地外网 listeners=PLAINTEXT://0.0.0.0:9092 # Hostname and port the broker will advertise to producers and consumers. If not set, # it uses the value for "listeners" if configured. Otherwise, it will use the value # returned from java.net.InetAddress.getCanonicalHostName(). #对外访问 advertised.listeners=PLAINTEXT://00.00.00.00:9092
本地日志发往kafka flume服务
vim /usr/lib/systemd/system/flume-kafka-system-log.service
[Unit] Description=flume-kafka-system-log After=network.target [Service] Environment=JAVA_HOME=/opt/soft/jdk1.8.0_141 Type=simple ExecStart=/data/server/flume/bin/flume-ng agent --conf /data/server/flume/conf --conf-file /data/server/flume/conf/exec-memory-kafka.conf --name a1 -Dflume.root.logger=INFO,console Restart=on-failure [Install] WantedBy=multi-user.target