使用 spring-boot:1.5.1 和 spring-cloud-stream 时无法启动 bean 'inputBindingLifecycle'

Failed to start bean 'inputBindingLifecycle' when using spring-boot:1.5.1 and spring-cloud-stream

我在使用 spring-boot:1.5.1 时遇到下面提到的错误,但在使用 spring-boot:1.4.4

时却没有

有人遇到过吗?

package org.test;

import lombok.Data;
import lombok.ToString;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;

@SpringBootApplication
@EnableBinding(Sink.class)
public class EtestSmsConsumerApplication {

    @StreamListener(Sink.INPUT)
    public void readSms(SmsRequest smsRequest) {
        System.out.println("Received sms " + smsRequest);
    }

    public static void main(String[] args) {
        SpringApplication.run(EtestSmsConsumerApplication.class, args);
    }
}

@Data
@ToString
class SmsRequest {
    private String message;
}

应用程序属性:

spring.cloud.stream.bindings.input.destination=sms
spring.cloud.stream.bindings.input.content-type=application/json
spring.cloud.stream.bindings.input.group=smsGroup2
server.port=8083

POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>etest-sms-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>etest-sms-consumer</name>
    <description></description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-kafka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.M1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

抛出异常

org.springframework.context.ApplicationContextException: 启动bean失败'inputBindingLifecycle';嵌套异常是 java.lang.NoSuchMethodError: org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.(Ljava/lang/reflect/Method;)V 在 org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:176) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:51) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:346) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:149) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:112) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:879) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) ~[spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE] 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 在 org.springframework.boot.SpringApplication.refresh上下文 (SpringApplication.java:370) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 在 org.test.EtestSmsConsumerApplication.main(EtestSmsConsumerApplication.java:21) [类/:na] 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)~[na:1.8.0_121] 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121] 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121] 在 java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121] 在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na] 原因:java.lang.NoSuchMethodError: org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.(Ljava/lang/reflect/Method;)V 在 org.springframework.integration.kafka.inbound.KafkaMessageDrivenChannelAdapter$IntegrationMessageListener.(KafkaMessageDrivenChannelAdapter.java:191) ~[spring-integration-kafka-2.0.1.RELEASE.jar:na] 在 org.springframework.integration.kafka.inbound.KafkaMessageDrivenChannelAdapter.(KafkaMessageDrivenChannelAdapter.java:51) ~[spring-integration-kafka-2.0.1.RELEASE.jar:na] 在 org.springframework.cloud.stream.binder.kafka.KafkaMessageChannelBinder.createConsumerEndpoint(KafkaMessageChannelBinder.java:338) ~[spring-cloud-stream-binder-kafka-1.2.0.M1.jar:1.2.0.M1] 在 org.springframework.cloud.stream.binder.kafka.KafkaMessageChannelBinder.createConsumerEndpoint(KafkaMessageChannelBinder.java:93) ~[spring-cloud-stream-binder-kafka-1.2.0.M1.jar:1.2.0.M1] 在 org.springframework.cloud.stream.binder.AbstractMessageChannelBinder.doBindConsumer(AbstractMessageChannelBinder.java:202) ~[spring-cloud-stream-1.2.0.M1.jar:1.2.0.M1] 在 org.springframework.cloud.stream.binder.AbstractMessageChannelBinder.doBindConsumer(AbstractMessageChannelBinder.java:55) ~[spring-cloud-stream-1.2.0.M1.jar:1.2.0.M1] 在 org.springframework.cloud.stream.binder.AbstractBinder.bindConsumer(AbstractBinder.java:143) ~[spring-cloud-stream-1.2.0.M1.jar:1.2.0.M1] 在 org.springframework.cloud.stream.binding.BindingService.bindConsumer(BindingService.java:98) ~[spring-cloud-stream-1.2.0.M1.jar:1.2.0.M1] 在 org.springframework.cloud.stream.binding.BindableProxyFactory.bindInputs(BindableProxyFactory.java:208) ~[spring-cloud-stream-1.2.0.M1.jar:1.2.0.M1] 在 org.springframework.cloud.stream.binding.InputBindingLifecycle.start(InputBindingLifecycle.java:55) ~[spring-cloud-stream-1.2.0.M1.jar:1.2.0.M1] 在 org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:173) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE] ... 省略了 19 个常见帧

参见recent chat in Gitter

启动使用 spring-kafka 1.1.2 和 0.10.x.x kafka 客户端。

您正在使用旧的 spring-integration-kafka (2.0.x),它使用旧的 1.0.x spring-kafka(对于 0.9.x.x卡夫卡客户端)。

要使用 boot 1.5.1,您需要 spring-integration-kafka 2.1.0、spring-kafka 1.1.2 和 0.10.x.x kafka 客户端。

本文档中的 gitter 聊天点 Using the binder with Apache Kafka 0.10

这是活页夹的 Chelsea.M1 版本与 Spring Initializr 之间的不一致 - Kafka 活页夹启动器需要 0.9 依赖集作为基线,但 Spring 引导父级将 Spring Kafka 依赖项覆盖为 1.1.2。由于 Spring 集成 Kafka 不是由 Spring Boot 管理的,因此由活页夹引入的版本 - 2.0.1 - 与 Spring Kafka 1.1.2 不兼容。

在此处跟踪:https://github.com/spring-cloud/spring-cloud-stream-binder-kafka/issues/88

更新:如果在 Initializr 中使用 Spring Boot 1.5.2.BUILD-SNAPSHOT 及更高版本,应检索正确的依赖集。请注意,需要 Kafka 0.10.1.x。

我在引导和云集成方面遇到了同样的问题,使用的是 Greenwich.SR1 spring 云版本和 2.1.3.RELEASE spring 引导版本。下面gradle为它

build.gradle

plugins {
    id 'org.springframework.boot' version '2.1.3.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', 'Greenwich.SR1')
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-stream'
    implementation 'org.springframework.cloud:spring-cloud-starter-stream-kafka'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.cloud:spring-cloud-stream-test-support'
}

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

为我添加这个依赖工作者。

implementation 'org.springframework.cloud:spring-cloud-context:2.1.2.RELEASE'

添加下面的依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-kafka</artifactId>
        </dependency>