org.apache.zookeeper.ZooKeeper.updateServerList()方法的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(3.8k)|赞(0)|评价(0)|浏览(218)

本文整理了Java中org.apache.zookeeper.ZooKeeper.updateServerList()方法的一些代码示例,展示了ZooKeeper.updateServerList()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZooKeeper.updateServerList()方法的具体详情如下:
包路径:org.apache.zookeeper.ZooKeeper
类名称:ZooKeeper
方法名:updateServerList

ZooKeeper.updateServerList介绍

[英]This function allows a client to update the connection string by providing a new comma separated list of host:port pairs, each corresponding to a ZooKeeper server.

The function invokes a probabilistic load-balancing algorithm which may cause the client to disconnect from its current host with the goal to achieve expected uniform number of connections per server in the new list. In case the current host to which the client is connected is not in the new list this call will always cause the connection to be dropped. Otherwise, the decision is based on whether the number of servers has increased or decreased and by how much. For example, if the previous connection string contained 3 hosts and now the list contains these 3 hosts and 2 more hosts, 40% of clients connected to each of the 3 hosts will move to one of the new hosts in order to balance the load. The algorithm will disconnect from the current host with probability 0.4 and in this case cause the client to connect to one of the 2 new hosts, chosen at random.

If the connection is dropped, the client moves to a special mode "reconfigMode" where he chooses a new server to connect to using the probabilistic algorithm. After finding a server, or exhausting all servers in the new list after trying all of them and failing to connect, the client moves back to the normal mode of operation where it will pick an arbitrary server from the connectString and attempt to connect to it. If establishment of the connection fails, another server in the connect string will be tried (the order is non-deterministic, as we random shuffle the list), until a connection is established. The client will continue attempts until the session is explicitly closed (or the session is expired by the server).
[中]此函数允许客户端通过提供一个新的以逗号分隔的主机:端口对列表来更新连接字符串,每个主机:端口对对应于ZooKeeper服务器。
该函数调用{$0$},这可能会导致客户端断开与当前主机的连接,目的是在新列表中实现每个服务器预期的统一连接数。如果客户端连接的当前主机不在新列表中,此调用将始终导致连接断开。否则,决定取决于服务器的数量是增加了还是减少了,以及减少了多少。例如,如果以前的连接字符串包含3台主机,而现在列表包含这3台主机和另外2台主机,那么连接到这3台主机中的每台主机的40%客户端将移动到新主机之一,以平衡负载。该算法将以0.4的概率断开与当前主机的连接,在这种情况下,会导致客户端连接到随机选择的两个新主机之一。
如果连接断开,客户机将转到特殊模式“reconfigMode”,在该模式下,他使用概率算法选择一个新服务器进行连接。找到服务器后,或在尝试所有服务器并连接失败后耗尽新列表中的所有服务器后,客户端将返回正常操作模式,在该模式下,它将从connectString中选择任意服务器并尝试连接到该服务器。如果建立连接失败,将尝试连接字符串中的另一个服务器(顺序是不确定的,因为我们随机洗牌列表),直到建立连接为止。客户端将继续尝试,直到会话被显式关闭(或会话被服务器终止)。

代码示例

代码示例来源:origin: apache/zookeeper

@Test
  public void testZooKeeperWithCustomHostProvider() throws IOException,
      InterruptedException {
    final int CLIENT_PORT = PortAssignment.unique();
    final HostProvider specialHostProvider = new SpecialHostProvider();
    int expectedCounter = 3;
    counter.set(expectedCounter);

    ZooKeeper zkDefaults = new ZooKeeper("127.0.0.1:" + CLIENT_PORT,
        ClientBase.CONNECTION_TIMEOUT, this, false);

    ZooKeeper zkSpecial = new ZooKeeper("127.0.0.1:" + CLIENT_PORT,
        ClientBase.CONNECTION_TIMEOUT, this, false, specialHostProvider);

    Assert.assertTrue(counter.get() == expectedCounter);
    zkDefaults.updateServerList("127.0.0.1:" + PortAssignment.unique());
    Assert.assertTrue(counter.get() == expectedCounter);

    zkSpecial.updateServerList("127.0.0.1:" + PortAssignment.unique());
    expectedCounter--;
    Assert.assertTrue(counter.get() == expectedCounter);
  }
}

代码示例来源:origin: org.apache.curator/curator-client

private void handleNewConnectionString(String newConnectionString)
{
  log.info("Connection string changed to: " + newConnectionString);
  new EventTrace("connection-string-changed", tracer.get(), getSessionId()).commit();
  try
  {
    ZooKeeper zooKeeper = this.zooKeeper.getZooKeeper();
    if ( zooKeeper == null )
    {
      log.warn("Could not update the connection string because getZooKeeper() returned null.");
    }
    else
    {
      if ( ensembleProvider.updateServerListEnabled() )
      {
        zooKeeper.updateServerList(newConnectionString);
      }
      else
      {
        reset();
      }
    }
  }
  catch ( Exception e )
  {
    ThreadUtils.checkInterrupted(e);
    queueBackgroundException(e);
  }
}

相关文章