我在 SpringBoot 中有一个微服务(产品服务),在 MySQL 中有一个数据库,并且希望在 Docker 中运行我的应用程序
我可以在本地运行该服务,没有任何错误。但是当我启动 Docker (docker-compose up --build) 时,它显示了一些连接错误。
我在 application.properties、Dockerfile、docker-compose、pom.xml 和错误中显示了我的代码。经过一番修改和询问朋友,错误依然存在。如果有人能帮助我,我将不胜感激:)
在 MySQL Workbench 中,我设置了一个与 URL localhost:3306
的连接,并在其中创建了一个 testdb
模式。
这是我的application.properties
spring.datasource.url= jdbc:mysql://localhost:3306/testdb spring.datasource.username= abc spring.datasource.password= def spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect #Hibernate ddl auto (create, create-drop, validate, update) spring.jpa.hibernate.ddl-auto= update
这是 Dockerfile
FROM maven:3.8.3-openjdk-17 AS build WORKDIR /app COPY mvnw . COPY .mvn .mvn COPY pom.xml . COPY src src RUN mvn install -DskipTests RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar) FROM openjdk:17-jdk-slim VOLUME /tmp ARG DEPENDENCY=/app/target/dependency COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app ENTRYPOINT ["java","-cp","app:app/lib/*","com.example.helloworld.EcommerceApplication"] EXPOSE 8080
这是 docker-compose.yml
version: "3.9" services: product-service: container_name: product-service build: context: . dockerfile: Dockerfile links: - database depends_on: - database environment: - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/testdb #- SPRING_DATASOURCE_USERNAME=abc #- SPRING_DATASOURCE_PASSWORD=def - SPRING_JPA_PROPERTIES_HIBERNATE_DIALECT=org.hibernate.dialect.MySQL5InnoDBDialect - SPRING_JPA_HIBERNATE_DDL-AUTO=update - SPRING_JPA_SHOW-SQL= true ports: - 8080:8080 networks: - internal database: platform: linux/x86_64 image: mysql:8.0 container_name: mysql #ports: # - "3306:3307" environment: MYSQL_DATABASE: testdb MYSQL_ROOT_USER: abc MYSQL_ROOT_PASSWORD: def networks: - internal networks: internal: name: internal
这是我在 pom.xml 中的依赖项
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test mysql mysql-connector-java 8.0.30 runtime org.projectlombok lombok 1.18.24 provided org.springframework spring-jdbc 6.0.5 org.springdoc springdoc-openapi-ui 1.6.12 org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-validation com.h2database h2 junit junit test org.hibernate.validator hibernate-validator 8.0.0.Final javax.xml.bind jaxb-api 2.3.1 org.javassist javassist 3.25.0-GA
我总结了这些代码中的错误
产品服务 |引起原因:com.mysql.cj.exceptions.CJCommunicationsException:通信链路故障
`product-service | Caused by: java.net.ConnectException: Connection refused` `product-service | 2023-03-14 08:45:26.585 WARN 1 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/a utoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution` `product-service | org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/b oot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution` `product-service | Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution ` `product-service | Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution ` `product-service | 2023-03-14 08:50:17.188 ERROR 1 --- [ main] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution ` `product-service | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. `
产品服务 | ...省略52个常见框架
Dans le fichier docker-compose, votre conteneur de base de données est nommé
database
et le même nom doit être utilisé comme nom d'hôte lors de l'accès à ce conteneur sur le réseauau lieu de