2017-09-06 84 views
0

我实现了它在这个site馆长领导人选举给予拒绝连接错误

我只加一个策展人客户端如下

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(); 

} 

}

给予而不必馆长客户数量馆长领导人选举例子

我只有一个zookeeper实例,我使用的是Zookeeper 3.4.6,curator-framework 4.0.0和curator-recipe 4.0.0。 当我启动客户端,它连接到zookeeper,并在日志中,我可以看到“状态更改:连接”消息。

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

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] 

经过一段时间它开始给我下面的错误信息。

org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss 
    at org.apache.zookeeper.KeeperException.create(KeeperException.java:99) ~[zookeeper-3.5.3-beta.jar:3.5.3-beta-8ce24f9e675cbefffb8f21a47e06b42864475a60] 
    at org.apache.curator.framework.imps.CuratorFrameworkImpl.checkBackgroundRetry(CuratorFrameworkImpl.java:831) [curator-framework-4.0.0.jar:4.0.0] 
    at org.apache.curator.framework.imps.CuratorFrameworkImpl.processBackgroundOperation(CuratorFrameworkImpl.java:623) [curator-framework-4.0.0.jar:4.0.0] 
    at org.apache.curator.framework.imps.WatcherRemovalFacade.processBackgroundOperation(WatcherRemovalFacade.java:152) [curator-framework-4.0.0.jar:4.0.0] 
    at org.apache.curator.framework.imps.GetConfigBuilderImpl$2.processResult(GetConfigBuilderImpl.java:222) [curator-framework-4.0.0.jar:4.0.0] 
    at org.apache.zookeeper.ClientCnxn$EventThread.processEvent(ClientCnxn.java:590) [zookeeper-3.5.3-beta.jar:3.5.3-beta-8ce24f9e675cbefffb8f21a47e06b42864475a60] 
    at org.apache.zookeeper.ClientCnxn$EventThread.run(ClientCnxn.java:499) [zookeeper-3.5.3-beta.jar:3.5.3-beta-8ce24f9e675cbefffb8f21a47e06b42864475a60] 

2017-09-06 09:34:31.897 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:31.898 INFO 1228 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn   : Socket connection established, initiating session, client: /127.0.0.1:49611, server: localhost/127.0.0.1:2181 
2017-09-06 09:34:31.899 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:31.899 INFO 1228 --- [c-1-EventThread] o.a.c.f.state.ConnectionStateManager  : State change: RECONNECTED 
2017-09-06 09:34:31.907 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: Xid out of order. Got Xid 41 with err -6 expected Xid 40 for a packet with details: clientPath:/leaderSelections serverPath:/leaderSelections finished:false header:: 40,12 replyHeader:: 0,0,-4 request:: '/leaderSelections,F response:: v{} 
    at org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:892) ~[zookeeper-3.5.3-beta.jar:3.5.3-beta-8ce24f9e675cbefffb8f21a47e06b42864475a60] 
    at org.apache.zookeeper.ClientCnxnSocketNIO.doIO(ClientCnxnSocketNIO.java:101) ~[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

我修复了这个问题。动物园管理员版本和馆长版本之间存在版本号不匹配。我用zookeeper 3.4.6使用了curator版本4.0.0。根据apache curator site

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