Pessoal, estou dando meus primeiros passos com docker e microservices e estou enfrentando um problema de estabelecer uma conexão entre o serviço e o banco de dados. Os demais serviços que foram dockerizados estão funcionando normalmente. O problema está com o serviço "buscar-cep-service" que não consegue estabelecer uma conexão com o banco de dados.
dockerfile:
FROM openjdk:17-jdk-slim
ARG JAR_FILE=02-buscar-cep-service/target/*.jar
COPY ${JAR_FILE} app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
buscar-cep-service (serviço que está dando problema)
buscar-cep-service:
image: henriquedev/buscar-cep-service
build:
context: .
dockerfile: 02-buscar-cep-service/Dockerfile
environment:
TZ: America/Sao_Paulo
EUREKA.CLIENT.SERVICEURL.DEFAULTZONE: http://eureka-server:8761/eureka
SPRING.ZIPKIN.BASEURL: http://zipkin-server:9411/
SPRING.DATASOURCE.URL: jdbc:mysql://cep-db:3309/cidades?useSSL=false&serverTimezone=UTC&enabledTLSProtocols=TLSv1.2
SPRING.DATASOURCE.USERNAME: root
SPRING.DATASOURCE.PASSWORD: henrique123
SPRING.FLYWAY.URL: jdbc:mysql://cep-db:3309/cidades?useSSL=false&serverTimezone=UTC&enabledTLSProtocols=TLSv1.2
SPRING.FLYWAY.USER: root
SPRING.FLYWAY.PASSWORD: henrique123
ports:
- 8000:8000
depends_on:
- eureka-server
- cep-db
networks:
- henrique-service
docker-compose.yml
version: '3.4'
services:
eureka-server:
image: henriquedev/eureka-server:0.0.1-SNAPSHOT
ports:
- 8761:8761
networks:
- henrique-service
cep-db:
image: mysql:5.7.22
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
TZ: America/Sao_Paulo
MYSQL_ROOT_PASSWORD: henrique123
MYSQL_USER: root
MYSQL_PASSWORD: henrique123
MYSQL_DATABASE: cidades
MYSQL_ROOT_HOST: '%'
MYSQL_TCP_PORT: 3309
ports:
- 3309:3309
expose:
- 3309
zipkin-server:
image: openzipkin/zipkin:2.23.16
ports:
- 9411:9411
networks:
- henrique-service
api-gateway:
image: henriquedev/api-gateway:0.0.1-SNAPSHOT
ports:
- 8765:8765
depends_on:
- eureka-server
environment:
EUREKA.CLIENT.SERVICEURL.DEFAULTZONE: http://eureka-server:8761/eureka
SPRING.ZIPKIN.BASEURL: htt://zipkin-server:9411/
networks:
- henrique-service
buscar-cep-service:
image: henriquedev/buscar-cep-service
build:
context: .
dockerfile: 02-buscar-cep-service/Dockerfile
environment:
TZ: America/Sao_Paulo
EUREKA.CLIENT.SERVICEURL.DEFAULTZONE: http://eureka-server:8761/eureka
SPRING.ZIPKIN.BASEURL: http://zipkin-server:9411/
SPRING.DATASOURCE.URL: jdbc:mysql://cep-db:3309/cidades?useSSL=false&serverTimezone=UTC&enabledTLSProtocols=TLSv1.2
SPRING.DATASOURCE.USERNAME: root
SPRING.DATASOURCE.PASSWORD: henrique123
SPRING.FLYWAY.URL: jdbc:mysql://cep-db:3309/cidades?useSSL=false&serverTimezone=UTC&enabledTLSProtocols=TLSv1.2
SPRING.FLYWAY.USER: root
SPRING.FLYWAY.PASSWORD: henrique123
ports:
- 8000:8000
depends_on:
- eureka-server
- cep-db
networks:
- henrique-service
networks:
henrique-service:
driver: bridge
pilha de erro do buscar-cep-service
`----------------------------------------------------`
`SQL State : 08S01`
`Error Code : 0`
`Message : 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.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:67) ~[flyway-core-8.0.5.jar!/:na]`
`at org.flywaydb.core.internal.jdbc.JdbcConnectionFactory.<init>(JdbcConnectionFactory.java:74) ~[flyway-core-8.0.5.jar!/:na]`
`at org.flywaydb.core.FlywayExecutor.execute(FlywayExecutor.java:143) ~[flyway-core-8.0.5.jar!/:na]`
`at org.flywaydb.core.Flyway.migrate(Flyway.java:124) ~[flyway-core-8.0.5.jar!/:na]`
`at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66) ~[spring-boot-autoconfigure-2.6.3.jar!/:2.6.3]`
`at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.15.jar!/:5.3.15]`
`at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.15.jar!/:5.3.15]`
`... 26 common frames omitted`
`Caused by: 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 com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]`
`at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]`
`at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:829) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]`
`at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:449) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]`
`at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:242) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]`
`at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]`
`at org.springframework.jdbc.datasource.SimpleDriverDataSource.getConnectionFromDriver(SimpleDriverDataSource.java:144) ~[spring-jdbc-5.3.15.jar!/:5.3.15]`
`at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:205) ~[spring-jdbc-5.3.15.jar!/:5.3.15]`
`at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:169) ~[spring-jdbc-5.3.15.jar!/:5.3.15]`
`at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:52) ~[flyway-core-8.0.5.jar!/:na]`
`... 32 common frames omitted`
`Caused by: com.mysql.cj.exceptions.CJCommunicationsException: 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 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]`
`at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[na:na]`
`at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]`
`at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]`
`at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]`
`at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]`
`at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]`
`at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]`
`at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]`
`at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]`
`at com.mysql.cj.NativeSession.connect(NativeSession.java:120) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]`
`at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:949) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]`
`at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:819) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]`
`... 39 common frames omitted`
`Caused by: java.net.UnknownHostException: cep-db: Temporary failure in name resolution`
`at java.base/java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[na:na]`
`at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:933) ~[na:na]`
`at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1519) ~[na:na]`
`at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:852) ~[na:na]`
`at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1509) ~[na:na]`
`at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1367) ~[na:na]`
`at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1301) ~[na:na]`
`at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:133) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]`
`at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) ~[mysql-connector-java-8.0.28.jar!/:8.0.28]`
`... 42 common frames omitted`