无法使用docker运行Spring Boot Application

时间:2019-02-04 09:30:05

标签: java spring spring-boot docker

以下情况:

场景: 我在Eclipse上使用Spring Boot 2开发了一个微服务应用程序。该应用程序运行正常。现在我想用docker运行它。另外,映像必须与mysql db交互。 为了构建docker镜像,我在pom.xml中使用com.spotify插件。

    <plugin>
      <groupId>com.spotify</groupId>
      <artifactId>docker-maven-plugin</artifactId>

      <configuration>
        <imageName>usermanagementservice</imageName>
        <baseImage>openjdk:8-jre-alpine</baseImage>
        <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
        <!-- copy the service's jar file from target into the root directory of the image --> 
        <resources>
           <resource>
             <targetPath>/</targetPath>
             <directory>${project.build.directory}</directory>
             <include>${project.build.finalName}.jar</include>
           </resource>
        </resources>
      </configuration>
    </plugin>

这将在我的目标文件夹中创建一个docker文件和.jar文件: Docker文件:

FROM java:8
ENTRYPOINT ["java", "-jar", "/UserManagementService-0.0.1-SNAPSHOT.jar"]

所以现在我想用docker compose运行项目:

> compose file: version: '2.1' services:   usermanagement-service:
>     image: usermanagementservice
>     restart: on-failure
>     ports:
>       - "7778:7778"
>     depends_on:
>       mysqldb:
>         condition: service_healthy
> 
>   mysqldb:
>     image: mysql
>     volumes:
>       - ./mysql-data:/var/lib/mysql
>       - ./mysql-init-files:/docker-entrypoint-initdb.d
>     restart: always
>     environment:
>       MYSQL_ROOT_PASSWORD: root
>       MYSQL_DATABASE: userdb
>       MYSQL_USER: testuser
>       MYSQL_PASSWORD: testuser
>     expose:
>       - "3306"
>     healthcheck:
>       test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
>       timeout: 3s
>       retries: 10

预期行为 usermanagementservice应该等到我的sql数据库初始化并准备好。完成此操作后,服务应连接到它并启动。

问题: 带有mysql初始化的部分可以工作,但是我的usermanagementservice在启动时崩溃。我遇到了大量不满意的bean异常。

堆栈跟踪:

usermanagement-service_1  | ERROR 04-02-2019 08:51:18,870 main o.s.b.w.e.t.TomcatStarter: Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'customBasicAuthenticat
ionFilter': Unsatisfied dependency expressed through field 'mobileUserController'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mobileUserController': Unsatisfied dependency expressed thro
ugh field 'restTemplate'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'doctorController': Unsatisfied dependency expressed through field 'restTemplate'; nested exception is org.springframe
work.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'restTemplate': Requested bean is currently in creation: Is there an unresolvable circular reference?
usermanagement-service_1  | Feb 04, 2019 8:51:18 AM org.apache.catalina.core.StandardService stopInternal
usermanagement-service_1  | INFO: Stopping service [Tomcat]
usermanagement-service_1  | Feb 04, 2019 8:51:18 AM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
usermanagement-service_1  | WARNING: The web application [ROOT] appears to have started a thread named [HikariPool-1 housekeeper] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
usermanagement-service_1  |  sun.misc.Unsafe.park(Native Method)
usermanagement-service_1  |  java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
usermanagement-service_1  |  java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
usermanagement-service_1  |  java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
usermanagement-service_1  |  java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
usermanagement-service_1  |  java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
usermanagement-service_1  |  java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
usermanagement-service_1  |  java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
usermanagement-service_1  |  java.lang.Thread.run(Thread.java:748)
usermanagement-service_1  | Feb 04, 2019 8:51:18 AM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
usermanagement-service_1  | WARNING: The web application [ROOT] appears to have started a thread named [HikariPool-1 connection adder] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
usermanagement-service_1  |  sun.misc.Unsafe.park(Native Method)
usermanagement-service_1  |  java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
usermanagement-service_1  |  java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
usermanagement-service_1  |  java.util.concurrent.LinkedBlockingQueue.poll(LinkedBlockingQueue.java:467)
usermanagement-service_1  |  java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1073)
usermanagement-service_1  |  java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
usermanagement-service_1  |  java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
usermanagement-service_1  |  java.lang.Thread.run(Thread.java:748)
usermanagement-service_1  | WARN  04-02-2019 08:51:18,918 main o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextExcep
tion: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
usermanagement-service_1  | INFO  04-02-2019 08:51:18,921 main o.s.o.j.LocalContainerEntityManagerFactoryBean: Closing JPA EntityManagerFactory for persistence unit 'default'
usermanagement-service_1  | INFO  04-02-2019 08:51:18,925 main c.z.h.HikariDataSource: HikariPool-1 - Shutdown initiated...
usermanagement-service_1  | INFO  04-02-2019 08:51:18,947 main c.z.h.HikariDataSource: HikariPool-1 - Shutdown completed.
usermanagement-service_1  | INFO  04-02-2019 08:51:18,985 main o.s.b.a.l.ConditionEvaluationReportLoggingListener:
usermanagement-service_1  |
usermanagement-service_1  | Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
usermanagement-service_1  | ERROR 04-02-2019 08:51:18,990 main o.s.b.SpringApplication: Application run failed
usermanagement-service_1  | org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
usermanagement-service_1  |     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155)
usermanagement-service_1  |     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:540)
usermanagement-service_1  |     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
usermanagement-service_1  |     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
usermanagement-service_1  |     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
usermanagement-service_1  |     at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
usermanagement-service_1  |     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
usermanagement-service_1  |     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
usermanagement-service_1  |     at com.usermanagement.UserManagementServiceApplication.main(UserManagementServiceApplication.java:21)
usermanagement-service_1  |     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
usermanagement-service_1  |     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
usermanagement-service_1  |     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
usermanagement-service_1  |     at java.lang.reflect.Method.invoke(Method.java:498)
usermanagement-service_1  |     at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
usermanagement-service_1  |     at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
usermanagement-service_1  |     at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
usermanagement-service_1  |     at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
usermanagement-service_1  | Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
usermanagement-service_1  |     at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:125)
usermanagement-service_1  |     at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:86)
usermanagement-service_1  |     at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:414)
usermanagement-service_1  |     at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:174)
usermanagement-service_1  |     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179)
usermanagement-service_1  |     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152)
usermanagement-service_1  |     ... 16 more
usermanagement-service_1  | Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customBasicAuthenticationFilter': Unsatisfied dependency expressed through field 'mobileUserController'; nested exception is
 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mobileUserController': Unsatisfied dependency expressed through field 'restTemplate'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyE
xception: Error creating bean with name 'doctorController': Unsatisfied dependency expressed through field 'restTemplate'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'restTemplate': Req
uested bean is currently in creation: Is there an unresolvable circular reference?
usermanagement-service_1  |     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
usermanagement-service_1  |     at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
usermanagement-service_1  |     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
usermanagement-service_1  |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378)
usermanagement-service_1  |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
usermanagement-service_1  |     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
usermanagement-service_1  |     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
usermanagement-service_1  |     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
usermanagement-service_1  |     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
usermanagement-service_1  |     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
usermanagement-service_1  |     at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:236)
usermanagement-service_1  |     at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:192)
usermanagement-service_1  |     at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:187)
usermanagement-service_1  |     at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAdaptableBeans(ServletContextInitializerBeans.java:169)
usermanagement-service_1  |     at org.springframework.boot.web.servlet.ServletContextInitializerBeans.<init>(ServletContextInitializerBeans.java:89)
usermanagement-service_1  |     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getServletContextInitializerBeans(ServletWebServerApplicationContext.java:250)
usermanagement-service_1  |     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.selfInitialize(ServletWebServerApplicationContext.java:237)
usermanagement-service_1  |     at org.springframework.boot.web.embedded.tomcat.TomcatStarter.onStartup(TomcatStarter.java:54)
usermanagement-service_1  |     at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5098)
usermanagement-service_1  |     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
usermanagement-service_1  |     at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1429)
usermanagement-service_1  |     at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
usermanagement-service_1  |     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
usermanagement-service_1  |     at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
usermanagement-service_1  |     at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
usermanagement-service_1  |     at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:944)
usermanagement-service_1  |     at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839)
usermanagement-service_1  |     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
usermanagement-service_1  |     at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1429)
usermanagement-service_1  |     at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
usermanagement-service_1  |     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
usermanagement-service_1  |     at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
usermanagement-service_1  |     at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
usermanagement-service_1  |     at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:944)
usermanagement-service_1  |     at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:261)
usermanagement-service_1  |     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
usermanagement-service_1  |     at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
usermanagement-service_1  |     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
usermanagement-service_1  |     at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770)
usermanagement-service_1  |     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
usermanagement-service_1  |     at org.apache.catalina.startup.Tomcat.start(Tomcat.java:370)
usermanagement-service_1  |     at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:106)
usermanagement-service_1  |     ... 21 more

尝试 我遵循了一些有关不同Docker文件的在线教程,等等。但是没有任何效果。 我想这可能是一个入口问题,因为Bean是在应用程序启动之前作为单例创建的。 我必须在我的dockerfile中添加任何导入或对入口点进行任何修改吗? 预先感谢

1 个答案:

答案 0 :(得分:0)

它会导致相同的堆栈跟踪。

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customBasicAuthenticationFilter': Unsatisfied dependency expressed through field 'mobileUserController'; nested exception is org.springframework.beans.factory.Unsat
isfiedDependencyException: Error creating bean with name 'mobileUserController': Unsatisfied dependency expressed through field 'restTemplate'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'doctorContr
oller': Unsatisfied dependency expressed through field 'restTemplate'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'restTemplate': Requested bean is currently in creation: Is there an unresolvable c
ircular reference?

更新:

POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.usermanagement</groupId>
    <artifactId>UserManagementService</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>UserManagementService</name>
    <description>Microservice for usermanagement</description>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-hateoas</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.5</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.2.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.json</groupId>
            <artifactId>javax.json-api</artifactId>   
        </dependency>

                <!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20180813</version>
        </dependency>

        <!-- <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.json</artifactId>
           <version>1.1</version>
        </dependency> -->
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.el</artifactId>
        </dependency>


    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
              <groupId>com.spotify</groupId>
              <artifactId>docker-maven-plugin</artifactId>

              <configuration>
                <imageName>usermanagementservice</imageName>
                <baseImage>openjdk:8-jre-alpine</baseImage>
                <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
                <!-- copy the service's jar file from target into the root directory of the image --> 
                <resources>
                   <resource>
                     <targetPath>/</targetPath>
                     <directory>${project.build.directory}</directory>
                     <include>${project.build.finalName}.jar</include>
                   </resource>
                </resources>
              </configuration>
            </plugin>

        </plugins>
    </build>


</project>