简体   繁体   English

应用服务器已经在docker容器中运行,无法使用postgresql dirver.Connection denied

[英]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.

相关问题 连接被拒绝:访问在Docker容器中运行的Spring Boot应用程序 - Connection refused: accessing a spring boot application running in docker container Docker PostgreSQL中的连接被拒绝 - Connection refused in Docker PostgreSQL Rabbitmq连接被拒绝,在Linux服务器上的docker容器中运行 - Rabbitmq connection refused , runs inside docker container on a linux server Docker容器中数据库容器和docker中的java容器之间的连接被拒绝 - Connection refused in Docker container between database container and java container in docker Postgresql docker 容器连接失败 - Postgresql docker container connection failure 无法在 docker 容器之间进行通信 - 连接被拒绝 - cannot communicate between docker container - connection refused 如何使用 docker 容器内我本地机器上运行的 MySQL 服务器用于 DropWizard 应用程序? - How to use the MySQL server running on my local machine inside the docker container for a DropWizard application? 无法连接到服务器:连接被拒绝 (0x0000274D/10061) PostgreSQL - could not connect to server: Connection refused (0x0000274D/10061) PostgreSQL 连接拒绝从 docker 连接到 Elasticsearch docker 容器 - Connection Refused connecting from docker to Elasticsearch docker container Netbeans GlassFish Server 4,部署,连接被拒绝:connect,false,模块尚未部署。 请参阅服务器日志以获取详细信息 - Netbeans GlassFish Server 4, deploy, Connection refused: connect, false , The module has not been deployed. See the server log for details
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM