应用服务器已在Docker容器中运行,但无法使用PostgreSQL驱动。连接被拒绝。

huangapple go评论81阅读模式
英文:

The application server has been running in docker container could not use postgresql dirver.Connection refused

问题

我正在尝试从第一个容器使用JDBC连接到第二个容器上的Postgresql数据库,但在尝试建立数据库连接时发生了错误。

conn = DriverManager.getConnection(url, user, password);

我将war文件部署到了运行Docker容器的Wildfly中。当我调用connect()方法时,出现了错误。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.corsan</groupId>
    <artifactId>corsan</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.16</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Dockerfile

FROM jboss/wildfly
ADD corsan.war /opt/jboss/wildfly/standalone/deployments/

用于JDBC连接的Java代码:

private final String url = "jdbc:postgresql://localhost:5432/postgres";
private final String user = "postgres";
private final String password = "1234";

public Connection connect() {
    Connection conn = null;
    try {
        conn = DriverManager.getConnection(url, user, password);
        System.out.println("Connected to the PostgreSQL server successfully.");
    } catch (SQLException e) {
        System.out.println("problem while making connection");
        System.out.println(e.getMessage());
    }

    return conn;
}

在Wildfly启动时,一直有一个警告,我认为这与连接被拒绝的错误有关:

无法索引类org/postgresql/jdbc/PgConnection$1.class位于/content/corsan.war/WEB-INF/lib/postgresql-42.2.16.jar:java.lang.IllegalStateException:缺少所需的类信息

这两个图片显示了IntelliJ项目结构和具有postgres驱动程序库的artifact。

连接到localhost:5432被拒绝。请检查主机名和端口是否正确,并且postmaster是否接受TCP/IP连接。
22:51:20.921 ERROR [io.undertow.request] (default task-1)
UT005023: 处理对/corsan/的请求时出现异常:java.lang.NullPointerException

当我直接将war部署到本地同一应用服务器时,一切正常。但当我使用Docker容器时,出现了这个问题。非常感谢您提供任何提示或帮助。

PostgreSQL和应用服务器在本地机器上的不同容器中运行。
Docker ps:

应用服务器端口:0.0.0.0:8080->8080/tcp
PostgreSQL:0.0.0.0:5432->5432/tcp

英文:

I am trying to use JDBC from first container to connect to Postgresql database on secend container .the error happens while trying to make a database connection.

conn = DriverManager.getConnection(url, user, password);

I deploy war file into wildfly that is docker container.
when I call the connect() method the error occur.
pom.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;org.corsan&lt;/groupId&gt;
    &lt;artifactId&gt;corsan&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;javax&lt;/groupId&gt;
            &lt;artifactId&gt;javaee-web-api&lt;/artifactId&gt;
            &lt;version&gt;7.0&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
            &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
            &lt;version&gt;4.0.1&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
            &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
            &lt;version&gt;42.2.16&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;


    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.8.0&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;release&gt;11&lt;/release&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

&lt;/project&gt;

docker file:

FROM jboss/wildfly
ADD corsan.war /opt/jboss/wildfly/standalone/deployments/

the Java code for JDBC connection:


private final String url = &quot;jdbc:postgresql://localhost:5432/postgres&quot;;
private final String user = &quot;postgres&quot;;
private final String password = &quot;1234&quot;;


    public Connection connect() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, user, password);
            System.out.println(&quot;Connected to the PostgreSQL server successfully.&quot;);
        } catch (SQLException e) {
            System.out.println(&quot;problem while making connection&quot;);
            System.out.println(e.getMessage());
        }

        return conn;
    }

and while the wildfly is lunching there is always this warning that I guess is relevant to the connection refused Error:

> Could not index class org/postgresql/jdbc/PgConnection$1.class at
> /content/corsan.war/WEB-INF/lib/postgresql-42.2.16.jar:
> java.lang.IllegalStateException: Required class information is missing

these two picture are intellij project structure and artifact that has postgres driver library in its lib.
Project structure
Project structure Artifact

Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
22:51:20,921 ERROR [io.undertow.request] (default task-1) 
  UT005023: Exception handling request to /corsan/: java.lang.NullPointerException  

when I deploy the war directly to the same application server in local it works fine. But when I use docker container this issue occure. I appreciate for any hint or help.

Postgresql and application server are running in separate container in local machine.
docker ps :

> app server port: 0.0.0.0:8080->8080/tcp
> PG: 0.0.0.0:5432->5432/tcp

答案1

得分: 3

The 应用服务器PostgreSQL数据库位于两个独立的容器中,因此它们需要连接在一起。这个链接有助于在两个独立的容器之间建立网络。此外,Portainer可以用作GUI工具,将不同的容器连接在一起。
在这两个容器拥有相同网络之后,我们可以使用以下命令检查,以确保两个容器都在使用同一个网络(corsannet是我使用的网络名称)。

docker network inspect corsannet

以确保两个容器都在使用同一个网络。

另一个问题是对于JDBC URL,我们不能使用<s>localhost</s>或域名,而是应该使用数据库容器的名称,例如在这个示例中是pg

private final String url = "jdbc:postgresql://pg:5432/postgres";
英文:

The application server and Postgresql database are in two separate containers so they need to be connected together. This link help to make network between two separate container.Also Portainer can be used as GUI tools for connecting different containers together.
After these two containers have a same network we can check it with this command to ensure that the both containers are using the same network(corsannet is the name of network that I used).

docker network inspect corsannet

To ensure that the both containers are using the same network.

The other issue is for JDBC url we can not use <s>localhost</s> or domain name instead we should use the name of the database container, in this sample is pg.

private final String url = &quot;jdbc:postgresql://pg:5432/postgres&quot;;

huangapple
  • 本文由 发表于 2020年10月13日 07:19:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64326533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定