原创

kafka学习笔记——安装配置(单例-linux)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lyhkmm/article/details/79525238

安装环境:linux系统
kafka入门原理:http://blog.csdn.net/lyhkmm/article/details/79528367

安装jdk

可以参考:http://blog.csdn.net/lyhkmm/article/details/79524712 已安装请忽略

安装zookeeper

1、创建zookeeper的安装目录
mkdir -p /usr/local/zookpper
2、进入到zookeeper的安装目录
cd /usr/local/zookpper
3、下载zookeeper的安装文件
wget https://archive.apache.org/dist/zookeeper/zookeeper-3.3.6/zookeeper-3.3.6.tar.gz
4、解压安装文件到当前目录下
tar -vzxf zookeeper-3.3.6.tar.gz
5、添加zookeeper的环境变量
vi /etc/profile
在文件最下方添加
export zookeeper_home=/usr/local/zookeeper/zookeeper-3.3.6
重新加载配置文件
source /etc/profile
6、进入zookeeper脚本目录,测试是否安装成功
cd /usr/local/zookeeper/zookeeper-3.3.6/bin/
执行脚本
./zkServer.sh start
7、出现如下表示安装成功

JMX enabled by default
Using config: /usr/local/zookeeper/zookeeper-3.3.6/bin/../conf/zoo.cfg
grep: /usr/local/zookeeper/zookeeper-3.3.6/bin/../conf/zoo.cfg: No such file or directory
Starting zookeeper ... STARTED

安装kafka

1、创建kafka的安装目录
mkdir -p /usr/local/kafka
2、进入到kafka的安装目录
cd /usr/local/kafka
3、下载安装文件
wget https://archive.apache.org/dist/kafka/0.8.0/kafka_2.8.0-0.8.0.tar.gz
4、解压
tar -zxvf kafka_2.8.0-0.8.0.tar.gz
5、配置kafka,进入配置文件目录
cd /usr/local/kafka/kafka_2.8.0-0.8.0/config
修改文件server.properties
vi server.properties
找到host.name,修改host.name为本地服务器地址,如下

# from java.net.InetAddress.getCanonicalHostName().
host.name=172.16.252.21

修改日志文件的目录(可以根据自己实际情况配置),如下

# A comma seperated list of directories under which to store log files
log.dirs=/usr/local/kafka/log/kafka

其他配置视情况修改
6、配置kafka下zookeeper
创建zookeeper目录
mkdir /usr/local/kafka/zookeeper
创建zookeeper日志目录
mkdir /usr/local/kafka/log/zookeeper
配置zookeeper,进入配置目录
cd /usr/local/kafka/kafka_2.8.0-0.8.0/config
vi zookeeper.properties

# the directory where the snapshot is stored.
dataDir=/usr/local/kafka/zookeeper
dataLogDir=/usr/local/kafka/log/zookeeper
# the port at which the clients will connect
clientPort=2181
# disable the per-ip limit on the number of connections since this is a non-production config
maxClientCnxns=0
tickTime=5000
initLimit=10

dataDir和dataLogDir可以依据自己建立的文件夹路径填写。
7、创建启动和关闭kafka脚本
创建kafka脚本
进入kafka的安装目录
cd /usr/local/kafka
新建启动脚本
vi kafkastart.sh

/usr/local/kafka/kafka_2.8.0-0.8.0/bin/zookeeper-server-start.sh /usr/local/kafka/kafka_2.8.0-0.8.0/config/zookeeper.properties & sleep 3
/usr/local/kafka/kafka_2.8.0-0.8.0/bin/kafka-server-start.sh /usr/local/kafka/kafka_2.8.0-0.8.0/config/server.properties &

创建关闭kafka脚本
vi kafkastop.sh

/usr/local/kafka/kafka_2.8.0-0.8.0/bin/zookeeper-server-stop.sh /usr/local/kafka/kafka_2.8.0-0.8.0/config & sleep 3
/usr/local/kafka/kafka_2.8.0-0.8.0/bin/kafka-server-stop.sh /usr/local/kafka/kafka_2.8.0-0.8.0/config/server.propertics &

添加脚本执行权限
chmod +x kafkastart.sh
chmod +x kafkastop.sh
8、测试kafka
创建主题
进入kafka的bin目录
cd /usr/local/kafka/kafka_2.8.0-0.8.0/bin
执行命令:
./kafka-create-topic.sh –partition 1 –replica 1 –zookeeper localhost:2181 –topic test
查看主题
./kafka-list-topic.sh –zookeeper localhost:2181
topic: test partition: 0 leader: 0 replicas: 0 isr: 0
启动producer
在/usr/local/kafka/kafka_2.8.0-0.8.0/bin目录下执行
./kafka-console-producer.sh broker-list 172.16.252.21:9092 –topic test
开启另一个终端
也是在/usr/local/kafka/kafka_2.8.0-0.8.0/bin目录下执行
./kafka-console-consumer.sh –zookeeper localhost:2181 –topic test
回到之前的终端,输入想发送的文字后按回车:
这里写图片描述
另一个终端就能输出之前发送的文字
这里写图片描述
终端1生产者下所输入的my kafka,在终端2下的消费者下显现,至此kafka的单机安装部署完成。
关闭kafka
进入kafka目录
cd /usr/local/kafka
执行关闭脚本
./kafkastop.sh
也可以用jps(Java Virtual Machine Process Status Too)查看kafka的进程pid,用kill -9 的方式杀死

正文到此结束
Loading...