我有一个spring-boot 2.16服务,该服务连接到my-sql数据库。只要所有组件都在同一主机上,它就可以按预期工作。我试图改进解决方案,并让一个运行有嵌入式Tomcat的sprin-boot服务的docker容器和另一个运行my-sql服务器的docker容器。 相同的代码不再起作用。例外:
2019-06-28 12:23:48 ERROR com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Exception during pool initialization.
com.mysql.jdbc.exceptions.jdbc4.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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:988)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341)
....
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
... 66 common frames omitted
因此,这就像找不到MySQL服务器IP地址或服务器未运行,等等。 这是docker-compse.yml文件:
version: "2"
services:
rdf-data:
container_name: rdf-data
image: mysql:latest
environment:
MYSQL_DATABASE: test
MYSQL_USER: nicolas
MYSQL_PASSWORD: mysql
MYSQL_ROOT_PASSWORD: mysql
ports:
- 3306:3306
- 33060:33060
hostname: rdf-data
networks:
net1:
ipv4_address: 192.19.0.4
rdf-core:
image: openjdk:8-jdk-alpine
depends_on:
- rdf-data
container_name: rdf-core
links:
- rdf-data:rdf-data
volumes:
- ../docker:/usr/local/share/rdf
ports:
- "8080:8080"
entrypoint: /usr/local/share/rdf/run.sh
hostname: rdf-core
environment:
PROFILE: "default"
SERVER_PORT: "8080"
networks:
net1:
ipv4_address: 192.19.0.8
networks:
net1:
driver: bridge
driver_opts:
com.docker.network.enable_ipv6: "false"
ipam:
driver: default
config:
- subnet: 192.19.0.0/24
gateway: 192.19.0.1
这里有两个容器:IP地址为192.19.0.8的rfd-core和IP地址为192.19.0.4的rdf-data。每个容器都可以ping通另一个。 hst机器能够对每个容器执行ping操作。从主机,我可以使用my-sql命令行客户端正确连接到在rfd-data上运行的my-sql服务器。 但是,下面显示的我的服务连接不起作用:
database=mysql
spring.datasource.url=jdbc:mysql://192.19.0.4/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=mysql
spring.datasource.driver-class=com.mysql.cj.jdbc.MysqlXADataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.hikari.connectionTimeout=30000
spring.datasource.hikari.idleTimeout=600000
spring.datasource.hikari.maxLifetime=1800000
...
有什么我想念的吗?
答案 0 :(得分:0)
尝试在docker-compose
文件的环境部分中指定spring boot应用程序的参数:
rdf-core:
image: openjdk:8-jdk-alpine
depends_on:
- rdf-data
container_name: rdf-core
links:
- rdf-data:rdf-data
volumes:
- ../docker:/usr/local/share/rdf
ports:
- "8080:8080"
entrypoint: /usr/local/share/rdf/run.sh
hostname: rdf-core
environment:
PROFILE: "default"
SERVER_PORT: "8080"
spring.datasource.url=jdbc:mysql://ipaddress/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=mysql
spring.datasource.driver-class=com.mysql.cj.jdbc.MysqlXADataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.hikari.connectionTimeout=30000
spring.datasource.hikari.idleTimeout=600000
spring.datasource.hikari.maxLifetime=1800000
此问题的另一个可能原因:depends_on
不保证依赖项容器已完全运行。因此,如果MySQL容器比Spring Boot容器花费更多的时间来完全启动,则连接将失败。
看看这个:
Spring Boot + docker-compose + MySQL: Connection refused
有几种方法可以使容器等待另一个容器: 使用restart on failure功能或docker compose wait( Github link)