天天看点

解决Kafka消费者启动时频繁打印日志环境:问题分析

环境:

SpringBoot 2.1.5.RELEASE

Kafka kafka_2.11-1.1.1.tgz

kafka maven依赖

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

消费者:

public class ConsumerFastStart {


    public static final String brokerList = "hadoop000:9092,hadoop001:9092,hadoop002:9092";

    public static final String topic = "tests";

    public static void main(String[] args) {

        Properties properties = new Properties();
        properties.put("key.deserializer", StringDeserializer.class.getName());
        properties.put("value.deserializer", StringDeserializer.class.getName());
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList);
        properties.put("group.id", args[0]);
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);

        consumer.subscribe(Collections.singletonList(topic));
        while (true){
            ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
            for (ConsumerRecord<String, String> record : records) {
                System.out.println(record.value());
            }
        }
    }
}
           

生产者:

public class ProducerFastStart {

    public static final String brokerList = "hadoop000:9092,hadoop001:9092,hadoop002:9092";

    public static final String topic = "tests";

    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList);
        //抛出可重试异常,设置重试次数
        properties.put(ProducerConfig.RETRIES_CONFIG, 10);
        KafkaProducer<String, String> producer = new KafkaProducer<>(properties);
        ProducerRecord<String, String> record = new ProducerRecord<>(topic, "Hello, world!");
        try {
            producer.send(record);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            producer.close();
        }

    }
}
           

问题分析

我在百度这个问题时,很多人都说添加一段日志配置到log4j.properties中,但是一直没生效。我查看了maven依赖找到了logback-classic。

我想应该配置的是logback.xml

1.创建logback.xml

2.添加xml配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <logger name="org.apache.kafka.clients" level="info" />
</configuration>
           

3.重启生产者没有日志输出,问题解决。

有缘人,希望能帮助你解决这个问题。