【问题标题】:spring-cloud-stream sample projects raises NoSuchBeanDefinitionException of KafkaStreamsFunctionProcessorspring-cloud-stream 示例项目引发 KafkaStreamsFunctionProcessor 的 NoSuchBeanDefinitionException
【发布时间】:2021-04-12 14:45:27
【问题描述】:

我正在尝试接触 spring-cloud-stream,并创建official blog 的示例项目。

实现与文章完全相同。

@SpringBootApplication
public class SimpleConsumerApplication {

   @Bean
   public java.util.function.Consumer<KStream<String, String>> process() {

       return input ->
               input.foreach((key, value) -> {
                   System.out.println("Key: " + key + " Value: " + value);
               });
   }
}

我在Spring initializr 上选择了Cloud StreamSpring for Apache Kafka Stream,并添加了ShadowJar。现在我的build.gradle是这样的。

plugins {
    id 'org.springframework.boot' version '2.4.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'com.github.johnrengelman.shadow' version '6.1.0'
}

group = 'com.lipsum'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

jar {
    manifest {
        attributes('Main-Class': 'com.lipsum.kafkastream.KafkastreamApplication')
    }
}

shadowJar {
    archiveBaseName.set('kafka-stream-practice')
    archiveClassifier.set('')
    archiveVersion.set('')
}

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', "2020.0.2")
}

dependencies {
    implementation 'org.apache.kafka:kafka-streams'
    implementation 'org.springframework.cloud:spring-cloud-stream'
    implementation 'org.springframework.cloud:spring-cloud-stream-binder-kafka-streams'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

我执行了 uber jar,但是 springboot 应用程序无法识别 bean。

$ java -jar kafka-stream-practice.jar --spring.cloud.stream.bindings.process-in-0.destination=kafka-stream-practice
...
22:47:21.162 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'kafkaStreamsFunctionProcessorInvoker' defined in class path resource [org/springframework/cloud/stream/binder/kafka/streams/function/KafkaStreamsFunctionAutoConfiguration.class]: Unsatisfied dependency expressed through method 'kafkaStreamsFunctionProcessorInvoker' parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsFunctionProcessor' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsFunctionProcessor' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

我认为实施没有任何问题。我错过了一些依赖项吗?

【问题讨论】:

    标签: apache-kafka-streams spring-cloud-stream spring-cloud-stream-binder-kafka


    【解决方案1】:

    我从初始化中尝试了一个快速的 maven 项目,它开始正常。上一个版本 (3.0.11) 有一个已知错误,此后在快照上已修复。您可以通过将启动执行器依赖项添加到项目或将活页夹升级到最新快照来解决此问题。你能试试maven的方法吗?如果问题依然存在,请分享一个可重现的样本,然后我们再看看。

    【讨论】:

    • 谢谢。移除shadowJar后,构建成功。
    【解决方案2】:

    它在删除shadowJar 后工作,而是使用 Spring Boot Gradle 插件的bootJar 任务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-03
      • 2021-06-12
      • 2018-01-24
      • 1970-01-01
      • 2017-03-26
      • 2018-01-24
      • 1970-01-01
      • 2016-10-01
      相关资源
      最近更新 更多