【问题标题】:How to create spring bean which is "Kafka SINK" bean to refresh the context from config server如何创建作为“Kafka SINK”bean 的 spring bean 以从配置服务器刷新上下文
【发布时间】:2019-08-03 20:41:49
【问题描述】:

我希望添加一个监听器,允许我从 Kafka 主题中提取消息。在收到消息并根据应用程序名称进行过滤后,会将上下文刷新请求发送到 Spring Config Server,后者会刷新 Spring Config Server 中的属性。

但是,我的应用程序不是 Spring Boot 应用程序,我发现几乎我阅读的每个文档都需要 Spring Boot 应用程序来实现该目标。有没有可能我可以在没有 Spring 启动应用程序的情况下在 Spring Bean 中添加这样的流侦听器?

我创建了一个 bean KafkaListener。并且还添加了@EnableBinding(Sink.class) 之类的注释。但是我添加的注解会抛出java.lang.NoSuchMethodError,然后阻塞运行进程,无法到达构造函数部分。

这是我的代码

@EnableBinding(Sink.class)
public class ApiApplication {
......


@StreamListener(Sink.INPUT)
    public void configRefreshSink(RefreshRemoteApplicationEvent event) {
        log.info("ApiApplication.configRefreshSink() called");
        String applicationToRefreshContext = event.getDestinationService();
        if (applicationToRefreshContext.startsWith(this.applicationName)) {
            Set<String> keys = contextRefresher.refresh();
            log.info("Received remote refresh request. Keys refreshed: " + keys);
        }
        log.info("Received remote refresh request. Destination: " + event.getDestinationService());
    }
}

这些是我得到的错误

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.context.support.ClassPathXmlApplicationContext]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: org/springframework/util/Assert.notNull(Ljava/lang/Object;Ljava/util/function/Supplier;)V (loaded from file:xxxxx/repository/org/springframework/spring-core/4.3.22.RELEASE/spring-core-4.3.22.RELEASE.jar by

【问题讨论】:

    标签: java spring apache-kafka spring-cloud-stream spring-bean


    【解决方案1】:
    1. 错误

    NoSuchMethodError: org/springframework/util/Assert.notNull(Ljava/lang/Object;Ljava/util/function/Supplier;)V

    表示Spring Framework版本太旧-该方法是在Spring Framework 5.0中添加的

    1. Spring Cloud Stream (@StreamListener) 需要 Spring Boot;如果没有 Boot,它将无法工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多