【问题标题】:Java spring boot cache generation in docker container failsDocker容器中的Java Spring Boot缓存生成失败
【发布时间】:2020-02-26 16:11:38
【问题描述】:

在 Docker 中运行 java spring boot 应用程序是否需要考虑特殊配置?

我尝试在 Docker 容器中运行的 application 失败,并出现与缓存生成相关的错误 (full error log)。在没有 Docker 的情况下构建和运行应用程序没有任何问题。

从日志中快速查看一些错误:

2019-10-31 08:24:37.982  INFO 617 --- [           main] org.ehcache.xml.XmlConfiguration         : Loading Ehcache XML configuration from file:/usr/project/application/target/application-0.9.0.jar!/BOOT-INF/classes!/ehcache.xml.
2019-10-31 08:24:38.611  INFO 617 --- [           main] org.ehcache.jsr107.ConfigurationMerger   : Configuration of cache introspectCache will be supplemented by template heap-cache
2019-10-31 08:24:38.612  INFO 617 --- [           main] org.ehcache.jsr107.ConfigurationMerger   : Cache introspectCache will use expiry configuration from template heap-cache
2019-10-31 08:24:38.809  INFO 617 --- [           main] o.t.o.p.UpfrontAllocatingPageSource      : Allocating 200.0MB in chunks
2019-10-31 08:24:38.965  INFO 617 --- [           main] org.ehcache.core.EhcacheManager          : Cache 'introspectCache' created in EhcacheManager.
2019-10-31 08:24:38.978  INFO 617 --- [           main] org.ehcache.jsr107.ConfigurationMerger   : Configuration of cache templateCache will be supplemented by template heap-cache
2019-10-31 08:24:38.978  INFO 617 --- [           main] org.ehcache.jsr107.ConfigurationMerger   : Cache templateCache will use expiry configuration from template heap-cache
2019-10-31 08:24:38.986  INFO 617 --- [           main] o.t.o.p.UpfrontAllocatingPageSource      : Allocating 200.0MB in chunks
2019-10-31 08:24:39.095  INFO 617 --- [           main] org.ehcache.core.EhcacheManager          : Cache 'templateCache' created in EhcacheManager.
2019-10-31 08:24:39.098  INFO 617 --- [           main] org.ehcache.jsr107.ConfigurationMerger   : Configuration of cache operationaltemplateCache will be supplemented by template heap-cache
2019-10-31 08:24:39.098  INFO 617 --- [           main] org.ehcache.jsr107.ConfigurationMerger   : Cache operationaltemplateCache will use expiry configuration from template heap-cache
2019-10-31 08:24:39.110  INFO 617 --- [           main] o.t.o.p.UpfrontAllocatingPageSource      : Allocating 200.0MB in chunks
2019-10-31 08:24:41.206  WARN 617 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'contributionServiceImp' defined in URL [jar:file:/usr/project/application/target/application-0.9.0.jar!/BOOT-INF/lib/service-0.9.0.jar!/org/ehrbase/service/ContributionServiceImp.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'knowledgeCacheService' defined in URL [jar:file:/usr/project/application/target/application-0.9.0.jar!/BOOT-INF/lib/service-0.9.0.jar!/org/ehrbase/service/KnowledgeCacheService.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManagerCustomizer' defined in class path resource [org/ehrbase/configuration/CacheConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.CacheManager]: Factory method 'cacheManagerCustomizer' threw exception; nested exception is org.ehcache.jsr107.MultiCacheException: [Exception 0] Cache 'operationaltemplateCache' creation in EhcacheManager failed.
2019-10-31 08:24:41.214  INFO 617 --- [           main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2019-10-31 08:24:41.233  INFO 617 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-10-31 08:24:41.273  INFO 617 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2019-10-31 08:24:41.281 ERROR 617 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'contributionServiceImp' defined in URL [jar:file:/usr/project/application/target/application-0.9.0.jar!/BOOT-INF/lib/service-0.9.0.jar!/org/ehrbase/service/ContributionServiceImp.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'knowledgeCacheService' defined in URL [jar:file:/usr/project/application/target/application-0.9.0.jar!/BOOT-INF/lib/service-0.9.0.jar!/org/ehrbase/service/KnowledgeCacheService.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManagerCustomizer' defined in class path resource [org/ehrbase/configuration/CacheConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.CacheManager]: Factory method 'cacheManagerCustomizer' threw exception; nested exception is org.ehcache.jsr107.MultiCacheException: [Exception 0] Cache 'operationaltemplateCache' creation in EhcacheManager failed.
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
    at org.ehrbase.application.EhrBase.main(EhrBase.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'knowledgeCacheService' defined in URL [jar:file:/usr/project/application/target/application-0.9.0.jar!/BOOT-INF/lib/service-0.9.0.jar!/org/ehrbase/service/KnowledgeCacheService.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManagerCustomizer' defined in class path resource [org/ehrbase/configuration/CacheConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.CacheManager]: Factory method 'cacheManagerCustomizer' threw exception; nested exception is org.ehcache.jsr107.MultiCacheException: [Exception 0] Cache 'operationaltemplateCache' creation in EhcacheManager failed.
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    ... 27 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManagerCustomizer' defined in class path resource [org/ehrbase/configuration/CacheConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.CacheManager]: Factory method 'cacheManagerCustomizer' threw exception; nested exception is org.ehcache.jsr107.MultiCacheException: [Exception 0] Cache 'operationaltemplateCache' creation in EhcacheManager failed.
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    ... 41 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.CacheManager]: Factory method 'cacheManagerCustomizer' threw exception; nested exception is org.ehcache.jsr107.MultiCacheException: [Exception 0] Cache 'operationaltemplateCache' creation in EhcacheManager failed.
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 54 common frames omitted
Caused by: org.ehcache.jsr107.MultiCacheException: [Exception 0] Cache 'operationaltemplateCache' creation in EhcacheManager failed.
    at org.ehcache.jsr107.Eh107CacheManager.createCache(Eh107CacheManager.java:205)
    at org.ehrbase.configuration.CacheConfiguration.buildCache(CacheConfiguration.java:64)
    at org.ehrbase.configuration.CacheConfiguration.cacheManagerCustomizer(CacheConfiguration.java:53)
    at org.ehrbase.configuration.CacheConfiguration$$EnhancerBySpringCGLIB$$a4ce32f5.CGLIB$cacheManagerCustomizer$0(<generated>)
    at org.ehrbase.configuration.CacheConfiguration$$EnhancerBySpringCGLIB$$a4ce32f5$$FastClassBySpringCGLIB$$b982cbd8.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
    at org.ehrbase.configuration.CacheConfiguration$$EnhancerBySpringCGLIB$$a4ce32f5.cacheManagerCustomizer(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 55 common frames omitted

The command '/bin/sh -c sh -c "./docker_build_script.sh"' returned a non-zero code: 1

不在应用程序仓库中的其他文件

Dockerfile

# postgres db container with some ehrbase customizations
FROM ehrbaseorg/ehrbase-database-docker:11.5

# you may need to adjust the IP address
ENV DB_URL="jdbc:postgresql://172.17.0.2:5432/ehrbase"
ENV DB_USER="postgres"
ENV DB_PASS="postgres"
ENV POSTGRES_USER="postgres"
ENV POSTGRES_PASSWORD="postgres"

# install java openjdk, maven
RUN apk add --no-cache openjdk8
RUN apk add --no-cache maven
RUN java -version
RUN mvn -version

# copy repo into container filesystem
WORKDIR /usr/project
COPY . .

RUN mkdir -p file_repo/knowledge/archetypes \
    &&  mkdir -p file_repo/knowledge/operational_templates \
    && mkdir -p file_repo/knowledge/templates

# Start Build Process
COPY ./docker_build_script.sh .
RUN chmod +x ./docker_build_script.sh
RUN sh -c "./docker_build_script.sh"

EXPOSE 8080

在 Docker 容器中启动所需的 Shell 脚本

#!/bin/bash

set -e

# Start DB
su - postgres -c "pg_ctl -D ${PGDATA} -w start && pg_ctl -D ${PGDATA} status"

mvn package
sleep 1
java -jar application/target/application-0.9.0.jar

我已将完整的错误日志和重现问题的步骤放入 Github Gist:full error log

有什么办法可以解决这个问题吗?

【问题讨论】:

    标签: spring-boot docker jar ehcache


    【解决方案1】:

    首先让我感到震惊的是,您没有将-XX:MaxDirectMemorySize 设置为任何特定的东西。这意味着它默认与最大堆大小相同。鉴于您正在尝试创建 4 个(?)缓存,每个缓存具有 200MB 的堆外空间,您很可能只是用完了堆外空间。尝试在生成 JVM 时添加合适的值?

    【讨论】:

    • 感谢您的提示。从 ehcache.xml 中删除 200 会使 jar 在 Docker 中运行 :)
    猜你喜欢
    • 2022-01-19
    • 2022-01-09
    • 2020-09-12
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 2017-11-10
    • 2022-11-01
    相关资源
    最近更新 更多