[英]Java client can't connect to Redis Sentinel which is running on localhost
I have setup 3 Redis Server and 3 Redis Sentinel instances in my localhost.我在我的本地主机中设置了 3 个 Redis 服务器和 3 个 Redis Sentinel 实例。 The servers are running at:
服务器运行在:
127.0.0.1:6379 // Master
127.0.0.1:6380 // Slave
127.0.0.1:6381 // Slave
and the sentinels are running at:哨兵正在运行:
127.0.0.1:5000
127.0.0.1:5001
127.0.0.1:5002
I have a (Java) client that tries to connect to one of the sentinels and set the keys in redis server:我有一个(Java)客户端,它尝试连接到其中一个哨兵并在 redis 服务器中设置密钥:
// import statements
public class RedisPush {
private static final String MASTER_NAME = "mymaster";
private static final String PASSWORD = "foobared";
private static final Set sentinels;
static {
sentinels = new HashSet();
sentinels.add("127.0.0.1:5000");
sentinels.add("127.0.0.1:5001");
sentinels.add("127.0.0.1:5002");
}
public static void pushToRedis() {
Jedis jedis = null;
try {
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels);
System.out.println("Fetching connection from pool.");
jedis = pool.getResource();
jedis.auth(PASSWORD);
Socket socket = jedis.getClient().getSocket();
System.out.println("Connected to " + socket.getRemoteSocketAddress());
int i = 0;
while (true) {
jedis.set("sentinel_key" + i, "value" + i);
System.out.println(i);
i++;
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
if(jedis != null)
jedis.close();
}
}
public static void main(String[] args) throws Exception {
while(true) {
pushToRedis();
Thread.sleep(1000);
}
}
}
Initially, my sentinel configuration is as follows (for example, the first sentinel running on port 5000
):最初,我的哨兵配置如下(例如第一个哨兵运行在
5000
端口):
bind 127.0.0.1
port 5000
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel auth-pass mymaster foobared
If I try to run my (Java) client, I get the following error:如果我尝试运行我的(Java)客户端,我会收到以下错误:
Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
INFO: Trying to find master from available Sentinels...
Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
WARNING: Cannot get master address from sentinel running @ 192.168.0.102:5001. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused). Trying next one.
Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
WARNING: Cannot get master address from sentinel running @ 192.168.0.102:5000. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused). Trying next one.
Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
WARNING: Cannot get master address from sentinel running @ 192.168.0.102:5002. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused). Trying next one.
redis.clients.jedis.exceptions.JedisConnectionException: All sentinels down, cannot determine where is mymaster 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:82)
at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:70)
at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:44)
at RedisPush.pushToRedis(RedisPush.java:29)
at RedisPush.main(RedisPush.java:61)
However, if I change my sentinel configuration script to the one below:但是,如果我将哨兵配置脚本更改为以下脚本:
# bind 127.0.0.1
port 5000
protected-mode no
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel auth-pass mymaster foobared
the client works perfectly.客户端完美运行。 I don't understand why.
我不明白为什么。
AFAIK, if requirepass
is not set in sentinel.conf
file AND bind
is commented in sentinel.conf
file, ONLY then protected-mode
will be yes
to avoid any client connecting to the sentinel apart from localhost
.据我所知,如果
requirepass
不设置sentinel.conf
文件,并bind
在评论中sentinel.conf
文件,只有protected-mode
将是yes
,以避免连接到定点除任何客户端localhost
。 In my first sentinel configuration, I had the bind
command but still it didn't work.在我的第一个哨兵配置中,我有
bind
命令,但它仍然不起作用。
Why does commenting out bind
and explicitly setting protected-mode
to no
works?为什么注释掉
bind
并将protected-mode
显式设置为no
?
PS I also tried having both bind 127.0.0.1
and protected-mode no
but even that didn't work. PS 我也尝试同时
bind 127.0.0.1
和protected-mode no
但即使这样也没有用。
I had a similar issue where Jedis was throwing the same error, it was related to https://github.com/xetorthio/jedis/issues/1367我有一个类似的问题,其中 Jedis 抛出了同样的错误,它与https://github.com/xetorthio/jedis/issues/1367有关
To get around this on my mac I did sudo ifconfig lo0 alias 127.0.1.1
Then changed my sentinel.conf to bind 127.0.1.1
and then updated my yaml for spring data to connect to that ip为了在我的 mac 上解决这个问题,我做了
sudo ifconfig lo0 alias 127.0.1.1
然后将我的 sentinel.conf 更改为bind 127.0.1.1
然后更新我的 yaml 以获取 spring 数据以连接到该 ip
sentinel.nodes: 127.0.1.1:5000, 127.0.1.1:5002, 127.0.1.1:5003
Jedis doesn't support well Redis Sentinel. Jedis 不支持 Redis Sentinel。 Use Lettuce instead which has a better pool management.
改用生菜,它具有更好的池管理。 And well supported on springboot too.
并且在 springboot 上也得到了很好的支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.