centos|centos 安装kafka

前言
Apache Kafka是一个流行的分布式消息代理,旨在有效地处理大量的实时数据。Kafka集群不仅具有高度的可伸缩性和容错能力,而且与ActiveMQ和RabbitMQ等其他消息代理相比,它还具有更高的吞吐量。尽管它通常用作发布/订阅消息传递系统,但许多组织也将其用于日志聚合,因为它为发布的消息提供了持久存储。
发布/订阅消息传递系统允许一个或多个生产者发布消息,而无需考虑使用者的数量或他们将如何处理消息。订阅的客户端会自动收到更新和创建新消息的通知。与客户端定期轮询以确定是否有新消息可用的系统相比,此系统更高效、可伸缩。
工欲善其事必先利其器,接下来看一下在centos上如何安装kafka。
安装步骤
一,更新系统
对centos系统的软件进行一些更新或者升级,从而在进行安装的时候可以很快的找到安装包。
sudo yum install epel-release -y
sudo yum update -y
centos|centos 安装kafka
文章图片
image centos|centos 安装kafka
文章图片
image 二,安装jdk
安装jdk有两种方式,一种方式安装oracle jdk得下载安装包,第二种方式不用下载安装包,直接通过命令执行就完成安装了,安装的是openjdk。
(1) jdk安装的第一种方式
首先到Oracle官网上下载jdk,jdk 下载地址是:https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html 下载之后上传到centos上,接着执行本地安装jdk。
centos|centos 安装kafka
文章图片
image 下载完成之后上传到centos,接着输入以下命令进行安装。

sudo yum -y localinstall jdk-8u241-linux-x64.rpm


centos|centos 安装kafka
文章图片
image 安装完成之后,接着检查一下jdk是否安装成功。
centos|centos 安装kafka
文章图片
image (2)jdk安装的第二种方式
使用以下得方式进行安装,就不用下载安装包,直接在centos输入命令即可完成安装。输入的命令是:
sudo yum install -y java-1.8.0-openjdk


centos|centos 安装kafka
文章图片
image 安装完成之后检查是否安装成功。
java -version
centos|centos 安装kafka
文章图片
image 由于执行kafka需要配置jdk环境变量,那接下来看一下如何配置jdk变量。
三,配置Java环境变量
如何配置Java变量呢?首先我们得检查一下环境种是否有Java变量。
echo $JAVA_HOME
centos|centos 安装kafka
文章图片
image 检查完没有配置,我们接着来配置一下环境变量,把jdk的安装路径找到,把这个路径赋值给JAVA_HOME,写入到用户主目录下的.bash_profile中或者放到/etc/profile中。通过如下命令来进行配置。
echo ``"JAVA_HOME=$(readlink -f /usr/bin/java | sed "``s:bin/java::``")" | sudo tee -a /etc/profile
centos|centos 安装kafka
文章图片
image 接着让配置文件生效。
source /etc/profile
|
centos|centos 安装kafka
文章图片
image 配置完成之后,接下来进行kafka的安装。
四,安装kafka
从kafka官网上下载kafka的二进制安装包,下载地址是:https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.4.0/kafka_2.12-2.4.0.tgz,下载下来之后进行解压。
wget https:``//mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.4.0/kafka_2.12-2.4.0.tgz
centos|centos 安装kafka
文章图片
image 接着对文件进行解压。
sudo tar -zvxf kafka_2.12-2.4.0.tgz -C /opt/
centos|centos 安装kafka
文章图片
image 提取文件成功之后,接着创建一个文件的软连接,这样可以方便进行kafka的升级。
sudo ln -s /opt/kafka_2.12-2.4.0 /opt/kafka
centos|centos 安装kafka
文章图片
image centos|centos 安装kafka
文章图片
image 接下来,进入到kafka目录下,启动kafka,启动kafka之前得先启动zookeeper。
sudo ./zookeeper-server-start.sh ../config/zookeeper.properties
centos|centos 安装kafka
文章图片
image 接着在开启一个shell进行启动kafka。
sudo ./kafka-server-start.sh ../config/server.properties
centos|centos 安装kafka
文章图片
image 由于通过上面的方式启动不是很方便,接下来就配置系统单元服务进行启动kafka和zookeeper。
按照以下方式创建zookeeper.service系统服务单元。
[sharplee@localhost ~]$sudo vi /etc/systemd/system/zookeeper.service
[Unit]
Description=Apache Zookeeper server
Documentation=http:``//zookeeper.apache.org
Requires=network.target remote-fs.target
After=network.target remote-fs.target
[Service]
Type=simple
ExecStart=/opt/kafka/bin/zookeeper-server-start.sh /opt/kafka/config/zookeeper.properties
ExecStop=/opt/kafka/bin/zookeeper-server-stop.sh
Restart=``on``-abnormal
User=root
Group=root
[Install]
WantedBy=multi-user.target
|
centos|centos 安装kafka
文章图片
image 保存完成之后,来启动验证一下是否配置成功。
sudo systemctl start zookeeper
sudo systemctl status zookeeper
sudo systemctl start zookeeper
centos|centos 安装kafka
文章图片
image 接着在配置一下kafka服务器的系统服务单元。
[sharplee@localhost ~]$ sudo vi /etc/systemd/system/kafka.service
[Unit]
Description=Apache Kafka Server
Documentation=http:``//kafka.apache.org/documentation.html
Requires=zookeeper.service
[Service]
Type=simple
ExecStart=/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties
ExecStop=/opt/kafka/bin/kafka-server-stop.sh
Restart=``on``-abnormal
[Install]
WantedBy=multi-user.target
centos|centos 安装kafka
文章图片
image 配置完成之后,接着来启动kafka。
sudo systemctl start kafka
sudo systemctl status kafka
sudo systemctl stop kafka
centos|centos 安装kafka
文章图片
image 创建成功之后,接下来,测试一下是否安装成功。
测试之前先通过kafka创建一个topic。进入到kafka目录下,接着通过下面命令创建一个kafka topic。
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic testTopic
centos|centos 安装kafka
文章图片
image 查看创建了多少个topic,可以使用以下命令进行查看。
bin/kafka-topics.sh --list --zookeeper localhost:2181
centos|centos 安装kafka
文章图片
image 接下来通过生产者进行发送消息。
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic testTopic
centos|centos 安装kafka
文章图片
image 接着创建一个消费者来进行接收消息。
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic testTopic --``from``-beginning
centos|centos 安装kafka
文章图片
image 【centos|centos 安装kafka】通过以上步骤,就成功安装kafka了,这个时候就可以通过java开发工具连接进行开发了。

    推荐阅读