kafka 소비자 잘못 신고: Class org. apache. kafka. comon. serialization. StringDeserializer 를 찾 을 수 없습니다.

1545 단어 자바
org.apache.kafka.common.config.ConfigException: Invalid value org.apache.kafka.common.serialization.StringDeserializer for configuration key.deserializer: Class org.apache.kafka.common.serialization.StringDeserializer could not be found.
 
classloader 가 같 지 않 아서 불 러 올 수 없습니다.
그 전에
Thread.currentThread().setContextClassLoader(null);

됐 습 니 다. 아래 를 보 세 요.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
kafka client use  Class.forName(trimmed, true, Utils.getContextOrKafkaClassLoader())  to get the Class object, and the create the instance, the key point is the classLoader, which is specified by the last param, the implementation of method  Utils.getContextOrKafkaClassLoader()  is
public static ClassLoader getContextOrKafkaClassLoader() {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    if (cl == null)
        return getKafkaClassLoader();
    else
        return cl;
}

so, by default, the Class object of  org.apache.kafka.common.serialization.StringSerializer  is load by the applicationClassLoader, if your target class is not loaded by the applicationClassLoader, this problem will happend !
to solve the problem, simply set the ContextClassLoader of current thread to null before new KafkaProducer instance like this
Thread.currentThread().setContextClassLoader(null);
Producer producer = new KafkaProducer(props);

좋은 웹페이지 즐겨찾기