无法将docker Spring-Boot应用程序与docker-compose中的mysql容器和flyway连接起来
Posted
技术标签:
【中文标题】无法将docker Spring-Boot应用程序与docker-compose中的mysql容器和flyway连接起来【英文标题】:Cannot connect docker Spring-Boot app with mysql container and flyway in docker-compose 【发布时间】:2016-02-19 22:55:23 【问题描述】:我有一个与 mysql 连接的小型 spring-boot 服务。当我使用 maven 和 mysql 的 docker 容器运行应用程序时,应用程序设置工作正常。
现在我也想用 docker 运行应用程序,所以我使用 docker-compose 来引导 mysql 和我的服务作为容器。
但是我不行,我的服务无法连接mysql,出现如下异常:
accountservice_1 | 2015-11-18 08:55:11.626 ERROR 1 --- [ main] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
accountservice_1 | com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
accountservice_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
accountservice_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_66-internal]
accountservice_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_66-internal]
accountservice_1 | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_66-internal]
accountservice_1 | at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_66-internal]
accountservice_1 | at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:983) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:339) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2252) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2285) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2084) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_66-internal]
accountservice_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_66-internal]
accountservice_1 | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_66-internal]
accountservice_1 | at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_66-internal]
accountservice_1 | at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:307) ~[tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:200) ~[tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:708) [tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:642) [tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:464) [tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:141) [tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:115) [tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:102) [tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:126) [tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.flywaydb.core.internal.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:50) [flyway-core-3.2.1.jar!/:na]
accountservice_1 | at org.flywaydb.core.Flyway.execute(Flyway.java:1385) [flyway-core-3.2.1.jar!/:na]
accountservice_1 | at org.flywaydb.core.Flyway.migrate(Flyway.java:1006) [flyway-core-3.2.1.jar!/:na]
accountservice_1 | at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66) [spring-boot-autoconfigure-1.3.0.RELEASE.jar!/:1.3.0.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:296) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1051) [spring-context-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:828) [spring-context-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) [spring-context-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) [spring-boot-1.3.0.RELEASE.jar!/:1.3.0.RELEASE]
accountservice_1 | at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-1.3.0.RELEASE.jar!/:1.3.0.RELEASE]
accountservice_1 | at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:347) [spring-boot-1.3.0.RELEASE.jar!/:1.3.0.RELEASE]
accountservice_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:295) [spring-boot-1.3.0.RELEASE.jar!/:1.3.0.RELEASE]
accountservice_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1112) [spring-boot-1.3.0.RELEASE.jar!/:1.3.0.RELEASE]
accountservice_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1101) [spring-boot-1.3.0.RELEASE.jar!/:1.3.0.RELEASE]
accountservice_1 | at net.twentyfourseven.account.AccountServiceApplication.main(AccountServiceApplication.java:44) [account-service.jar!/:0.0.1-SNAPSHOT]
accountservice_1 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_66-internal]
accountservice_1 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_66-internal]
accountservice_1 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_66-internal]
accountservice_1 | at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_66-internal]
accountservice_1 | at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53) [account-service.jar!/:0.0.1-SNAPSHOT]
accountservice_1 | at java.lang.Thread.run(Thread.java:745) [na:1.8.0_66-internal]
accountservice_1 | Caused by: java.net.ConnectException: Connection refused
accountservice_1 | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_66-internal]
accountservice_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_66-internal]
accountservice_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_66-internal]
accountservice_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_66-internal]
accountservice_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_66-internal]
accountservice_1 | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_66-internal]
accountservice_1 | at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:214) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:298) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
Flyway db迁移好像有问题……
现在,当我的服务容器退出并且我使用 CTRL-C 退出 docker-compose 并使用 docker-compose up 重新启动时,一切都很好,我的 servive 和 mysql 启动并且 flyway 迁移按预期进行。
有人有想法吗?对我来说没问题,因为我知道第二次启动时一切都会好起来的,但我真的不明白为什么会出现这个错误,这真的很烦人......
这是我的配置,缩写:
flyway:
enabled: true
spring.datasource:
url: jdbc:mysql://localhost:3306/dbaccounts
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
spring.jpa:
database-platform: org.hibernate.dialect.MySQL5Dialect
show-sql: true
generate-ddl: false
hibernate:
ddl-auto: validate
还有我的 docker-compose.yml:
mysqlaccounts:
image: mysql:5.6
ports:
- 3306:3306
environment:
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: dbaccounts
accountservice:
image: account-service
ports:
- 8081:8081
links:
- mysqlaccounts
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://mysqlaccounts:3306/dbaccounts
- SPRING_DATASOURCE_USERNAME=root
- SPRING_DATASOURCE_PASSWORD=root
当我从一开始就禁用 flyway 时,一切都很好......
提前感谢任何想法和建议..;)
P.S:我使用的是 Ubuntu 15.10、Java8、spring-boot 1.3.RELEASE 和 Docker 1.9.0 以及 docker-compose 版本:1.5.1。
【问题讨论】:
我也遇到了同样的问题,请问哪里可以解决?或者它是最新撰写中的错误? 对不起,还没有找到解决办法... @MarkusLamm 你能发布解决方案吗? 【参考方案1】:您应该将datasource url
中的主机替换为docker-machine
的ip。它看起来像:
jdbc:mysql://192.168.99.100/dbaccounts
【讨论】:
以上是关于无法将docker Spring-Boot应用程序与docker-compose中的mysql容器和flyway连接起来的主要内容,如果未能解决你的问题,请参考以下文章
使用docker-compose时无法访问docker容器内的spring-boot rest-endpoint
如何将 Spring-Boot Web 服务转换为 Docker 映像?
无法将 spring-boot 2 服务连接到不同容器中的 mysql