使用 spring 启动的 Cloud Kafka 给我错误

Cloud Kafka with spring boot gives me errors

嘿,我遵循了在 spring 引导中设置 kafka 的分步指南。

但是现在我无法启动该应用程序。任何建议:)

日志错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'functionInitializer' defined in class path resource [org/springframework/cloud/stream/function/FunctionConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Found more then one function in BeanFactory: [persistentEntities, resourceMappings]. Consider providing 'spring.cloud.function.definition' property.

什么是spring.cloud.function.definition?以及如何设置? 原因:java.lang.IllegalArgumentException:在 BeanFactory 中发现不止一个函数:[persistentEntities, resourceMappings]。考虑提供 'spring.cloud.function.definition' 属性.

Spring Cloud Functions 在应用程序中执行以下操作:

  1. 搜索任何 Spring 托管 bean 实现或符合功能接口 Supplier<T>Consumer<T>Function<T, R> 的约定。
  2. 将这些 beans 标记为可能 beans 绑定到事件端点(绑定)。
  3. 如果在步骤 1 中只有一个 Spring 托管 bean 解析,那么该 bean 将是唯一的 bean 绑定为 函数 由应用程序公开(绑定到外部事件端点) - 有效地假设应用程序是 Serverless Function.
  4. 如果在第 1 步中解决了多个 Spring 托管 bean,则 Spring Cloud Function 将查看 属性 spring.cloud.function.definition解决哪些 beans 应该绑定到端点。
  5. 如果这个 属性 没有被赋值,那么 Spring Cloud Function 会抛出一个 IllegalArgumentException(你得到的那个)抱怨它无法解析哪些 bean(在所有它已发现的潜在 功能 bean)您想要公开。
  6. 分配给 属性 spring.cloud.function.definition 的值将被读取为 semi-colon separated list of functional bean names

因此,要解决您的问题,您需要:

  • 只需在Spring上下文beanpersistentEntities或beanresourceMappings中注册;但不是两者。

  • 将以下条目添加到您的 Spring 上下文的属性中:

spring.cloud.function.definition=persistentEntities;resourceMappings

希望这对您有所帮助。