简体   繁体   English

Java 客户端无法连接到在本地主机上运行的 Redis Sentinel

[英]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.1protected-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.

相关问题 无法连接到在 localhost 上运行的远程 JMX - Can't connect to remote JMX running on localhost 我的 tomcat 正在运行,但无法连接到 http://localhost:8080 - My tomcat is running but I can't connect to http://localhost:8080 在 redis 哨兵配置中,如果 master 关闭,则无法连接到从机,所有这些都在 Docker 容器中运行 - Not able to connect to slave machine in case of master is down in redis sentinel configuration, all are running in Docker container 无法将Java客户端连接到C服务器 - can't connect Java client to C server Javascript 客户端无法连接到 Java 服务器 - Javascript client can't connect to java server 在docker上运行的mysql容器无法与spring boot java api服务连接 - mysql container which is running on docker can't able to connect with spring boot java api service 用于连接ElasticCache Redis缓存节点的Java客户端 - Java Client to connect ElasticCache Redis Cache Node 除非使用本地主机,否则无法使用Java连接到MySQL - Can't connect to MySQL using java unless its localhost 将JDBC Java客户端连接到本地主机上的SQL Developer - Connect a JDBC Java Client to SQL Developer on localhost 无法连接到本地主机上的MySQL - can't connect to MySQL on localhost
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM