无法连接到在docker容器中的不同系统上运行的docker和spring boot服务器的不同系统中的redis

时间:2019-11-12 10:38:58

标签: spring-boot docker redis

获取异常java.net.ConnectException:当我尝试连接到Redis服务器时,连接被拒绝。

这是我刚才提到要连接的redis属性

spring.redis.database=2 spring.redis.host=172.15.5.100

//这是运行Redis容器的我的系统IP地址

spring.redis.port=6380 `spring.redis.sentinel.master= redis

这是我的docker-compose-yml文件

services:
    server:
      container_name: server
      build: .
      ports: 
          - "8080:8080"
          - "3000:3000"
      depends_on:
          - redis_presence_system

    redis_presence_system:
      container_name: redis
      image: "redis:4.0.9" 
      hostname: redis
      ports:
        - "6380:6380"
      expose:
        - "6380"
      volumes:
         - ./redis-data:/data    
      restart: always``


Error message which i'm getting
java.net.ConnectException: Connection refused at redis.clients.jedis.Connection.connect(Connection.java:207) [jedis-2.9.0.jar:] at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93) [jedis-2.9.0.jar:] at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767) [jedis-2.9.0.jar:] at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106) [jedis-2.9.0.jar:] at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:888) [commons-pool2-2.4.3.jar:2.4.3] at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:432) [commons-pool2-2.4.3.jar:2.4.3] at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:361) [commons-pool2-2.4.3.jar:2.4.3] at redis.clients.util.Pool.getResource(Pool.java:49) [jedis-2.9.0.jar:] ... 27 more Caused by: java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) [rt.jar:1.7.0_80] at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) [rt.jar:1.7.0_80] at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) [rt.jar:1.7.0_80] at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) [rt.jar:1.7.0_80] at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) [rt.jar:1.7.0_80] at java.net.Socket.connect(Socket.java:579) [rt.jar:1.7.0_80] at redis.clients.jedis.Connection.connect(Connection.java:184)

1 个答案:

答案 0 :(得分:0)

ports:中的第二个数字必须与容器内正在侦听的端口相匹配。这通常是您正在运行的服务的“普通”端口;如果是Redis,则使用端口6379。因此,更改

ports:
  - "6380:6379"

(您不需要设置container_name:hostname:expose:;这些实际上并没有设置与大多数应用程序相关的任何属性,并且有合理的默认值对于所有的人。)

在两个容器之间进行连接时,需要使用内部主机名(services:块的名称)和进程本身正在侦听的端口号进行配置。 ports:无效,实际上并不需要仅在容器之间进行连接。在您的应用程序中,您还需要设置

spring.redis.port=6379
spring.redis.sentinel.master=redis_presence_system

匹配标准Redis端口号以及Compose YAML文件中的Redis服务块的名称。