ZooKeeper客户端地址列表的随机原理
2013-07-22 00:31 java20130722 阅读(474) 评论(0) 编辑 收藏 举报
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://nileader.blog.51cto.com/1381108/932948
- public ZooKeeper(String connectString, //
- int sessionTimeout, //
- Watcher watcher,//
- boolean canBeReadOnly )
- throws IOException
- 192.168.1.1:2181,192.168.1.2:2181,192.168.1.3:2181
- new ZooKeeper(“192.168.1.1:2181,192.168.1.2:2181,192.168.1.3:2181”,...)
new ConnectStringParser(“192.168.1.1:2181,192.168.1.2:2181,192.168.1.3:2181”);
- ConnectStringParser connectStringParser =
- public final class ConnectStringParser {
- private final String chrootPath;
- private final ArrayList<InetSocketAddress> serverAddresses = new ArrayList<InetSocketAddress>();
- }
- public StaticHostProvider(Collection<InetSocketAddress> serverAddresses)
- throws UnknownHostException {
- ……
- ……
- Collections.shuffle(this.serverAddresses);
- }
- public InetSocketAddress next(long spinDelay) {
- ++currentIndex;
- if (currentIndex == serverAddresses.size()) {
- currentIndex = 0;
- }
- ……
- ……
- return serverAddresses.get(currentIndex);
- }