在Linux环境下安装Kafka

 更新时间:2021年08月19日 16:45:56   作者:老周聊架构  
本文以安装Kafka的步骤做了图文详细介绍,Kafka是一种高吞吐量 的分布式发布订阅消息系统,对Kagka感兴趣的小伙伴可以参考参考本篇文章

一、环境准备

jdk下载地址链接:下载地址

zookeeper下载地址链接:下载地址

kafka下载地址链接:下载地址

1.1 Java环境为前提

1.1.1 上传jdk-8u261-linux-x64.rpm到服务器并安装

# 安装命令
rpm -ivh jdk-8u261-linux-x64.rpm 

在这里插入图片描述

1.1.2 配置环境变量

# 编辑配置文件,jdk的bin目录到/etc/profile文件,对所有用户的shell有效
vim /etc/profile
# 生效
source /etc/profile

在这里插入图片描述

export JAVA_HOME=/usr/java/jdk1.8.0_261-amd64
export PATH=$PATH:$JAVA_HOME/bin

在这里插入图片描述

# 验证
java -version

在这里插入图片描述

至此,jdk安装成功。

1.2 Zookeeper的安装配置

1.2.1 上传zookeeper-3.4.14.tar.gz到服务器,解压到/opt

# 解压zk到指定目录
tar -zxf zookeeper-3.4.14.tar.gz -C /opt

在这里插入图片描述

1.2.2 修改Zookeeper保存数据的目录,dataDir

# 进入conf配置目录
cd /opt/zookeeper-3.4.14/conf
# 复制zoo_sample.cfg命名为zoo.cfg
cp zoo_sample.cfg zoo.cfg
# 编辑zoo.cfg文件
vim zoo.cfg
dataDir=/var/riemann/zookeeper/data

在这里插入图片描述
在这里插入图片描述

1.2.3 编辑/etc/profile,使配置生效

设置环境变量ZOO_LOG_DIR,指定Zookeeper保存日志的位置;
ZOOKEEPER_PREFIX指向Zookeeper的解压目录;
Zookeeperbin目录添加到PATH中:

在这里插入图片描述

export ZOOKEEPER_PREFIX=/opt/zookeeper-3.4.14
export PATH=$PATH:$ZOOKEEPER_PREFIX/bin
export ZOO_LOG_DIR=/var/riemann/zookeeper/log

配置完以后再生效配置:

source /etc/profile 

1.2.4 启动Zookeeper,确认Zookeeper的状态

zkServer.sh start

在这里插入图片描述

至此,zookeeper安装成功。

1.3 Kafka 的安装与配置

1.3.1 上传kafka_2.12-1.0.2.tgz到服务器并解压

tar -zxf kafka_2.12-1.0.2.tgz -C /opt

在这里插入图片描述

1.3.2 配置环境变量并生效

vim /etc/profile 
export KAFKA=/opt/kafka_2.12-1.0.2
export PATH=$PATH:$KAFKA/bin
source /etc/profile 

1.3.3 配置/opt/kafka_2.12-1.0.2/config中的server.properties文件

vi  /opt/kafka_2.12-1.0.2/config/server.properties

在这里插入图片描述

Kafka连接Zookeeper的地址,此处使用本地启动的Zookeeper实例
连接地址是localhost:2181
后面的 myKafkaKafka在Zookeeper中的根节点路径

在这里插入图片描述

配置kafka存储持久化数据目录

在这里插入图片描述

log.dirs=/var/riemann/kafka/kafka-logs

创建上述持久化数据目录

mkdir -p /var/riemann/kafka/kafka-logs

1.4 启动Kafka

进入Kafka安装的根目录,执行如下命令:

kafka-server-start.sh ../config/server.properties

在这里插入图片描述

启动成功,可以看到控制台输出的最后一行的started状态:此时kafka安装成功。

在这里插入图片描述

1.5 重新开一个窗口,查看Zookeeper的节点


在这里插入图片描述

1.6 此时Kafka是前台模式启动,要停止,使用Ctrl+C

如果要后台启动,使用命令:

kafka-server-start.sh -daemon config/server.properties 

查看Kafka的后台进程:

ps -ef | grep kafka

在这里插入图片描述

停止后台运行的Kafka:

kafka-server-stop.sh 

二、生产与消费

查看zookeeper状态,zookeeper启动成功,再启动kafka。

在这里插入图片描述

2.1 kafka-topics.sh 用于管理主题

查看命令的帮助信息

[root@master1 bin]# kafka-topics.sh
Create, delete, describe, or change a topic.
Option                                   Description                            
------                                   -----------                            
--alter                                  Alter the number of partitions,        
                                           replica assignment, and/or           
                                           configuration for the topic.         
--config <String: name=value>            A topic configuration override for the 
                                           topic being created or altered.The   
                                           following is a list of valid         
                                           configurations:                      
                                         	cleanup.policy                        
                                         	compression.type                      
                                         	delete.retention.ms                   
                                         	file.delete.delay.ms                  
                                         	flush.messages                        
                                         	flush.ms                              
                                         	follower.replication.throttled.       
                                           replicas                             
                                         	index.interval.bytes                  
                                         	leader.replication.throttled.replicas 
                                         	max.message.bytes                     
                                         	message.format.version                
                                         	message.timestamp.difference.max.ms   
                                         	message.timestamp.type                
                                         	min.cleanable.dirty.ratio             
                                         	min.compaction.lag.ms                 
                                         	min.insync.replicas                   
                                         	preallocate                           
                                         	retention.bytes                       
                                         	retention.ms                          
                                         	segment.bytes                         
                                         	segment.index.bytes                   
                                         	segment.jitter.ms                     
                                         	segment.ms                            
                                         	unclean.leader.election.enable        
                                         See the Kafka documentation for full   
                                           details on the topic configs.        
--create                                 Create a new topic.                    
--delete                                 Delete a topic                         
--delete-config <String: name>           A topic configuration override to be   
                                           removed for an existing topic (see   
                                           the list of configurations under the 
                                           --config option).                    
--describe                               List details for the given topics.     
--disable-rack-aware                     Disable rack aware replica assignment  
--force                                  Suppress console prompts               
--help                                   Print usage information.               
--if-exists                              if set when altering or deleting       
                                           topics, the action will only execute 
                                           if the topic exists                  
--if-not-exists                          if set when creating topics, the       
                                           action will only execute if the      
                                           topic does not already exist         
--list                                   List all available topics.             
--partitions <Integer: # of partitions>  The number of partitions for the topic 
                                           being created or altered (WARNING:   
                                           If partitions are increased for a    
                                           topic that has a key, the partition  
                                           logic or ordering of the messages    
                                           will be affected                     
--replica-assignment <String:            A list of manual partition-to-broker   
  broker_id_for_part1_replica1 :           assignments for the topic being      
  broker_id_for_part1_replica2 ,           created or altered.                  
  broker_id_for_part2_replica1 :                                                
  broker_id_for_part2_replica2 , ...>                                           
--replication-factor <Integer:           The replication factor for each        
  replication factor>                      partition in the topic being created.
--topic <String: topic>                  The topic to be create, alter or       
                                           describe. Can also accept a regular  
                                           expression except for --create option
--topics-with-overrides                  if set when describing topics, only    
                                           show topics that have overridden     
                                           configs                              
--unavailable-partitions                 if set when describing topics, only    
                                           show partitions whose leader is not  
                                           available                            
--under-replicated-partitions            if set when describing topics, only    
                                           show under replicated partitions     
--zookeeper <String: urls>               REQUIRED: The connection string for    
                                           the zookeeper connection in the form 
                                           host:port. Multiple URLS can be      
                                           given to allow fail-over.            
[root@master1 bin]# 
# 列出现有的主题
[root@master1 ~]# kafka-topics.sh --list --zookeeper localhost:2181/myKafka
# 创建主题,该主题包含一个分区,该分区为Leader分区,它没有Follower分区副本。
[root@master1 ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --create --topic topic_test --partitions 1 --replication-factor 1
# 查看分区信息
[root@master1 ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --list
# 查看指定主题的详细信息
[root@master1 ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --describe --topic topic_test 
# 删除指定主题
[root@master1 ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --delete --topic topic_test 

列出现有主题,创建主题,该主题包含一个分区,该分区为Leader分区,它没有Follower分区副本。

在这里插入图片描述

查看指定主题的详细信息

在这里插入图片描述

创建主题,该主题包含多个分区

多个分区:横向扩展
多个副本:高可用

在这里插入图片描述

2.2 kafka-console-consumer.sh用于消费消息

# 开启消费者
[root@node1 ~]# kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic topic_test 
# 开启消费者方式二,从头消费,不按照偏移量消费
[root@node1 ~]# kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic topic_test --from-beginning

2.3 kafka-console-producer.sh用于生产消息

# 开启生产者
[root@node1 ~]# kafka-console-producer.sh --topic topic_test --broker-list  localhost:9020

2.4 具体操作

开启消费者和生产者,生产并消费消息。

在这里插入图片描述

消费者,按照偏移量消费

在这里插入图片描述

消费者从头消费,不按照偏移量消费

在这里插入图片描述

到此这篇关于在Linux环境下安装Kafka的文章就介绍到这了,更多相关Linux下安装Kafka内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Linux中查找工具的友好替代方案

    Linux中查找工具的友好替代方案

    今天小编就为大家分享一篇关于Linux中查找工具的友好替代方案,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-09-09
  • 让Apache支持cgi、SSI、shtml的配置方法

    让Apache支持cgi、SSI、shtml的配置方法

    配置Apache支持cgi、SSI、shtml,供大家学习参考
    2013-02-02
  • Linux环境下快速搭建ftp服务器方法介绍

    Linux环境下快速搭建ftp服务器方法介绍

    这篇文章主要介绍了Linux环境下快速搭建ftp服务器方法介绍,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01
  • Linux命令行上如何使用日历详解

    Linux命令行上如何使用日历详解

    这篇文章主要给大家介绍了Linux命令行上如何使用日历的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用linux具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-04-04
  • 解析Linux文件夹文件创建、删除

    解析Linux文件夹文件创建、删除

    本篇文章主要介绍解析Linux文件夹文件创建、删除,具有一定的参考价值,有需要的可以了解一下。
    2016-11-11
  • Linux(CentOS7)使用 RPM 安装 mysql 8.0.11的教程

    Linux(CentOS7)使用 RPM 安装 mysql 8.0.11的教程

    这篇文章主要介绍了Linux(CentOS7)使用 RPM 安装 mysql 8.0.11的教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-03-03
  • linux 基础命令大全

    linux 基础命令大全

    学过Linux的人都会知道,Linux中的命令的确是非常多,但是玩过Linux的人也从来不会因为Linux的命令如此之多而烦恼,本文介绍了linux 基础命令,有兴趣的可以了解一下。
    2016-10-10
  • CentOS服务器下安装Webmin管理系统的步骤

    CentOS服务器下安装Webmin管理系统的步骤

    如果你想像windows图形化管理系统,Webmin无疑是最好的选择。
    2011-03-03
  • linux 安装配置lamp v2

    linux 安装配置lamp v2

    距离第一个版本已经有一年了。修正了几个错误的地方,还有取消了某些lib的安装,因为centos有,所以相关的lib安装我都没有去查找错误。
    2009-02-02
  • linux命令详解date使用方法(计算母亲节和父亲节日期脚本示例)

    linux命令详解date使用方法(计算母亲节和父亲节日期脚本示例)

    date命令可以用来显示和修改系统日期时间,本文对其做了详细说明,最近提供计算指定年份的母亲节和父亲节的日期脚本示例,大家参考使用吧
    2013-12-12

最新评论