CentOS 7 安装kafka

参考了 https://blog.csdn.net/q383965374/article/details/83348419

先安装Zookeeper
CentOS 7 安装 Zookeeper
http://kafka.apache.org/downloads tar xvzf kafka_2.13-2.6.0.tgz mv kafka_2.13-2.6.0 /usr/local/kafka_2.13-2.6.0 cd /usr/local/kafka_2.13-2.6.0 vi config/server.properties listeners=PLAINTEXT://192.168.146.128:9092 zookeeper.connect=192.168.146.128:2181,192.168.146.129:2181,192.168.146.130:2181 iptables -I INPUT -p tcp --dport 9092 -j ACCEPT service iptables save 启动服务器 bin/kafka-server-start.sh config/server.properties 输出 ... [2020-08-11 20:15:27,316] INFO [KafkaServer id=0] started (kafka.server.KafkaServer) 创建单分区单副本的topic test: bin/kafka-topics.sh --create --zookeeper 192.168.146.128:2181 --replication-factor 1 --partitions 1 --topic test 生产消息: bin/kafka-console-producer.sh --broker-list 192.168.146.128:9092 --topic test 然后输入想要发送的消息例如: Hello world! Hello Kafka! 消费消息: bin/kafka-console-consumer.sh --bootstrap-server 192.168.146.128:9092 --topic test --from-beginning 输出如下: Hello world! Hello Kafka! 停止服务器: bin/kafka-server-stop.sh config/server.properties Use Kafka GUI tools: https://kafkatool.com/download.html
添加自动启动
vi /lib/systemd/system/kafka.service
[Unit]
Description=Apache Kafka server (broker)
After=network.target zookeeper.service

[Service]
Type=idle
Environment=PATH=/usr/local/jdk1.8.0_261/bin:/usr/local/jdk1.8.0_261/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/bin
User=root
Group=root
ExecStart=/usr/local/kafka_2.13-2.6.0/bin/kafka-server-start.sh /usr/local/kafka_2.13-2.6.0/config/server.properties
ExecStop=/usr/local/kafka_2.13-2.6.0/bin/kafka-server-stop.sh /usr/local/kafka_2.13-2.6.0/config/server.properties
Restart=on-failure

[Install]
WantedBy=multi-user.target


systemctl daemon-reload
systemctl start kafka
systemctl status kafka
systemctl enable kafka
安装php kafka扩展
注意:librdkafka版本必须为1.5.3,1.6.0有bug
git clone --depth 1 https://github.com/edenhill/librdkafka.git
chmod -R 777 librdkafka
cd librdkafka
./configure && make -j$(nproc) && make install
pecl install rdkafka

如果报错:
/usr/bin/ld:librdkafka.lds:0: syntax error in VERSION script
vi Makefile.config
注释
#WITH_LDS=y
make clean

vi /etc/php.d/30-rdkafka.ini
or
vi /etc/php/8.0/fpm/conf.d/30-rdkafka.ini
vi /etc/php/8.0/cli/conf.d/30-rdkafka.ini
extension=rdkafka.so
For php-fpm docker:
git clone --depth 1 https://github.com/edenhill/librdkafka.git
git clone https://github.com/arnaud-lb/php-rdkafka.git
docker cp librdkafka/ a8d307a3b81b:/root/
docker cp php-rdkafka/ a8d307a3b81b:/root/

docker exec -it a8d307a3b81b /bin/bash
cd /root/librdkafka
./configure
make clean && make && make install

cd /root/php-rdkafka
pecl install rdkafka
phpize
./configure --with-php-config=/usr/local/bin/php-config
make && make install
echo extension=rdkafka.so > /usr/local/etc/php/conf.d/docker-php-ext-rdkafka.ini
cat /usr/local/etc/php/conf.d/docker-php-ext-rdkafka.ini
php -m