配置kafkaTemplate失败[重复]

bd1hkmkf  于 2023-10-15  发布在  Apache
关注(0)|答案(1)|浏览(221)

这个问题已经有答案了

java.lang.ClassNotFoundException: org.apache.kafka.clients.consumer.ConsumerGroupMetadata(1个答案)
2个月前关闭。
使用Kotlin和spring Boot ,我设置了简单的Kafka配置代码。

@Configuration
class KafkaProducerConfig(
    @Value("\${kafka.hosts}") private val hosts: String,
    @Value("\${kafka.retry.interval}") private val interval: Int,
    @Value("\${kafka.retry.max-attempts}") private val maxAttempts: Int,
) {

    @Bean
    fun myRetryTopic(kafkaTemplate: KafkaTemplate<String, RetryTarget>): RetryTopicConfiguration {
        return RetryTopicConfigurationBuilder
            .newInstance()
            .fixedBackOff(interval)
            .maxAttempts(maxAttempts)
            .concurrency(1)
            .includeTopics(listOf(KafkaTopicName.RETRY.name))
            .create(kafkaTemplate)
    }

    @Bean
    fun kafkaTemplate(): KafkaTemplate<String, RetryTarget> {
        return KafkaTemplate<String, RetryTarget>(producerFactory())
    }

    fun producerFactory(): ProducerFactory<String, RetryTarget> {
        val producerConfig: Map<String, Any> = hashMapOf(
            ProducerConfig.BOOTSTRAP_SERVERS_CONFIG to hosts,
            ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG to StringSerializer::class.java,
            ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG to JsonSerializer::class.java,
        )
        return DefaultKafkaProducerFactory(producerConfig)
    }
}

enum class KafkaTopicName {
    RETRY,
}

data class TestRetryEvent(
    val id: String,
): RetryTarget

interface RetryTarget

Web应用程序入口代码如下。

@SpringBootApplication(
    scanBasePackages = ["com.test"],
    exclude = [KafkaAutoConfiguration::class]
)
class WebApplication

fun main(args: Array<String>) {
    if (Objects.equals(System.getenv("SHUTDOWN_IMMEDIATE"), "true")) {
        exitProcess(0)
    }
    runApplication<WebApplication>(*args)
}

问题是
1.这段代码在新的skeleton spring Boot 项目中运行良好。
1.然而,同样的代码在我的工作项目中抛出错误。

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'kafkaTemplate' defined in class path resource [com/test/mine/adaptor/out/event/retry/kafka/KafkaProducerConfig.class]: Post-processing of merged bean definition failed
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1405)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1325)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:885)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:789)
    ... 19 common frames omitted
Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.kafka.core.KafkaTemplate] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@2aae9190]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:483)
    at org.springframework.util.ReflectionUtils.doWithLocalMethods(ReflectionUtils.java:320)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.buildPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:444)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:415)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findInjectionMetadata(PersistenceAnnotationBeanPostProcessor.java:369)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(PersistenceAnnotationBeanPostProcessor.java:352)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1082)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573)
    ... 29 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/apache/kafka/clients/consumer/ConsumerGroupMetadata
    at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
    at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3402)
    at java.base/java.lang.Class.getDeclaredMethods(Class.java:2504)
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:465)
    ... 36 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.clients.consumer.ConsumerGroupMetadata
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    ... 40 common frames omitted

Process finished with exit code 1

我自己调试了一天多,还是没能抓到原因。

  • 添加了消费者配置与消费者组ID等->不工作
  • 使用@Primary到kafkaTemplate bean ->不起作用
  • 使用KafkaTemplate<String,String>而不是KafkaTemplate<String,RetryTarget> ->不起作用
  • 等等

如果您知道抛出错误的原因,这将对我有很大的帮助。
我先谢谢你。

axr492tv

axr492tv1#

我找到了答案,通过如何接近使用另一个职位已经解决
java.lang.ClassNotFoundException: org.apache.kafka.clients.consumer.ConsumerGroupMetadata
我的一个队友使用特定的kafka-clients版本,我没有注意到。implementation("org.apache.kafka:kafka-clients:2.2.1")
在我删除了特定的kafka-clients版本(2.2.1)之后,终于构建并运行成功了。

相关问题