CentOS7 CentOS8安装并使用kafka

CentOS7 CentOS8安装并使用Kafka+Zookeeper

    • 安装zookeeper
    • 安装kafka
    • 关闭防火墙
    • 使用

安装zookeeper

看到这篇文章的同学应该都安装了 VMware Workstation Pro,并安装了centos 7 或者 centos 8,我安装的是桌面版,因为操作太舒服了

  1. 下载地址:https://zookeeper.apache.org/releases.html

CentOS7 CentOS8安装并使用kafka_第1张图片
我选的3.6.3这个版本,太新的话有些框架不兼容~

2.登录linux 创建一个文件夹 zookeeper
CentOS7 CentOS8安装并使用kafka_第2张图片

3.在线下载

wget https://mirrors.bfsu.edu.cn/apache/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz

CentOS7 CentOS8安装并使用kafka_第3张图片
下载后解压

 tar -zxvf apache-zookeeper-3.6.3-bin.tar.gz

进入conf目录 复制 zoo_sample.cfg 命名zoo.cfg

 cp zoo_sample.cfg zoo.cfg

CentOS7 CentOS8安装并使用kafka_第4张图片

  1. 启动zookeeper
bin/zkServer.sh start
  1. 设置zookeeper开机自启动
    1.进入到/etc/init.d目录下,新建一个zookeeper脚本
cd /etc/init.d

CentOS7 CentOS8安装并使用kafka_第5张图片

使用命令vi zookeeper 或者文本编辑器打开

#!/bin/bash    
#chkconfig:2345 20 90    
#description:zookeeper    
#processname:zookeeper    
export JAVA_HOME=/usr/local/programs/jdk1.8
case $1 in    
        start) su root /usr/local/programs/zookeeper-3.4.10/bin/zkServer.sh start;;    
        stop) su root /usr/local/programs/zookeeper-3.4.10/bin/zkServer.sh stop;;    
        status) su root /usr/local/programs/zookeeper-3.4.10/bin/zkServer.sh status;;    
        restart) su root /usr/local/programs/zookeeper-3.4.10/bin/zkServer.sh restart;;    
        *) echo "require start|stop|status|restart" ;;    
esac

CentOS7 CentOS8安装并使用kafka_第6张图片

2.给脚本添加执行权限

chmod +x zookeeper

3.使用service zookeeper start/stop命令来尝试启动关闭zookeeper,使用service zookeeper status查看zookeeper状态。
或者直接 zookeeper start/stop/status

4.添加到开机启动

chkconfig --add zookeeper

5.查看开机自启的服务中是否已经有我们的zookeeper

 chkconfig --list zookeeper

zookeeper 0:off 1:off 2:on 3:on 4:on 5:on 6:off

安装kafka

  1. 下载地址:https://zookeeper.apache.org/releases.html
    CentOS7 CentOS8安装并使用kafka_第7张图片
    我选的2.7这个版本
  2. 登录linux 创建一个文件夹 kafka
    CentOS7 CentOS8安装并使用kafka_第8张图片
  3. 在线下载
wget https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.7.0/kafka_2.12-2.7.0.tgz

下载后解压

 tar -zxvf kafka_2.12-2.7.0.tgz
  1. 修改配置文件(关键)
    进入config目录修改server.properties
# The address the socket server listens on. It will get the value returned from 
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
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://your.host.name:9092
advertised.listeners=PLAINTEXT://192.168.73.128:9092

192.168.73.128 这个是我虚拟机的ip ,修改成同学你的ip就行,通过下面的命令可以查询

ifconfig -a
  1. 后台启动
bin/kafka-server-start.sh -daemon config/server.properties

关闭防火墙

  1. centos7 8 默认用firewalld做防火墙,远程连接的话,关闭防火墙简单粗暴
systemctl stop firewalld

使用

  1. 创建一个topic
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic beike

–create 创建主题

–topic 主题名称

–zookeeper zookeeper集群地址

–replication-factor 每个分区副本因子个数即每个分区有多少副本

–partitions 主题partition数

  1. 打开生产者终端
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic beike
  1. 打开消费者终端
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic beike --from-beginning

CentOS7 CentOS8安装并使用kafka_第9张图片

你可能感兴趣的:(java,linux,kafka,zookeeper)