Spring Boot Dockerize项目和Redis容器获取java.net.ConnectException:连接被拒绝(连接被拒绝)

我正在使用Dockerize春季启动应用程序,并在另一个容器中进行Redis。我以这种方式运行redis容器:docker run -d --name redis -p 6379:6379 redis

当我从id运行我的应用程序时,我没有任何问题,并且我的应用程序能够连接到Redis。

但是,当我将我的应用作为容器运行时:docker run -p 8080:8080 shortenurl,我遇到了下一个问题:java.net.connectexception: Connection refused (Connection refused)

这是我的pom.xml:

<groupId>com.neueda.shorturl</groupId>
    <artifactId>shortenurl</artifactId>
    <version>0.0.1-snAPSHOT</version>
    <packaging>jar</packaging>

    <name>shortenurl</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>shortenurl</finalName>
    </build>

这是我的application.yml配置:

spring:
  application:
    name: shortenurl-neueda-assignment
  redis:
    host: localhost
    port: 6379

有什么想法吗?

谢谢。

hahahaha548 回答:Spring Boot Dockerize项目和Redis容器获取java.net.ConnectException:连接被拒绝(连接被拒绝)

那是因为,一旦在两个不同的容器中运行应用程序,它们便不再位于同一网络中。除非您位于容器中,否则您将无法访问localhost或任何回送IP。容器内只有本地主机。但是,如果两个容器都在同一网络中,则可以按其容器名称访问容器。

因此,您应该编排一个docker网络并在该网络中部署两个容器,然后每个其他能够通过其容器名称解析主机的容器。

尝试一下;

h = "a"
<<h::binary>>
"a"

并在$ docker network create test-netw $ docker run --net test-netw -d --name redis -p 6379:6379 redis 文件中更改Redis主机;

aplication.yml

也添加此配置;

spring:
  redis:
    host: redis
    port: 6379
    jedis:
      pool:
        max-active: 7
        max-idle: 7
        min-idle: 2

而不是使用它;

@Bean
JedisPool jedisPool(RedisProperties redisProperties) {
    return new JedisPool(new JedisPoolConfig(),redisProperties.getHost(),redisProperties.getPort());
}

使用这个;

Jedis jedis = new Jedis();

最后一步是;

Jedis jedis = pool.getResource();
本文链接:https://www.f2er.com/3168805.html

大家都在问