世界上并没有完美的程序,但我们并不因此而沮丧,因为写程序本来就是一个不断追求完美的过程。 ——摘自周志明

kafka案例搭建

kafka0.9搭建

1、创建项目,引入jar包,注意kafka的版本,引入对应的jar包,可以到官网查找需要引入的包
由于是0.9版本,所以导入如下包:

<dependency>
      <groupId>org.apache.kafka</groupId>
      <artifactId>kafka-clients</artifactId>
      <version>0.9.0.0</version>
</dependency>

2、创建topic(参照官网:kafka官网)
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
3、关闭虚拟机防火墙,并且在windos端ping虚拟机,查看网络情况
4、编写生产者

@Test
void producer() {
	Properties props = new Properties();
	props.put("bootstrap.servers", "192.168.3.204:9092");
	props.put("acks", "all");
	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<>(props);
	for(int i = 0; i < 10; i++)
		producer.send(new ProducerRecord<String, String>("srmis-amis", Integer.toString(i), Integer.toString(i)));
	producer.close();
	}

5、编写消费者

@Test
void consumer(){

	Properties props = new Properties();
	props.put("bootstrap.servers", "192.168.3.204:9092");
	//group-id在consumer.properties配置文件中进行配置的
	props.put("group.id", "test");
	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");
	KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
	consumer.subscribe(Arrays.asList("srmis-amis"));
	while (true) {
		ConsumerRecords<String, String> records = consumer.poll(100);
		for (ConsumerRecord<String, String> record : records)
			System.out.printf("offset = %d, key = %s, value = %s", record.offset(), record.key(), record.value());
	}
}

遇到的问题

1、开始生产者极慢,超时退出
原因:kafka没有配置producer.properties
metadata.broker.list=192.168.3.204:9092
2、消费者慢:Attempt to join group test failed due to obsolete coordinator information, retrying.
原因:尝试删除zookper中的数据,然后重启kafka。

posted @ 2021-01-03 12:18  白杯与咖啡  阅读(127)  评论(0编辑  收藏  举报