kafka
下载:kafka
wget https://archive.apache.org/dist/kafka/1.0.2/kafka_2.11-1.0.2.tgz
解压:
tar -zxvf kafka_2.11-1.1.0.tgz
修改名称
mv kafka_2.11-1.1.0 kafka
配置环境变量:
vi /etc/profile
在配置文件中添加: export KAFKA_HOME=/usr/local/apps/kafka export PATH=$PATH:$KAFKA_HOME/bin
server.properties文件:
broker.id=0 broker的id 唯一的 int类型 listeners=PLAINTEXT://:9092 监听端口 默认9092 log.dirs=/usr/local/apps/tmp/kafka-logs 日志文件存放地址
外网访问:
advertised.listeners=PLAINTEXT://145.133.1.49:9092
kafka-server-start.sh $KAFKA_HOME/config/server.properties
后台启动:
kafka-server-start.sh --daemon $KAFKA_HOME/config/server.properties
创建topic
kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
查看所有topic
kafka-topics.sh --list --zookeeper localhost:2181
发送消息
kafka-console-producer.sh --broker-list localhost:9092 --topic test
消费消息
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
kafka-topics.sh --zookeeper localhost:2181 --delete --topic test
集群搭建:
拷贝配置文件:
cp config/server.properties config/server-1.properties cp config/server.properties config/server-2.properties
修改配置文件:
config/server-1.properties: broker.id=1 listeners=PLAINTEXT://:9093 log.dirs=/tmp/kafka-logs-1 config/server-2.properties: broker.id=2 listeners=PLAINTEXT://:9094 log.dirs=/tmp/kafka-logs-2
启动
> bin/kafka-server-start.sh config/server-1.properties & ... > bin/kafka-server-start.sh config/server-2.properties & ...
创建一个分区三个副本
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic
查看
> bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic my-replicated-topic Topic:my-replicated-topic PartitionCount:1 ReplicationFactor:3 Configs: Topic: my-replicated-topic Partition: 0 Leader: 1 Replicas: 1,2,0 Isr: 1,2,0