1. 解压安装包到/data目录,并修改权限,并配置环境变量

  2. 修改config/zookeeper.properties文件

    dataDir=/data/zookeeper_data
    clientPort=2181
  3. 修改config/server.properties文件

    broker.id=0
    delete.topic.enable=true
    listeners=PLAINTEXT://0.0.0.0:9092
    advertised.listeners=PLAINTEXT://[外网ip]:9092
    log.dirs=/data/kafka-logs
    zookeeper.connect=localhost:2181
  4. 新建文件夹 /data/kafka-logs 、 /data/zookeeper_data 并修改权限

    sudo chown elk:elk -R /data/kafka-logs
    sudo chown elk:elk -R /data/zookeeper_data
  5. 启动zookeeper、启动kafka

    zookeeper-server-start.sh /data/kafka_2.11-0.10.1.0/config/zookeeper.properties
    kafka-server-start.sh /data/kafka_2.11-0.10.1.0/config/server.properties
  6. 编写操作脚本 kafka.sh

    
    #!/bin/bash
    
    source /etc/profile
    
    #前台启动zk
    
    if [ $1 = "zk" ]
    then
    zookeeper-server-start.sh /data/kafka_2.11-0.10.1.0/config/zookeeper.properties
    
    
    #后台启动zk
    
    elif [ $1 = "zkbg" ]
    then
    nohup zookeeper-server-start.sh /data/kafka_2.11-0.10.1.0/config/zookeeper.properties >/dev/null 2>&1 &
    
    
    #关闭zk
    
    elif [ $1 = "zkstop" ]
    then
    zookeeper-server-stop.sh
    
    
    #前台启动kafka
    
    elif [ $1 = "kafka" ]
    then
    kafka-server-start.sh /data/kafka_2.11-0.10.1.0/config/server.properties
    
    
    #后台启动kafka
    
    elif [ $1 = "kafkabg" ]
    then
    nohup kafka-server-start.sh /data/kafka_2.11-0.10.1.0/config/server.properties >/dev/null 2>&1 &
    
    
    #关闭kafka
    
    elif [ $1 = "kafkastop" ]
    then
    kafka-server-stop.sh
    
    elif [ $1 = "consumer" ]
    then
    kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning  --topic $2
    elif [ $1 = "producer" ]
    then
           kafka-console-producer.sh --broker-list localhost:9092 --topic $2
    elif [ $1 = "create" ]
    then
    kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic $2 #创建topic
    elif [ $1 = "list" ]
    then
    kafka-topics.sh --list --zookeeper localhost:2181 #查看topic
    elif [ $1 = "delete" ]
    then
    kafka-topics.sh --delete --zookeeper localhost:2181 --topic $2 #删除topic
    else
    echo "parameter invalid"
    fi
Logo

Kafka开源项目指南提供详尽教程,助开发者掌握其架构、配置和使用,实现高效数据流管理和实时处理。它高性能、可扩展,适合日志收集和实时数据处理,通过持久化保障数据安全,是企业大数据生态系统的核心。

更多推荐