1,kafka依赖于zookeeper,下载:
,;
2,配置启动ZOOKEEPER
配置项:ZOOKEEPER_HOME,和PATH;参考:
修改zookeeper-3.4.10/conf下,zoo.conf文件:
设置项:
dataDir=/home/t/source/zookeeper-3.4.10/dataDirdataLogDir=/home/t/source/zookeeper-3.4.10/dataLogDir
zookeeper启动:
./zkServer.sh start
3,配置启动kafka
修改kafka配置项:
启动kafka
./kafka-server-start.sh ../config/server.properties
创建topic(消息类型)
./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
生产消息:
./kafka-console-producer.sh --broker-list localhost:9092 --topic test
消费消息:
./kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
最终效果:
生产端输入什么,消费端输出什么。
分区,对于一个topic,3个分区,则同一组消费者数量应当<=3,否则有消费者接受不到数据;
http://www.cnblogs.com/liuwei6/p/6900686.html
Topic在逻辑上可以被认为是一个queue,每条消费都必须指定它的Topic,可以简单理解为必须指明把这条消息放进哪个queue里。为了使得Kafka的吞吐率可以线性提高,物理上把Topic分成一个或多个Partition,每个Partition在物理上对应一个文件夹,该文件夹下存储这个Partition的所有消息和索引文件。
kafka外网访问 advertised.listeners=PLAINTEXT://x.x.x.x:9092
kafka读写
import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.ConsumerRecords; import org.apache.kafka.clients.consumer.KafkaConsumer;import org.apache.kafka.clients.producer.KafkaProducer;import org.apache.kafka.clients.producer.ProducerConfig;import org.apache.kafka.clients.producer.ProducerRecord;import org.apache.kafka.common.TopicPartition;import java.util.Arrays;import java.util.Date;import java.util.Properties;import javax.print.attribute.standard.PrinterLocation; public class KafkaConsumerExample { public static void main(String[] args) throws InterruptedException { Properties props = new Properties(); props.put("bootstrap.servers", "192.168.1.166:9092"); props.put("group.id", "test13"); props.put("enable.auto.commit", "true"); props.put("auto.commit.interval.ms", "1000"); props.put("session.timeout.ms", "30000"); props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("auto.offset.reset", "earliest"); props.put(ProducerConfig.BATCH_SIZE_CONFIG, 1024*1024*5); //往kafka服务器提交消息间隔时间,0则立即提交不等待 props.put(ProducerConfig.LINGER_MS_CONFIG,0); //Kafka Reader KafkaConsumerconsumer = new KafkaConsumer<>(props); consumer.subscribe(Arrays.asList("test")); consumer.seek(new TopicPartition("test", 1), 1); while (true) { ConsumerRecords records = consumer.poll(2000); System.out.println("-------------"+new Date()); for (ConsumerRecord record : records) System.out.printf("offset = %d, key = %s, value = %s\n", record.offset(), record.key(), record.value()); } /* //KafkaWriter KafkaProducer productor = new KafkaProducer<>(props); productor.send(new ProducerRecord ("test", "aaa", "xiaoxiaoxiao2018")); */ } }
名词解释:
bootstrap.servers:Kafka集群连接串,可以由多个host:port组成【your.host.name:9092】