平时工作中用到了kafka这个消息中间件,但是自己没有动手安装过,所以自己在闲暇时尝试安装了一次kafka,本文就是记录一下本次安装遇到的问题与解决办法。
先在服务器上opt下创建zookeeper和kafka文件夹cd optmkdir -p /opt/zookeepermkdir -p /opt/kafka
先找到国内的zookeeper和kafka的镜像下载下来:cd /opt/zookeeperwget https://dlcdn.apache.org/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz cd /opt/kafkawget http://archive.apache.org/dist/kafka/2.4.0/kafka_2.11-2.4.0.tgz
下载好之后就行解压,先对zookeeper进行配置修改cd /opt/zookeeper/tar -zxvf apache-zookeeper-3.6.3-bin.tar.gz sudo mv apache-zookeeper-3.5.6-bin zookeeper ##把文件名修改为zookeepercd /opt/zookeeper/zookeeper/confcp zoo_sample.cfg zoo.cfgsudo vi zoo.cfg
dataDir 定义:zookeeper保存数据的目录;
dataLogDir= zookeeper将写数据的日志文件保存在这个目录里;
cd /opt/zookeeper/zookeeper/bin./zkServer.sh start ###启动./zkServer.sh status ###启动后要查看是否成功启动
对kafka包进行解压及配置修改cd /opt/kafkatar -xzvf kafka_2.11-2.4.0.gzcd kafka_2.11-2.4.0mkdir logs ## 创建日志cd /opt/kafka/kafka_2.11-2.4.0/configsudo vi server.properties
以后台启动的方式启动kafkanohup ./kafka-server-start.sh ../config/server.properties 1>/dev/null 2>&1 &sudo lsof -i:9092 ##查看是否成功启动
创建一个topic[root@mode1 kafka_2.11-2.4.0] bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic zxxOpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=NCreated topic zxx.[root@mode1 kafka_2.11-2.4.0] bin/kafktopics.sh --list --zookeeper localhost:2181 ##查看所有topicOpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=Nzlhzxx
生产者与消费者[root@mode1 kafka_2.11-2.4.0] bin/kafka-console-producer.sh --broker-list xx.xx.x.x:9092 --topic zlh ##启动一##个生产者,往里边写入数据OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N>ll>zz>hh>好>131
#在服务器启动消费者
[root@mode1 kafka_2.11-2.4.0]# bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic zlh --from-beginningOpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=Nllzzhh好131
在自己mac电脑上启动一个消费者,外网连接kafka
➜ kafka_2.11-2.4.0 bin/kafka-console-consumer.sh --bootstrap-server xx.xx.xx.xx:9092 --topic zlh --from-beginningllzzhh好131
至此,在服务器搭建kafka已基本完成,接下来可以在代码中使用kafka来进行玩了