多个 mysql 图像在 Springboot 和 docker 中不起作用

Multiple mysql image not working in Springboot and docker

我是 Springboot 和 docker 的新手。 我正在尝试创建三个不同的微服务并将它们加载到 docker.

其中两个正确启动,而最后一个returns出现以下错误:

Caused by: com.mysql.cj.exceptions.CJException: Access denied for user 'db_user'@'%' to database 'PaymentDB' 

我找不到错误。这三个微服务的配置完全相同(有时我复制并粘贴类似的代码,只是将名称从 Catalog 更改为 Payment。你能给我一些建议吗?

这是application.properties:

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${PM_DATASOURCE_HOST}:${DATASOURCE_PORT}/${PM_DATASOURCE_NAME}?autoReconnect=true
spring.datasource.username=${DATASOURCE_USER}
spring.datasource.password=${DATASOURCE_PASSWORD}
spring.kafka.bootstrap-servers=kafka:9092
kafkaTopic=payment-topic

这是.env :

DB_DATABASE_PAYMENT=PaymentDB
DB_HOST_PAYMENT=paymentmysqldb

DB_DATABASE_USER=UserDB
DB_HOST_USER=usermysqldb

DB_DATABASE_CATALOG=CatalogDB
DB_HOST_CATALOG=catalogmysqldb


DB_USER=db_user
DB_PASSWORD=alx2022
DB_ROOT_PASSWORD=user
DB_PORT=3306

这是docker-compose.yml


version: '3.4'

x-common-variables: &common-variables
  DATASOURCE_USER: ${DB_USER}
  DATASOURCE_PASSWORD: ${DB_PASSWORD}
  DATASOURCE_PORT: ${DB_PORT}

services:
  paymentmysqldb:
    container_name: paymentmysqldb
    image: mysql
    ports:
      - "3313:3306"
    environment:
      - MYSQL_DATABASE=${DB_DATABASE_PAYMENT}
      - MYSQL_USER=${DB_USER}
      - MYSQL_PASSWORD=${DB_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
    volumes:
      - paymentstorage:/var/lib/mysql


  usermysqldb:
    container_name: usermysqldb
    image: mysql
    ports:
      - "3311:3306"
    environment:
      - MYSQL_DATABASE=${DB_DATABASE_USER}
      - MYSQL_USER=${DB_USER}
      - MYSQL_PASSWORD=${DB_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
    volumes:
      - userstorage:/var/lib/mysql

  catalogmysqldb:
    container_name: catalogmysqldb
    image: mysql
    ports:
      - "3312:3306"
    environment:
      - MYSQL_DATABASE=${DB_DATABASE_CATALOG}
      - MYSQL_USER=${DB_USER}
      - MYSQL_PASSWORD=${DB_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
    volumes:
      - catalogstorage:/var/lib/mysql

  paymentmanager:
    container_name: paymentmanager
    image: arausa/paymentimage
    build:
      context: .
      dockerfile: MicroServices/PaymentManager/Dockerfile
    depends_on:
      - paymentmysqldb
    ports:
      - "3333:8080"
    restart: always
    environment:
      <<: *common-variables
      PM_DATASOURCE_HOST: ${DB_HOST_PAYMENT}
      PM_DATASOURCE_NAME: ${DB_DATABASE_PAYMENT}

  usermanager:
    container_name: usermanager
    image: arausa/userimage
    build:
      context: .
      dockerfile: MicroServices/UserManager/Dockerfile
    depends_on:
      - usermysqldb
    ports:
      - "1111:8080"
    restart: always
    environment:
      <<: *common-variables
      UM_DATASOURCE_HOST: ${DB_HOST_USER}
      UM_DATASOURCE_NAME: ${DB_DATABASE_USER}

  catalogmanager:
    container_name: catalogmanager
    image: arausa/catalogimage
    build:
      context: .
      dockerfile: MicroServices/CatalogManager/Dockerfile
    depends_on:
      - catalogmysqldb
    ports:
      - "2222:8080"
    restart: always
    environment:
      <<: *common-variables
      CM_DATASOURCE_HOST: ${DB_HOST_CATALOG}
      CM_DATASOURCE_NAME: ${DB_DATABASE_CATALOG}



#kafka usa zookeeper tiene traccia dei broker, topologia della network e info per la sincronizzazione
  zookeeper:
    image: wurstmeister/zookeeper

#identifica il broker kafka
  kafka:
    image: wurstmeister/kafka
    ports:
      - "9092:9092"  #porta di default per il broker kafka
    environment:
      KAFKA_ADVERTISED_HOST_NAME: kafka
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181  #serve a dire dove sta girando zookeeper

volumes:
  userstorage:
  catalogstorage:
  paymentstorage:

这是 pom.xml :

<?xml version="1.0" encoding="UTF-8"?>

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">

<modelVersion>4.0.0</modelVersion>


<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.6.4</version>

<relativePath/>

<!-- lookup parent from repository -->


</parent>

<groupId>com.example</groupId>

<artifactId>PaymentManager</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>PaymentManager</name>

<description>PaymentManager</description>


<properties>

<java.version>11</java.version>

</properties>


<dependencies>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-validation</artifactId>

</dependency>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>


<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<scope>runtime</scope>

</dependency>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>


<dependency>

<groupId>org.springframework.kafka</groupId>

<artifactId>spring-kafka</artifactId>

</dependency>


<dependency>

<groupId>com.google.code.gson</groupId>

<artifactId>gson</artifactId>

<version>2.8.5</version>

</dependency>

</dependencies>


<build>


<plugins>


<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>


<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.12.4</version>


<configuration>

<skipTests>true</skipTests>

</configuration>

</plugin>

</plugins>

</build>

</project>

这是 docker 文件:

FROM maven:3-openjdk-17-slim as builder
WORKDIR /project
COPY MicroServices/PaymentManager/ .
RUN mvn package

FROM openjdk:17-alpine
WORKDIR /app
COPY --from=builder /project/target/PaymentManager-0.0.1-SNAPSHOT.jar ./PaymentManager.jar
CMD java -jar PaymentManager.jar

您的 application.properties 似乎有一些错误。

spring.datasource.username=${DATASOURCE_USER}
spring.datasource.password=${DATASOURCE_PASSWORD}

这些环境变量未在任何地方定义或导出。

我用奇怪的方式解决了

我到处用“支付”一词修改“付款”一词,它神奇地起作用了。就算不明白原因