redis / jedis

Redis Java client
MIT License
11.86k stars 3.87k forks source link

Loopback host address ('localhost','127.0.0.1', etc.) resolves to network IP #1367

Closed tonyghita closed 3 years ago

tonyghita commented 8 years ago

Expected behavior

I have my Jedis client configured to connect to localhost. I expect the connection to be made to the localhost via loopback.

Actual behavior

As of the changes in https://github.com/xetorthio/jedis/pull/1342, when specifying a localhost address, it now resolves to my machine's network ip address.

This makes it impossible to connect to a redis-sentinel on localhost because the sentinel only allows connections from loopback on more recent versions of Redis.

Steps to reproduce:

  1. Initialize a Jedis 2.9.+ client with the host 'localhost'
  2. Unable to connect to local redis-sentinel (version 3.2.2) because 'localhost' resolved to an external ip (i.e. 10.1.10.254).

I think the new behavior in getLocalHostQuietly() introduces system-dependent behavior via InetAddress.getLocalHost().getHostAddress(), which does not seem to be the intent of the code.

Redis / Jedis Configuration

Jedis version:

2.9.x (since https://github.com/xetorthio/jedis/pull/1342)

Redis version:

3.2.2

Java version:

java version "1.8.0_40"
Java(TM) SE Runtime Environment (build 1.8.0_40-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)
zbstof commented 7 years ago

Same problem here. We use spring boot with spring-boot-starter-data-redis

application.yml:

spring:
  redis:
    sentinel:
      master: webrio-integration-master
      nodes: localhost:26379
    password: secret

Log:

03-Aug-2017 14:51:46 [localhost-startStop-1] INFO  : redis.clients.jedis.JedisSentinelPool:136     - Trying to find master from available Sentinels...
03-Aug-2017 14:51:46 [localhost-startStop-1] DEBUG : redis.clients.jedis.JedisSentinelPool:141     - Connecting to Sentinel 10.183.160.202:26379
03-Aug-2017 14:19:40 [localhost-startStop-1] WARN  : redis.clients.jedis.JedisSentinelPool:164     - Cannot get master address from sentinel running @ 10.183.160.202:26379.
Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused). Trying next one.
...
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: All sentinels down, cannot determine where is webrio-integration-master master is running...
        at redis.clients.jedis.JedisSentinelPool.initSentinels(JedisSentinelPool.java:180)
        at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:95)
        at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:76)

Changing to spring.redis.sentinel.nodes: 127.0.0.1:26379 doesn't help, as per original post.

marcosnils commented 6 years ago

A quick hack around this is to use 127.0.1.1 instead which shouldn't trigger the getLocalHostQuietly function.

dylankolson commented 5 years ago

For those trying this on OSX https://stackoverflow.com/a/56082275/6190803

yuriyz commented 4 years ago

One of possible workarounds is to set it explicitly

HostAndPort.setLocalhost("127.0.0.1");

then jedis will not try to resolve it.

sazzad16 commented 3 years ago

From Jedis 3.6.0 (due to #2445), there are two new constructors with Set<HostAndPort> sentinels param. These constructors will not convert the hosts and treat the hosts as they are provided.