Docker化Spring Boot实例化MqttClient时Bean创建异常求助
问题:Docker容器中MqttClient实例化失败
我在MqttClientService中通过以下方式实例化MqttClient:
public MqttClientService(KipitProperties properties) { String connectionString = String.format("%s://%s:%s", properties.getMqtt().getProtocol(), properties.getMqtt().getUrl(), properties.getMqtt().getPort()); try { this.publisher = new MqttClient(connectionString, properties.getMqtt().getPublisher().getId()); } catch (MqttException ex) { log.error(ExceptionUtils.getMessage(ex)); log.error(ExceptionUtils.getFullStackTrace(ex)); } }
应用在本地运行正常,但部署通过jib构建的Docker镜像时出现如下错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mqttClientService' defined in file [/app/classes/com/kipit/wallet/event/mqtt/MqttClientService.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.kipit.wallet.event.mqtt.MqttClientService]: Constructor threw exception; nested exception is java.lang.RuntimeException: Can not initialize MqttClients at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:310) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:291) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:929) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:591) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:409) at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) at com.kipit.WalletServiceApplication.main(WalletServiceApplication.java:40) Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.kipit.wallet.event.mqtt.MqttClientService]: Constructor threw exception; nested exception is java.lang.RuntimeException: Can not initialize MqttClients at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:226) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117) at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:306) ... 17 common frames omitted Caused by: java.lang.RuntimeException: Can not initialize MqttClients at com.kipit.wallet.event.mqtt.MqttClientService.<init>(MqttClientService.java:30) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:213) ... 19 common frames omitted
我推测问题在于MqttClient无法在Docker容器(基础镜像为openjdk:17-oracle)中创建持久化文件。
内容的提问来源于stack exchange,提问作者Hani Bikdeli
相关产品推荐
相关产品推荐

