Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';t连接到testcontainer Postgres实例_Java_Spring_Postgresql_Spring Boot_Testcontainers - Fatal编程技术网

Java Can';t连接到testcontainer Postgres实例

Java Can';t连接到testcontainer Postgres实例,java,spring,postgresql,spring-boot,testcontainers,Java,Spring,Postgresql,Spring Boot,Testcontainers,我已经使用testcontainers创建了一个Postgres实例。容器已启动,但我无法访问它 我已经尝试使用DBeaver在容器化数据库连接。 在eclipse控制台中,一切似乎都很好: 01:29:34.662[main]调试com.github.dockerjava.core.command.absrdockercmd-Cmd:com.github.dockerjava.core.command。CreateContainerCmdImpl@73386d72[name=,hostName

我已经使用testcontainers创建了一个Postgres实例。容器已启动,但我无法访问它

我已经尝试使用DBeaver在容器化数据库连接。 在eclipse控制台中,一切似乎都很好:

01:29:34.662[main]调试com.github.dockerjava.core.command.absrdockercmd-Cmd:com.github.dockerjava.core.command。CreateContainerCmdImpl@73386d72[name=,hostName=,domainName=,user=,attachStdin=,attachStdout=,attachStderr=,portSpecs=,tty=,stdinOpen=,stdInOnce=,env={POSTGRES\u user=test,POSTGRES\u PASSWORD=test,POSTGRES\u DB=ASIGDB\u test}

这是我的密码:

public class CustomPostgresContainer extends PostgreSQLContainer<CustomPostgresContainer>{
    private static final String IMAGE_VERSION = "postgres:9.6";
    private static CustomPostgresContainer customPostgresContainer;
    private static final int EXPOSED_PORT = 5555;
    private static final String DB_NAME = "ASIGDB_TEST";
    private static final String DB_USER= "test";
    private static final String DB_PASSWORD= "test";


    public CustomPostgresContainer() {
        super(IMAGE_VERSION);
    }

    public static CustomPostgresContainer getCustomPostgresContainerInstance() {
        if(customPostgresContainer == null) {
            return extracted().withExposedPorts(EXPOSED_PORT)
                                                .withDatabaseName(DB_NAME)
                                                .withUsername(DB_USER)
                                                .withPassword(DB_PASSWORD);
        }

        return customPostgresContainer;
    }

    private static CustomPostgresContainer extracted() {
        return new CustomPostgresContainer();
    }

    @Override
    public void start() {
        super.start();
    }

    @Override
    public void stop() {
        //do nothing, JVM handles shut down
    }
}
公共类CustomPostgresContainer扩展了PostgreSQLContainer{
私有静态最终字符串IMAGE_VERSION=“postgres:9.6”;
私有静态CustomPostgresContainer CustomPostgresContainer;
专用静态最终int暴露_端口=5555;
私有静态最终字符串DB_NAME=“ASIGDB_TEST”;
私有静态最终字符串DB_USER=“test”;
私有静态最终字符串DB_PASSWORD=“test”;
公共客户PostgresContainer(){
超级(图片版);
}
公共静态CustomPostgresContainer getCustomPostgresContainerInstance(){
if(customPostgresContainer==null){
返回ExposedPorts(EXPOSED_PORT)的extracted()
.withDatabaseName(数据库名称)
.withUsername(数据库用户)
.带密码(DB_密码);
}
返回customPostgresContainer;
}
私有静态CustomPostgresContainer提取(){
返回新的CustomPostgresContainer();
}
@凌驾
公开作废开始(){
super.start();
}
@凌驾
公共停车场(){
//不执行任何操作,JVM句柄将关闭
}
}
我得到:

与本地主机的连接:5555被拒绝。请检查主机名和端口是否正确,以及邮局主管是否正在接受TCP/IP连接

有人知道发生了什么吗?

根据,
withExposedPorts()
-->这个公开的端口号是从容器的角度来看的。
从主机的角度来看,Testcontainers实际上在一个随机空闲端口上公开了这一点。这是为了避免本地运行的软件或并行测试运行之间可能出现的端口冲突。
由于存在此间接层,因此有必要在运行时向Testcontainers请求实际映射的端口。这可以使用
getMappedPort
方法完成,该方法将原始(容器)端口作为参数:

Integer firstMappedPort = container.getMappedPort(yourExposedPort);<br/>
Integer firstMappedPort=container.getMappedPort(您的ExposedPort);
尝试使用DBeaver连接到第一个出现的端口

根据,
withExposedPorts()
-->此公开端口号是从容器的角度来看的。
从主机的角度来看,Testcontainers实际上在一个随机空闲端口上公开了这一点。这是为了避免本地运行的软件或并行测试运行之间可能出现的端口冲突。
由于存在此间接层,因此有必要在运行时向Testcontainers请求实际映射的端口。这可以使用
getMappedPort
方法完成,该方法将原始(容器)端口作为参数:

Integer firstMappedPort = container.getMappedPort(yourExposedPort);<br/>
Integer firstMappedPort=container.getMappedPort(您的ExposedPort);
尝试使用DBeaver连接到第一个出现的端口


谢谢Vitalii。我确实误解了公开端口的观点。基本上,testcontainers启动的对docker入口点(在该随机端口上公开)的任何请求都会被转发到容器(5432)内运行的默认postgres实例和我添加的公开端口(5555)。再次感谢。感谢Vitalii。我确实误解了公开端口的角度。基本上,testcontainers启动的对docker入口点(在该随机端口上公开)的任何请求都会转发到容器(5432)内运行的默认postgres实例和我添加的公开端口(5555).再次感谢。