1.linux单机版

前置条件

服务器安装JAVA环境

Zookeeper部署

Kafka目前还是需要借助于Zookeeper实现分布式部署,因此需要先部署一个zookeeper

进入目录

cd /usr/local

下载地址

下载地址如下:https://zookeeper.apache.org/releases.html
下载对应版本即可
image.png
点击之后跳转到如下页面,点击HTTP下载即可,当然也可以复制下载链接,直接在服务器端下载
image.png
下载命令示例:wget https://dlcdn.apache.org/zookeeper/zookeeper-3.5.10/apache-zookeeper-3.5.10-bin.tar.gz

解压

  1. tar -zxvf apache-zookeeper-3.5.10-bin.tar.gz
  2. mv apache-zookeeper-3.5.10-bin zookeeper
  3. rm -rf apache-zookeeper-3.5.10-bin.tar.gz
  4. cd zookeeper/
  5. ls

image.png

创建文件夹

  1. mkdir data
  2. mkdir logs

    修改配置文件

  3. mv conf/zoo_sample.cfg conf/zoo.cfg

  4. vim conf/ zoo.cfg
  5. 修改为如下内容即可,其实最终修改的还是dataDir属性

    1. # The number of milliseconds of each tick
    2. tickTime=2000
    3. # The number of ticks that the initial
    4. # synchronization phase can take
    5. initLimit=10
    6. # The number of ticks that can pass between
    7. # sending a request and getting an acknowledgement
    8. syncLimit=5
    9. # the directory where the snapshot is stored.
    10. # do not use /tmp for storage, /tmp here is just
    11. # example sakes.
    12. dataDir=/usr/local/zookeeper/data
    13. # the port at which the clients will connect
    14. clientPort=2181

    启动zookeeper

  6. /usr/local/zookeeper/bin/zkServer.sh start

  7. jps -l

如图,启动了一个zookeeper的JAVA进程
image.png

  1. ps -ef|grep zookeeper

如图,zookeeper进程启动成功
image.png

其他命令

  1. 重启zookeeper:/usr/local/zookeeper/bin/zkServer.sh restart
  2. 停止zookeeper:/usr/local/zookeeper/bin/zkServer.sh stop

    使用客户端工具链接测试

    如图,zookeeper链接成功
    image.png

    Kafka部署

    进入目录

    cd /usr/local

    下载地址

    下载地址如下:https://kafka.apache.org/downloads
    下载对应版本即可
    image.png
    下载命令示例:wget https://archive.apache.org/dist/kafka/3.0.0/kafka_2.12-3.0.0.tgz

    解压

  3. tar -zxvf kafka_2.13-3.0.0.tgz

  4. mv kafka_2.13-3.0.0 kafka
  5. rm -rf kafka_2.13-3.0.0.tgz
  6. cd kafka
  7. ls

image.png

创建文件夹

mkdir logs

修改配置文件

  1. vim /usr/local/kafka/config/kraft/server.properties
  2. 主要变动如下3处,确保Kafka能被外网链接、修改log保存地址、修改zookeeper地址 ```properties
    ####################### Socket Server Settings

    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://120.48.107.224:9092

####################### Log Basics

A comma separated list of directories under which to store log files

log.dirs=/usr/local/kafka/logs

####################### Zookeeper

Zookeeper connection string (see zookeeper docs for details).

This is a comma separated host:port pairs, each corresponding to a zk

server. e.g. “127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002”.

You can also append an optional chroot string to the urls to specify the

root directory for all kafka znodes.

zookeeper.connect=localhost:2181

Timeout in ms for connecting to zookeeper

zookeeper.connection.timeout.ms=18000

  1. <a name="A0HIU"></a>
  2. ### 整合supervisor管理进程
  3. 1. touch /usr/local/kafka/logs/run.log
  4. 2. cd /etc/supervisord.d/
  5. 3. vim kafka.ini
  6. 4. 输入如下内容
  7. ```shell
  8. [program:kafka]
  9. directory=/usr/local/kafka/bin
  10. command=/usr/local/kafka/bin/kafka-server-start.sh /usr/local/kafka/config/server.properties
  11. autorestart=true
  12. redirect_stderr=true
  13. stdout_logfile=/usr/local/kafka/logs/run.log
  1. supervisorctl update
  2. supervisorctl status
  3. 如图,Kafka进程已开始运行

image.png

  1. tail -f -n 100 /usr/local/kafka/logs/run.log

日志确认,Kafka启动正常
image.png

使用客户端工具链接测试

如图,Kafka链接成功
image.png