我在本地主机中设置了3个Redis Server和3个Redis Sentinel实例。服务器在以下位置运行:
127.0.0.1:6379 // Master
127.0.0.1:6380 // Slave
127.0.0.1:6381 // Slave
和哨兵在:
127.0.0.1:5000
127.0.0.1:5001
127.0.0.1:5002
我有一个(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);
}
}
}
最初,我的哨兵配置如下(例如,第一个哨兵在端口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
如果我尝试运行(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)
但是,如果我将前哨配置脚本更改为以下脚本:
# 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
客户端运行良好。我不明白为什么。
AFAIK,如果未在requirepass
文件中设置sentinel.conf
并且在bind
文件中注释了sentinel.conf
,则只有protected-mode
将是{{1} },以避免除yes
之外的任何客户端连接到哨兵。在我的第一个哨兵配置中,我使用了localhost
命令,但仍然无法正常工作。
为什么注释掉bind
并将bind
显式设置为protected-mode
有用?
P.S。我也尝试过同时使用no
和bind 127.0.0.1
,但即使这样也没有用。
答案 0 :(得分:0)
我有一个类似的问题,杰迪斯(Jedis)抛出了相同的错误,这与https://github.com/xetorthio/jedis/issues/1367
有关要在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
答案 1 :(得分:-2)
Jedis对Redis Sentinel的支持不佳。请改用生菜,这样可以更好地管理池。 而且在springboot上也得到了很好的支持。