【问题标题】:Spring Boot Dockerize project and redis container getting java.net.ConnectException: Connection refused (Connection refused)Spring Boot Dockerize项目和redis容器获取java.net.ConnectException:连接被拒绝(连接被拒绝)
【发布时间】:2020-02-29 10:40:25
【问题描述】:

我正在另一个容器中使用 Dockerize spring boot 应用程序和 redis。我是这样运行redis容器的:docker run -d --name redis -p 6379:6379 redis

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

但是当我将我的应用程序作为容器运行时:docker run -p 8080:8080 shorturl,我遇到了下一个问题: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

有什么想法吗?

谢谢。

【问题讨论】:

标签: spring-boot redis jedis docker-image


【解决方案1】:

这是因为,一旦您在两个不同的容器中运行应用程序,它们就不再位于同一个网络中。除非您在容器中,否则您无法访问 localhost 或任何环回 IP。容器内只有 localhost。但是,如果两个容器都在同一个网络中,您可以通过容器名称访问容器。

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

试试这个;

$ docker network create test-netw
$ docker run --net test-netw -d --name redis -p 6379:6379 redis

并更改aplication.yml文件中的Redis主机;

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();

最后一步是;

$ docker run -p 8080:8080 --net test-netw shortenurl

【讨论】:

  • 我遇到了同样的问题。
  • 能否请您在此处插入完整的错误消息@rasilvap?
  • 我已经添加了完整的错误信息,但还是和以前一样,谢谢!
  • 我编辑了答案并添加了一个连接池以防止 Jedis 使用默认配置“localhost”。 @rasilvap
猜你喜欢
  • 1970-01-01
  • 2020-01-19
  • 1970-01-01
  • 2023-03-21
  • 2015-05-18
  • 2018-06-10
  • 2017-08-10
  • 2021-06-27
相关资源
最近更新 更多