1

i have a spring boot application using spring-kafka dependency to publish a message to kafka, the kafka config uses the standard kafka.properties.sasl.jaas.config for authentication to the kafka server, the application is able to run and publish the message when run on intelliJ IDEA but same code when i run using a docker-compose by building a container it fails to publish a message with the below logs.

org.apache.kafka.common.KafkaException: Failed to construct kafka producer at org.apache.kafka.clients.producer.KafkaProducer.(KafkaProducer.java:434) ~[kafka-clients-2.5.0.jar:na] | at org.apache.kafka.clients.producer.KafkaProducer.(KafkaProducer.java:287) ~[kafka-clients-2.5.0.jar:na]

Caused by: javax.security.auth.login.LoginException: unable to find LoginModule class: org.apache.kafka.common.security.plain.PlainLoginModule | at javax.security.auth.login.LoginContext.invoke(LoginContext.java:794) ~[na:1.8.0_212] | at javax.security.auth.login.LoginContext.access$000(LoginContext.java:195) ~[na:1.8.0_212] | at javax.security.auth.login.LoginContext$4.run(LoginContext.java:682) ~[na:1.8.0_212] | at javax.security.auth.login.LoginContext$4.run(LoginContext.java:680) ~[na:1.8.0_212] | at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_212] | at javax.security.auth.login.LoginContext.invokePriv(LoginContext.java:680) ~[na:1.8.0_212] | at javax.security.auth.login.LoginContext.login(LoginContext.java:587) ~[na:1.8.0_212] | at org.apache.kafka.common.security.authenticator.AbstractLogin.login(AbstractLogin.java:60) ~[kafka-clients-2.5.0.jar:na] | at org.apache.kafka.common.security.authenticator.LoginManager.(LoginManager.java:62) ~[kafka-clients-2.5.0.jar:na] | at org.apache.kafka.common.security.authenticator.LoginManager.acquireLoginManager(LoginManager.java:105) ~[kafka-clients-2.5.0.jar:na] | at org.apache.kafka.common.network.SaslChannelBuilder.configure(SaslChannelBuilder.java:158) ~[kafka-clients-2.5.0.jar:na]

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Manjeshkm
  • 11
  • 1
  • 3
  • Sounds like you didn't create a fat jar that includes the Kafka dependencies... Please show your dockerfile and build file (maven/Gradle) – OneCricketeer Feb 18 '21 at 13:55
  • in the dockerfile i am copying a war generated by the mvn clean install to the tomcat base image – Manjeshkm Feb 18 '21 at 13:59
  • I'm not familiar with war files, but spring has an embedded tomcat server, so you only need a jar file and the openjdk base image. Regardless, that doesn't answer the question - you need to package the Kafka dependencies into that file – OneCricketeer Feb 18 '21 at 14:19
  • Refer https://www.baeldung.com/deployable-fat-jar-spring-boot – OneCricketeer Feb 18 '21 at 14:21

1 Answers1

3

A bit late on this issue, I hope someone will find this useful.

The dependencies might be there. However, you can "force" the classloader to pick up the correct classes by invoking:

final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();

try {
    Thread.currentThread().setContextClassLoader(null);
    Thread.currentThread().setContextClassLoader(KafkaProducer.class.getClassLoader());
    // PRODUCER INVOCATION HERE
} finally {
    Thread.currentThread().setContextClassLoader(currentClassLoader);
}

This is similar to the question asked here: Kafka Producer - org.apache.kafka.common.serialization.StringSerializer could not be found