[英]The application server has been running in docker container could not use postgresql dirver.Connection refused
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.我正在尝试使用第一个容器中的 JDBC 连接到第二个容器上的 Postgresql 数据库。尝试建立数据库连接时发生错误。
conn = DriverManager.getConnection(url, user, password);
I deploy war file into wildfly that is docker container.我将war文件部署到docker容器的wildfly中。 when I call the connect() method the error occur.当我调用 connect() 方法时发生错误。
pom.xml 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>
docker file :泊坞窗文件:
FROM jboss/wildfly
ADD corsan.war /opt/jboss/wildfly/standalone/deployments/
the Java code for JDBC connection: 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;
}
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无法在 /content/corsan.war/WEB-INF/lib/postgresql-42.2.16.jar 处索引类 org/postgresql/jdbc/PgConnection$1.class:java.lang.IllegalStateException:缺少所需的类信息
these two picture are intellij project structure and artifact that has postgres driver library in its lib.这两张图片是intellij项目结构和在其lib中有postgres驱动程序库的工件。
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.但是当我使用 docker 容器时会出现这个问题。 I appreciate for any hint or help.我感谢任何提示或帮助。
Postgresql and application server are running in separate container in local machine. Postgresql 和应用程序服务器在本地机器的单独容器中运行。 docker ps :码头工人:
app server port: 0.0.0.0:8080->8080/tcp应用服务器端口:0.0.0.0:8080->8080/tcp
PG: 0.0.0.0:5432->5432/tcp PG:0.0.0.0:5432->5432/tcp
The application server and Postgresql database are in two separate containers so they need to be connected together.应用服务器和Postgresql 数据库位于两个独立的容器中,因此需要将它们连接在一起。 This link help to make network between two separate container.Also Portainer can be used as GUI tools for connecting different containers together.此链接有助于在两个独立的容器之间建立网络。Portainer也可以用作 GUI 工具将不同的容器连接在一起。
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).在这两个容器具有相同的网络后,我们可以使用此命令进行检查以确保两个容器使用相同的网络(corsannet 是我使用的网络名称)。
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 localhost or domain name instead we should use the name of the database container, in this sample is pg .另一个问题是对于JDBC url,我们不能使用localhost或域名,而应该使用数据库容器的名称,在此示例中为pg 。
private final String url = "jdbc:postgresql://pg:5432/postgres";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.