策展人领导选举给连接拒绝错误

时间:2017-09-06 04:43:26

标签: apache-zookeeper apache-curator

我实施了此site

中提供的策展人领导人选举示例

我没有添加一个策展人客户端,而是拥有多个策展人客户端,如下所示

DatabaseReference dRef = FirebaseDatabase.getInstance().getReference().child("group_list").child("First_Group");
    dRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                String userName = snapshot.child("user").getValue(String.class);
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

}

我只有一个zookeeper实例,我使用的是Zookeeper 3.4.6,curator-framework 4.0.0和curator-recipes 4.0.0。 当我启动客户端时,它连接到zookeeper,在日志中我可以看到“State change:connected”消息。

然后我等了10秒并开始领导选举,这反复给我以下错误。

public void selectLeader() {
    CuratorFramework    client = null;
    try {
        client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(1000, 3));
        LeaderSelectorService    service = new LeaderSelectorService(client, "/leaderSelections", "LeaderElector");

        client.start();
        Thread.sleep(10000);
        service.start();
    } catch (Exception e) {
        System.out.println("error"+e);
    }finally
    {
        System.out.println("Shutting down...");
       // CloseableUtils.closeQuietly(client);
    }

}

public class LeaderSelectorService extends LeaderSelectorListenerAdapter implements Closeable {
private final String name;
private final LeaderSelector leaderSelector;

public LeaderSelectorService(CuratorFramework client, String path, String name) {
    this.name = name;

    // create a leader selector using the given path for management
    // all participants in a given leader selection must use the same path
    // ExampleClient here is also a LeaderSelectorListener but this isn't required
    leaderSelector = new LeaderSelector(client, path, this);

    // for most cases you will want your instance to requeue when it relinquishes leadership
    leaderSelector.autoRequeue();   
}

public void start() throws IOException
    {
        // the selection for this instance doesn't start until the leader selector is started
        // leader selection is done in the background so this call to leaderSelector.start() returns immediately
        leaderSelector.start();
    }
@Override
public void takeLeadership(CuratorFramework arg0) throws Exception {
    // we are now the leader. This method should not return until we want to relinquish leadership

    final int         waitSeconds = (int)(5 * Math.random()) + 1;

    System.out.println(name + " is now the leader. Waiting " + waitSeconds + " seconds...");
    //System.out.println(name + " has been leader " + leaderCount.getAndIncrement() + " time(s) before.");
    try
    {
        Thread.sleep(TimeUnit.SECONDS.toMillis(waitSeconds));
    }
    catch ( InterruptedException e )
    {
        System.err.println(name + " was interrupted.");
        Thread.currentThread().interrupt();
    }
    finally
    {
        System.out.println(name + " relinquishing leadership.\n");
    }

}

@Override
public void close() throws IOException {
    leaderSelector.close();

}

一段时间后,它开始给我以下错误信息。

2017-09-06 09:34:22.727  INFO 1228 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Unable to read additional data from server sessionid 0x15e555a719d0000, likely server has closed socket, closing socket connection and attempting reconnect
2017-09-06 09:34:22.830  INFO 1228 --- [c-1-EventThread] o.a.c.f.state.ConnectionStateManager     : State change: SUSPENDED
2017-09-06 09:34:23.302  INFO 1228 --- [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)
2017-09-06 09:34:23.303  INFO 1228 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Socket connection established, initiating session, client: /127.0.0.1:49594, server: localhost/127.0.0.1:2181
2017-09-06 09:34:23.305  INFO 1228 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x15e555a719d0000, negotiated timeout = 120000
2017-09-06 09:34:23.305  INFO 1228 --- [c-1-EventThread] o.a.c.f.state.ConnectionStateManager     : State change: RECONNECTED
2017-09-06 09:34:23.310  WARN 1228 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Session 0x15e555a719d0000 for server localhost/127.0.0.1:2181, unexpected error, closing socket connection and attempting reconnect

java.io.IOException: Connection reset by peer
    at sun.nio.ch.FileDispatcherImpl.read0(Native Method) ~[na:1.8.0_131]
    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39) ~[na:1.8.0_131]
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223) ~[na:1.8.0_131]
    at sun.nio.ch.IOUtil.read(IOUtil.java:192) ~[na:1.8.0_131]
    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380) ~[na:1.8.0_131]
    at org.apache.zookeeper.ClientCnxnSocketNIO.doIO(ClientCnxnSocketNIO.java:75) ~[zookeeper-3.5.3-beta.jar:3.5.3-beta-8ce24f9e675cbefffb8f21a47e06b42864475a60]
    at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:363) ~[zookeeper-3.5.3-beta.jar:3.5.3-beta-8ce24f9e675cbefffb8f21a47e06b42864475a60]
    at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1214) ~[zookeeper-3.5.3-beta.jar:3.5.3-beta-8ce24f9e675cbefffb8f21a47e06b42864475a60]

我在互联网上尝试了几种解决方案但是没有成功。有没有人知道这个问题的根本原因。

1 个答案:

答案 0 :(得分:1)

我已经解决了这个问题。 zookeeper版本和策展人版本之间的版本号不匹配。我使用curator版本4.0.0和zookeeper 3.4.6。根据{{​​3}}

Curator 4.0.0 - 与ZooKeeper 3.5.x兼容。我将我的馆长版本改为2.8.0