1

我有一个创建PersistentEntitiesbean 的基于 Spring Boot 的库(使用 spring-data-mongo)。PersistentEntities碰巧实现了Supplier<T>接口,所以 Spring Cloud Stream 功能绑定器正在创建一个绑定到它。更具体地说,BeanFactoryAwareFunctionRegistry.discoverDefaultDefinitionIfNecessary发现它是一个 bean 类型Supplier

我们正在使用 Spring Cloud Streams Kafka binder,因此 Spring 尝试将这些对象中的每一个发布到它创建的 Kafka 主题。这会在 JSON 序列化程序中导致无限递归问题:

2019-12-04 15:36:54.323 错误 1 ​​--- [scheduling-1] osihLoggingHandler:org.springframework.messaging.MessagingException:调用方法失败;嵌套异常是 org.springframework.messaging.converter.MessageConversionException:无法写入 JSON:无限递归(StackOverflowError)(通过引用链:org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity["idProperty"] -> org. springframework.data.mongodb.core.mapping.CachingMongoPersistentProperty["owner"] -> org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity["idProperty"] -> org.springframework.data.mongodb.core.mapping。 CachingMongoPersistentProperty["owner"] -> org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity["idProperty"] ->

有没有办法将我的 bean 从函数绑定中排除?使用这个库的项目没有使用 Spring Cloud Function,但我更愿意保持这种可能性。

作为参考,我的 bean 定义为:

@Bean
public PersistentEntities myPersistentEntities(List<MongoTemplate> mongoTemplates) {
    return new PersistentEntities(() -> {
        List<MappingContext<?, ?>> mappingContexts = mongoTemplates.stream().map(t -> t.getConverter().getMappingContext()).collect(Collectors.toList());
        return mappingContexts.iterator();
    });
}

我们刚刚将 Spring Cloud 从 Greenwich 升级到了 Hoxton,所以自动功能绑定对我们来说是新的。

4

2 回答 2

3

通常,您可以通过将 spring-cloud-function 明确排除为

@SpringBootApplication(exclude = ContextFunctionCatalogAutoConfiguration.class)

也就是说,请提出一个问题 - https://github.com/spring-cloud/spring-cloud-stream/issues。以前出现过这种变体,我开始相信我们需要比上述更好的解决方案。

另一种解决方法是明确指定不存在的spring.cloud.function.definition=blah属性。blah丑陋,但可以解决问题,并且不需要重新编译,因为不涉及注释或其他属性。

但正如我所说,请提出一个问题,链接到这篇文章,我们将在今年年底之前为 SR1 解决这个问题。

于 2019-12-04T18:27:50.880 回答
0

所以,我遇到了类似的事情。TL;DR 修复是显式定义 Spring Cloud Streams 可用的函数,如下所示:

消费者 Bean 名称:inputConsumer

spring:
  cloud:
    stream:
      function:
        bindings:
          inputConsumer-in-0: DataInputBinding
        definition: inputConsumer
      bindings:
        DataInputBinding:
          binder: kinesis
          destination: whatever
          group: whatever

在我的情况下,我的应用程序有另一个Component实现Supplier. 没有在配置中明确定义函数,Spring Cloud Streams 只是将 allFunction和beans 添加到 the 中,Consumer然后期望它们都附加到流中。SupplierFunctionCatalog

显然,如果它们不是,那么它只是不附加它们中的任何一个并且没有任何作用。:/

于 2021-04-01T17:58:17.833 回答