我正在使用EmbeddedKafka通过以下注释配置来测试我的模块:
@ExtendWith(SpringExtension.class)
@SpringBootTest
@TestPropertySource(locations = "classpath:test.properties")
@EmbeddedKafka(partitions = 1,
topics = {"topic"},
brokerProperties = {
"auto.create.topics.enable=${topics.autoCreate:false}",
"delete.topic.enable=${topic.delete:true}",
"broker.id=2"})
大多数时候都可以使用。
但是有时,由于Zookeeper会话超时,创建弹簧上下文失败:
java.lang.IllegalStateException:无法在以下位置加载ApplicationContext
...
在java.base / java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) 在 java.base / java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:628) 在 org.gradle.internal.concurrent.ThreadFactoryImpl $ ManagedThreadRunnable.run(ThreadFactoryImpl.java:55) 在java.base / java.lang.Thread.run(Thread.java:834)造成原因: org.springframework.beans.factory.BeanCreationException:错误 创建名称为'embeddedKafka'的bean:init方法的调用 失败嵌套异常为 org.apache.zookeeper.KeeperException $ SessionExpiredException: KeeperErrorCode =会话已在过期 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1745) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:410) 在 org.springframework.kafka.test.context.EmbeddedKafkaContextCustomizer.customizeContext(EmbeddedKafkaContextCustomizer.java:106) 在 org.springframework.boot.test.context.SpringBootContextLoader $ ContextCustomizerAdapter.initialize(SpringBootContextLoader.java:320) 在 org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:649) 在 org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:373) 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:314) 在 org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:127) 在 org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99) 在 org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117) ... 73更多原因: org.apache.zookeeper.KeeperException $ SessionExpiredException: KeeperErrorCode =会话已在过期 org.apache.zookeeper.KeeperException.create(KeeperException.java:130) 在 kafka.zk.KafkaZkClient.checkedEphemeralCreate(KafkaZkClient.scala:1631) 在kafka.zk.KafkaZkClient.registerBroker(KafkaZkClient.scala:87)在 kafka.server.KafkaServer.startup(KafkaServer.scala:257)在 kafka.utils.TestUtils $ .createServer(TestUtils.scala:132)在 kafka.utils.TestUtils.createServer(TestUtils.scala)在 org.springframework.kafka.test.EmbeddedKafkaBroker.afterPropertiesSet(EmbeddedKafkaBroker.java:215) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741) ...还有82
有人知道为什么会发生吗?为什么在使用EmbeddedKafka时甚至需要ZooKeeper?
任何帮助将不胜感激。