Kafka消费消息样例配置文件

样例配置文件

private KafkaConsumer<String, String> createConsumer() {
        Properties props = new Properties();
        props.put("bootstrap.servers", kafkaServers);
        props.put("group.id", MiscUtil.getIp());
        props.put("key.deserializer", StringDeserializer.class.getName());
        props.put("value.deserializer", StringDeserializer.class.getName());

        //最佳实践中提到,不要使用 auto commit,只在消息处理成功才提交
        props.put("enable.auto.commit", "false");
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
        consumer.subscribe(Collections.singletonList(topicSync));
        return consumer;
    }
posted @ 2025-06-20 10:18  SpecialSpeculator  阅读(16)  评论(0)    收藏  举报