如何使用Curator设置与Zookeeper的连接延迟,所以它不会每秒ping一次?

时间:2016-09-29 10:59:41

标签: java apache-zookeeper apache-curator

我正在使用Curator连接到Zookeeper。在Zookeeper不可用的情况下,我经常遇到这样的事件:

2016-09-29 12:54:22.831  INFO 43937 --- [localhost:2181)] 
org.apache.zookeeper.ClientCnxn          : Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2016-09-29 12:54:22.832  WARN 43937 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect

java.net.ConnectException: Connection refused
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
    at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
    at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081)

2016-09-29 12:54:23.940  INFO 43937 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2016-09-29 12:54:23.940  WARN 43937 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect

java.net.ConnectException: Connection refused
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
    at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
    at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081)

我无法找到可以将此行为配置为不经常发生的地方。尝试连接Zookeeper时,第一次不考虑可能使用RetryPolicy设置的CuratorFramework。有什么建议吗?我希望线程不会每秒检查一次连接,但不经常,或理想情况下调整指数策略。

1 个答案:

答案 0 :(得分:0)

据我所知,没有办法配置它。有来自ClientCnxnSocket类的代码:

@Override
    public void run() {
        clientCnxnSocket.introduce(this,sessionId);
        clientCnxnSocket.updateNow();
        clientCnxnSocket.updateLastSendAndHeard();
        int to;
        long lastPingRwServer = System.currentTimeMillis();
        final int MAX_SEND_PING_INTERVAL = 10000; //10 seconds
        while (state.isAlive()) {
            try {
                if (!clientCnxnSocket.isConnected()) {
                    if(!isFirstConnect){
                        try {
       >>>>>>>>>>>>>>>>>>>Thread.sleep(r.nextInt(1000));
                        } catch (InterruptedException e) {
                            LOG.warn("Unexpected exception", e);
                        }
                    }
                    // don't re-establish connection if we are closing
                    if (closing || !state.isAlive()) {
                        break;
                    }
                    startConnect();
                    clientCnxnSocket.updateLastSendAndHeard();
                }

查看标有">>>>>>>>>>>>>>>>>>>>>的行",我想在这个地方发生了等待下一次尝试。