Kafka
一、单例模式安装
1、准备安装文件kafka_2.11-0.11.0.0.tgz,放到linux服务器,并解压
2、vim config/server.properties
3、修改项如下:
host.name=你的ip
listeners=PLAINTEXT://你的ip:9092
advertised.listeners=PLAINTEXT://你的ip:9092
zookeeper.connect=zookeeper集群地址
4、启动kafka
./bin/kafka-server-start.sh -daemon config/server.properties &
5、测试
(1)创建topic:my-first-topic
./kafka-topics.sh --create --zookeeper 192.168.85.130:2181 --topic my-first-topic --partitions 1 --replication-factor 1
(2)启动consumer
./kafka-console-consumer.sh --bootstrap-server 192.168.85.130:9092 --topic my-first-topic --from-beginning
本指令执行完毕后暂时没有任何输出。
(3)启动producer,这里需要新开一个窗口
./kafka-console-producer.sh --broker-list 192.168.85.130:9092 --topic my-first-topic
启动成功后,在本窗口输入任意内容,回车发送后,可以上consumer窗口接受到相同的消息。
6、java代码测试
//新建topic public class TestKafka { public static void main(String[] arg){ Properties prop = new Properties(); prop.put("bootstrap.servers","192.168.85.130:9092"); AdminClient adminClient = AdminClient.create(prop); List<NewTopic> topicList = new ArrayList<>(); NewTopic topic = new NewTopic("my-first-topic", 1, (short)1); topicList.add(topic); adminClient.createTopics(topicList); } }
//Producer public class MyProducer { public static void main(String[] args){ Properties props = new Properties(); props.put("bootstrap.servers", "192.168.85.130:9092"); props.put("acks", "all"); props.put("retries", 0); props.put("batch.size", 16384); props.put("linger.ms", 1); props.put("buffer.memory", 33554432); props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); Producer<String, String> producer = new KafkaProducer<String, String>(props); for (int i = 0; i < 10; i++) producer.send(new ProducerRecord<String, String>("my-first-topic", Integer.toString(i), Integer.toString(i))); producer.close(); } }
//Consumer
public class MyConsumer { public static void main(String[] args){ Properties props = new Properties(); props.put("bootstrap.servers", "192.168.85.130:9092"); props.put("group.id", "my-first-topic"); props.put("enable.auto.commit", "true"); props.put("auto.commit.interval.ms", "1000"); props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); final KafkaConsumer<String, String> consumer = new KafkaConsumer<String,String>(props); consumer.subscribe(Arrays.asList("my-first-topic"),new ConsumerRebalanceListener() { public void onPartitionsRevoked(Collection<TopicPartition> collection) { } public void onPartitionsAssigned(Collection<TopicPartition> collection) { //将偏移设置到最开始 consumer.seekToBeginning(collection); } }); while (true) { ConsumerRecords<String, String> records = consumer.poll(100); for (ConsumerRecord<String, String> record : records) System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value()); } } }