🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
**注意 - 1**:`listeners`参数不可以为默认值`listeners=PLAINTEXT://:9092` , 否则JavaApi可能连接不上。 ```properties listeners=PLAINTEXT://192.168.8.110:9092 ``` **注意 - 2**:开放端口 ```shell firewall-cmd --add-port=9092/tcp --permanent firewall-cmd --reload firewall-cmd --list-port ``` --- ```java public class ConsumerBuilder { public static void main(String[] args) { Consumer consumer = new ConsumerBuilder().build(); consumer.subscribe(Arrays.asList("gosuncn"));//订阅的主题列表 while (true) { ConsumerRecords<String, String> records = consumer.poll(100); for (ConsumerRecord<String, String> record : records) System.out.printf("偏移量 = %d, 键 = %s, 值 = %s%n", record.offset(), record.key(), record.value()); } } private Properties props = new Properties(); { props.put(ConsumerConfig.GROUP_ID_CONFIG, "test"); props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "true"); props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "1000"); props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); } public ConsumerBuilder() { this("192.168.8.110:9092"); } public ConsumerBuilder(String bootstrapServers) { props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); } public Consumer build() { return new KafkaConsumer<>(props); } } ``` ```xml <dependencies> <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> <version>0.10.2.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> ``` > 特别注意:`ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG`一定一定要和`listeners`相关参数保持一致。