Spring Boot 2 应用程序在由 docker-maven-plugin 触发的 docker 容器中运行时无法



当使用mvn spring-boot:run在没有 docker 的情况下运行时,该应用程序连接得很好。使用mvn docker:remove docker:stop clean package docker:build docker:start运行时,将引发以下错误

错误:

[main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.DatabaseException: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796)

application.yaml

spring:
datasource:
url: jdbc:mysql://localhost:3306/db?user=root&createDatabaseIfNotExist=true
username: root
password: new_password
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
connection-timeout: 20000
idle-timeout: 10000
liquibase:
enabled: true

绒球.xml

<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.33.0</version>
<extensions>true</extensions>
<executions>
<execution>
<id>docker-build</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
<execution>
<id>prepare-database</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>remove-database</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<images>
<image>
<name>mysql</name>
<alias>database</alias>
<run>
<log>
<prefix>mysql > </prefix>
<enabled>true</enabled>
<color>green</color>
</log>
<env>
<MYSQL_ROOT_PASSWORD>${db.password}</MYSQL_ROOT_PASSWORD>
<MYSQL_DATABASE>${db.name}</MYSQL_DATABASE>
<MYSQL_PASSWORD>${db.password}</MYSQL_PASSWORD>
</env>
<ports>
<port>3306:3306</port>
</ports>
<wait>
<log>(?s)ready for connections</log>
<time>30000</time>
</wait>
</run>
</image>
<image>
<alias>service</alias>
<name>service</name>
<build>
<from>java:8-jre</from>
<cmd>
<shell>java -jar /maven/mysql-producer-consumer-1.0-SNAPSHOT.jar</shell>
</cmd>
<assembly>
<descriptorRef>artifact-with-dependencies</descriptorRef>
</assembly>
</build>
<run>
<log>
<prefix>service > </prefix>
<enabled>true</enabled>
<color>black</color>
</log>
<wait>
<shutdown>500</shutdown>
<time>20000</time>
</wait>
<dependsOn>
<container>mysql</container>
</dependsOn>
</run>
</image>
</images>
</configuration>
</plugin>

datasourceurl更改为以下值不起作用:

  • jdbc:mysql://mysql/db?user=root&createDatabaseIfNotExist=true
  • jdbc:mysql://database/db?user=root&createDatabaseIfNotExist=true
  • jdbc:mysql://localhost/db?user=root&createDatabaseIfNotExist=true

以下更改有效:

更改url: jdbc:mysql://127.0.0.1:3306/db?user=root&createDatabaseIfNotExist=true

添加network

<image>
<alias>service</alias>
<name>service</name>
<build>
<from>java:8-jre</from>
<cmd>
<shell>java -jar /maven/mysql-producer-consumer-1.0-SNAPSHOT.jar</shell>
</cmd>
<assembly>
<descriptorRef>artifact-with-dependencies</descriptorRef>
</assembly>
</build>
<run>
<log>
<prefix>service > </prefix>
<enabled>true</enabled>
<color>black</color>
</log>
<network>
<mode>container</mode>
<name>mysql</name>
<alias>mysql</alias>
</network>
<wait>
<shutdown>500</shutdown>
<time>20000</time>
</wait>
<dependsOn>
<container>mysql</container>
</dependsOn>
</run>
</image>

更改 MySQL 容器的等待时间:

<wait>
<time>20000</time>
</wait>

相关内容

  • 没有找到相关文章

最新更新