Payara Docker 数据源连接问题

Payara Docker datasource connection problems

我开发了一个简单的 JAKARTAEE 网络应用程序并使用 netbeans 将其部署到 payara。我已经通过 docker 设置了一个 mysql 数据库并且它工作正常。现在我正在尝试使用 docker-compose.yml 文件来设置 mysql 和 payara 服务器。问题是,当我尝试将我的 payara-server 容器化时,我似乎无法访问数据库...

我的 docker-compose.yml 看起来如下:

version: '3'

services:
  docker-mysql:
    container_name: docker-mysql
    restart: always
    image: mysql:8
    environment:
      MYSQL_DATABASE: helloworld
      MYSQL_ROOT_PASSWORD: snoopy
      MYSQL_ROOT_HOST: '%'
      MYSQL_USER: snoopy
      MYSQL_PASSWORD: snoopy
    expose:
      - "3306"
    ports:
      - "3306:3306"
    networks:
      - backend

  helloworld-javaee-app:
    container_name: helloworld-javaee-app
    restart: on-failure
    build: ./
    expose:
      - "8080"
      - "4848"
    ports:
      - "8080:8080"
      - "4848:4848"
    environment:
      DATASOURCE_URL: jdbc:mysql://docker-mysql:3306
      DATASOURCE_USERNAME: root
      DATASOURCE_PASSWORD: snoopy
      DATASOURCE_DATABASE: helloworld
    depends_on:
      - docker-mysql
    networks:
      - backend
networks:
    backend:

我的 Dockerfile 如下所示:

#
# Build stage
#
FROM maven:3.6.0-jdk-11-slim AS build
WORKDIR /
COPY . /home/app
RUN mvn -f /home/app/pom.xml clean package

#
# Package stage
#
FROM payara/server-full
COPY --from=build /home/app/target/*.war $DEPLOY_DIR

我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
  <persistence-unit name="payaraHibernate" transaction-type="JTA">
      <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
      <jta-data-source>java:app/jdbc/hibernate</jta-data-source>
      <properties>
          <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
          <property name="hibernate.hbm2ddl.auto" value="create" />
          <property name="hibernate.show_sql" value="true" />
          <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
      </properties>
    </persistence-unit>
</persistence>

我的玻璃鱼-resources.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
    <jdbc-resource enabled="true" 
                   pool-name="MySQLPool" 
                   jndi-name="java:app/jdbc/hibernate"/>
    <jdbc-connection-pool name="MySQLPool" 
                          res-type="javax.sql.DataSource" 
                          datasource-classname="com.mysql.cj.jdbc.MysqlDataSource">
      <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://docker-mysql:3306"/>
      <property name="UseSSL" value="false"></property>
      <property name="User" value="root"></property>
      <property name="Password" value="snoopy"></property>
      <property name="DatabaseName" value="helloworld"></property>
      <property name="AllowPublicKeyRetrieval" value="true"></property>
    </jdbc-connection-pool>
</resources>

有人面临类似的情况吗?我似乎无法找到为什么 payara 无法达到 docker-mysql...

错误:

| 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.

网络检查:

[
    {
        "Name": "helloworld_backend",
        "Id": "0b79d93b27d8c1f51763c4fea90ae9b73fffce7ec0d26fab5d7f1769c3acbf92",
        "Created": "2021-04-30T23:00:18.7217142Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.30.0.0/16",
                    "Gateway": "172.30.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "2dfa3cadc424323cfdc8ea657db11f9bf4fb58ea44303711c5e8a97b6bb618bf": {
                "Name": "helloworld-javaee-app",
                "EndpointID": "9da38f596bcbeafecd34b36d5686aff7cfc6319ce75cdb3519eb891d8f2ed210",
                "MacAddress": "02:42:ac:1e:00:03",
                "IPv4Address": "172.30.0.3/16",
                "IPv6Address": ""
            },
            "e470e2d587ca417d4cbcffc7c038858cf51892274c5c534a088c11a47e8dc566": {
                "Name": "docker-mysql",
                "EndpointID": "e0ef57640d48520b616b6d314bd7306d69ec89efd89a0dba86dfcc3113248445",
                "MacAddress": "02:42:ac:1e:00:02",
                "IPv4Address": "172.30.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "backend",
            "com.docker.compose.project": "helloworld",
            "com.docker.compose.version": "1.29.0"
        }
    }
]

将 glassfish-resources.xml 更改为 payara-resources.xml 并将文件内容更改为:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//Payara.fish//DTD Payara Server 4 Resource Definitions//EN" "https://raw.githubusercontent.com/payara/Payara-Server-Documentation/master/schemas/payara-resources_1_6.dtd">
<resources>
    <jdbc-resource enabled="true" 
                   pool-name="MySQLPool" 
                   jndi-name="java:app/jdbc/hibernate"/>
    <jdbc-connection-pool name="MySQLPool" 
                          res-type="javax.sql.DataSource" 
                          datasource-classname="com.mysql.cj.jdbc.MysqlDataSource">
      <property name="URL" value="jdbc:mysql://docker-mysql:3306/helloworld"/>
      <property name="UseSSL" value="false"></property>
      <property name="User" value="snoopy"></property>
      <property name="Password" value="snoopy"></property>
      <property name="AllowPublicKeyRetrieval" value="true"></property>
    </jdbc-connection-pool>
</resources>

它奏效了...将它留在这里供遇到相同问题的任何人使用。我认为问题是 root 用户,尽管我将 MYSQL_ROOT_HOST 指定为 %