org.apache.curator.RetryPolicy Java Examples

The following examples show how to use org.apache.curator.RetryPolicy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ZookeeperNamingService.java    From brpc-java with Apache License 2.0 6 votes vote down vote up
public ZookeeperNamingService(BrpcURL url) {
    super(url);
    this.url = url;
    int sleepTimeoutMs = url.getIntParameter(
            Constants.SLEEP_TIME_MS, Constants.DEFAULT_SLEEP_TIME_MS);
    int maxTryTimes = url.getIntParameter(
            Constants.MAX_TRY_TIMES, Constants.DEFAULT_MAX_TRY_TIMES);
    int sessionTimeoutMs = url.getIntParameter(
            Constants.SESSION_TIMEOUT_MS, Constants.DEFAULT_SESSION_TIMEOUT_MS);
    int connectTimeoutMs = url.getIntParameter(
            Constants.CONNECT_TIMEOUT_MS, Constants.DEFAULT_CONNECT_TIMEOUT_MS);
    String namespace = Constants.DEFAULT_PATH;
    if (url.getPath().startsWith("/")) {
        namespace = url.getPath().substring(1);
    }
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(sleepTimeoutMs, maxTryTimes);
    client = CuratorFrameworkFactory.builder()
            .connectString(url.getHostPorts())
            .connectionTimeoutMs(connectTimeoutMs)
            .sessionTimeoutMs(sessionTimeoutMs)
            .retryPolicy(retryPolicy)
            .namespace(namespace)
            .build();
    client.start();
}
 
Example #2
Source File: BeihuZookeeperAutoConfiguration.java    From beihu-boot with Apache License 2.0 6 votes vote down vote up
@Bean(name = "curatorFramework")
    @ConditionalOnMissingBean(name = "curatorFramework")
    protected CuratorFramework curatorFramework() throws Exception {

        RetryPolicy retryPolicy = new ExponentialBackoffRetry(beihuZookeeperProperties.getRetryPolicy().getBaseSleepTime(),
                beihuZookeeperProperties.getRetryPolicy().getRetryNum(),
                beihuZookeeperProperties.getRetryPolicy().getMaxSleepTime());

        return CuratorFrameworkFactory.builder()
                .connectString(beihuZookeeperProperties.getZhosts())
                .sessionTimeoutMs(beihuZookeeperProperties.getSessionTimeout())
//                .connectionTimeoutMs(beihuZookeeperProperties.getConnectionTimeout())
                .namespace(beihuZookeeperProperties.getNamespace())
                .retryPolicy(retryPolicy)
                .build();
    }
 
Example #3
Source File: ZkReentrantLockTemplateTest.java    From Mykit with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
	RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
	CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);
	client.start();

	final ZkDistributedLockTemplate template = new ZkDistributedLockTemplate(client);// 本类多线程安全,可通过spring注入
	template.execute("订单流水号", 5000, new Callback() {
		@Override
		public Object onGetLock() throws InterruptedException {
			// TODO 获得锁后要做的事
			return null;
		}

		@Override
		public Object onTimeout() throws InterruptedException {
			// TODO 获得锁超时后要做的事
			return null;
		}
	});
}
 
Example #4
Source File: ZookeeperOffsetHandler.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public ZookeeperOffsetHandler(Properties props) {
	this.groupId = props.getProperty(ConsumerConfig.GROUP_ID_CONFIG);
	if (this.groupId == null) {
		throw new IllegalArgumentException("Required property '"
				+ ConsumerConfig.GROUP_ID_CONFIG + "' has not been set");
	}

	String zkConnect = props.getProperty("zookeeper.connect");
	if (zkConnect == null) {
		throw new IllegalArgumentException("Required property 'zookeeper.connect' has not been set");
	}

	// we use Curator's default timeouts
	int sessionTimeoutMs =  Integer.valueOf(props.getProperty("zookeeper.session.timeout.ms", "60000"));
	int connectionTimeoutMs = Integer.valueOf(props.getProperty("zookeeper.connection.timeout.ms", "15000"));

	// undocumented config options allowing users to configure the retry policy. (they are "flink." prefixed as they are no official kafka configs)
	int backoffBaseSleepTime = Integer.valueOf(props.getProperty("flink.zookeeper.base-sleep-time.ms", "100"));
	int backoffMaxRetries =  Integer.valueOf(props.getProperty("flink.zookeeper.max-retries", "10"));

	RetryPolicy retryPolicy = new ExponentialBackoffRetry(backoffBaseSleepTime, backoffMaxRetries);
	curatorClient = CuratorFrameworkFactory.newClient(zkConnect, sessionTimeoutMs, connectionTimeoutMs, retryPolicy);
	curatorClient.start();
}
 
Example #5
Source File: ZKDiscoveryServiceImpl.java    From YuRPC with Apache License 2.0 6 votes vote down vote up
/**
 * 初始化方法,(仅在使用无参构造器时使用)
 *
 * @param zookeeper
 * @throws java.lang.Throwable 异常
 */
public void init(String zookeeper) throws Throwable {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    this.client = CuratorFrameworkFactory.newClient(zookeeper, retryPolicy);
    this.client.start();
    this.client.getConnectionStateListenable().addListener((ConnectionStateListener) (CuratorFramework cf, ConnectionState cs) -> {
        if (cs == ConnectionState.RECONNECTED) {
            if (pathValue != null && !pathValue.isEmpty()) {
                pathValue.entrySet().forEach((entry) -> {
                    String path = entry.getKey();
                    byte[] value = entry.getValue();
                    try {
                        cf.create().withMode(CreateMode.EPHEMERAL).forPath(path, value);
                    } catch (Exception ex) {
                        LOGGER.error(ex.getMessage());
                    }
                });
            }
        }
    }, watcherExecutorService);
}
 
Example #6
Source File: ZookeeperOffsetHandler.java    From flink with Apache License 2.0 6 votes vote down vote up
public ZookeeperOffsetHandler(Properties props) {
	this.groupId = props.getProperty(ConsumerConfig.GROUP_ID_CONFIG);
	if (this.groupId == null) {
		throw new IllegalArgumentException("Required property '"
				+ ConsumerConfig.GROUP_ID_CONFIG + "' has not been set");
	}

	String zkConnect = props.getProperty("zookeeper.connect");
	if (zkConnect == null) {
		throw new IllegalArgumentException("Required property 'zookeeper.connect' has not been set");
	}

	// we use Curator's default timeouts
	int sessionTimeoutMs =  Integer.valueOf(props.getProperty("zookeeper.session.timeout.ms", "60000"));
	int connectionTimeoutMs = Integer.valueOf(props.getProperty("zookeeper.connection.timeout.ms", "15000"));

	// undocumented config options allowing users to configure the retry policy. (they are "flink." prefixed as they are no official kafka configs)
	int backoffBaseSleepTime = Integer.valueOf(props.getProperty("flink.zookeeper.base-sleep-time.ms", "100"));
	int backoffMaxRetries =  Integer.valueOf(props.getProperty("flink.zookeeper.max-retries", "10"));

	RetryPolicy retryPolicy = new ExponentialBackoffRetry(backoffBaseSleepTime, backoffMaxRetries);
	curatorClient = CuratorFrameworkFactory.newClient(zkConnect, sessionTimeoutMs, connectionTimeoutMs, retryPolicy);
	curatorClient.start();
}
 
Example #7
Source File: ZKUtil.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private static CuratorFramework getZookeeperClient(final String zkString, final RetryPolicy retryPolicy) {
    if (StringUtils.isEmpty(zkString)) {
        throw new IllegalArgumentException("ZOOKEEPER_QUORUM is empty!");
    }
    try {
        CuratorFramework instance = CACHE.get(zkString, new Callable<CuratorFramework>() {
            @Override
            public CuratorFramework call() throws Exception {
                return newZookeeperClient(zkString, retryPolicy);
            }
        });
        // during test, curator may be closed by others, remove it from CACHE and reinitialize a new one
        if (instance.getState() != CuratorFrameworkState.STARTED) {
            logger.warn("curator for {} is closed by others unexpectedly, reinitialize a new one", zkString);
            CACHE.invalidate(zkString);
            instance = getZookeeperClient(zkString, retryPolicy);
        }
        return instance;
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}
 
Example #8
Source File: Zk.java    From t-io with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param address
 * @param clientDecorator
 * @author tanyaowu
 * @throws Exception
 */
public static void init(String address, ClientDecorator clientDecorator) throws Exception {
	//		String zkhost = "192.168.1.41:2181";//AppConfig.getInstance().getString("zk.address", null);//"192.168.1.41:2181";//ZK host
	//		zkhost = AppConfig.getInstance().getString("zk.address", null);

	if (StrUtil.isBlank(address)) {
		log.error("zk address is null");
		throw new RuntimeException("zk address is null");
	}

	//		RetryPolicy rp = new ExponentialBackoffRetry(500, Integer.MAX_VALUE);//Retry mechanism
	RetryPolicy rp = new RetryForever(500);
	Builder builder = CuratorFrameworkFactory.builder().connectString(address).connectionTimeoutMs(15 * 1000).sessionTimeoutMs(60 * 1000).retryPolicy(rp);
	//				builder.namespace(nameSpace);
	zkclient = builder.build();

	if (clientDecorator != null) {
		clientDecorator.decorate(zkclient);
	}

	//		zkclient.start();
}
 
Example #9
Source File: ZkReentrantLockTemplateTest.java    From javatech with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
public static void main(String[] args) {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);
    client.start();

    final ZkDLockTemplate template = new ZkDLockTemplate(client);//本类多线程安全,可通过spring注入
    template.execute("订单流水号", 5000, new Callback<Object>() {
        @Override
        public Object onGetLock() throws InterruptedException {
            //TODO 获得锁后要做的事
            return null;
        }

        @Override
        public Object onTimeout() throws InterruptedException {
            //TODO 获得锁超时后要做的事
            return null;
        }
    });
}
 
Example #10
Source File: ZkConnection.java    From xian with Apache License 2.0 6 votes vote down vote up
public static void start(String connectionStr) {
    synchronized (zkConnectionStartStopLock) {
        if (connected.get()) {
            LOG.info("zkConnection已经启动,不再重复启动");
            return;
        }
        try {
            RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
            client = CuratorFrameworkFactory.newClient(connectionStr, retryPolicy);
            client.start();
            LOG.info("阻塞直到与zookeeper连接建立完毕!");
            client.blockUntilConnected();
        } catch (Throwable e) {
            LOG.error(e);
        } finally {
            connected.set(true);
        }
    }
}
 
Example #11
Source File: ZkReentrantLockTemplateTest.java    From Mykit with Apache License 2.0 5 votes vote down vote up
@Test
public void testTry() throws InterruptedException {
	RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
	CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);
	client.start();

	final ZkDistributedLockTemplate template = new ZkDistributedLockTemplate(client);
	int size = 100;
	final CountDownLatch startCountDownLatch = new CountDownLatch(1);
	final CountDownLatch endDownLatch = new CountDownLatch(size);
	for (int i = 0; i < size; i++) {
		new Thread() {
			public void run() {
				try {
					startCountDownLatch.await();
				} catch (InterruptedException e) {
					Thread.currentThread().interrupt();
				}
				final int sleepTime = ThreadLocalRandom.current().nextInt(5) * 1000;
				template.execute("test", 5000, new Callback() {
					public Object onGetLock() throws InterruptedException {
						System.out.println(Thread.currentThread().getName() + ":getLock");
						Thread.sleep(sleepTime);
						System.out.println(Thread.currentThread().getName() + ":sleeped:" + sleepTime);
						endDownLatch.countDown();
						return null;
					}

					public Object onTimeout() throws InterruptedException {
						System.out.println(Thread.currentThread().getName() + ":timeout");
						endDownLatch.countDown();
						return null;
					}
				});
			}
		}.start();
	}
	startCountDownLatch.countDown();
	endDownLatch.await();
}
 
Example #12
Source File: TestLockACLs.java    From xian with Apache License 2.0 5 votes vote down vote up
private CuratorFramework createClient(ACLProvider provider) throws Exception
{
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFramework client = CuratorFrameworkFactory.builder()
        .namespace("ns")
        .connectString(server.getConnectString())
        .retryPolicy(retryPolicy)
        .aclProvider(provider)
        .build();
    client.start();
    return client;
}
 
Example #13
Source File: ZkClientProxy.java    From socket with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * default contrastor
 *
 * @param config config is required;
 * @throws IllegalArgumentException 当config是null时抛出该异常
 */
public ZkClientProxy(ZKConfig config) throws IllegalArgumentException {
    if (config == null) {
        throw new IllegalArgumentException("config must non null");
    }
    this.config = config;
    this.serializers = config.getSerializers() == null ? Collections.emptyList()
        : config.getSerializers();
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(config.getBaseSleepTimeMs(),
        config.getMaxRetry());
    CuratorFrameworkFactory.builder().authorization("", null).authorization("", null);
    this.client = CuratorFrameworkFactory.newClient(config.getConnectStr(), retryPolicy);
    this.client.getConnectionStateListenable()
        .addListener(new ConnectionStateListenerAdapter(this, ((client1, newState) -> {
            if (newState
                .equals(com.joe.easysocket.server.common.spi.ConnectionState.CONNECTED)) {
                log.info("客户端连接成功,准备启动节点监听器");
                listenerLock.lock();
                try {
                    treeCaches.entrySet().stream().map(Map.Entry::getValue)
                        .forEach(CacheAdapater::start);
                    pathChildrenCaches.entrySet().stream().map(Map.Entry::getValue)
                        .forEach(CacheAdapater::start);
                } finally {
                    listenerLock.unlock();
                }
            }
        })));
}
 
Example #14
Source File: CreateClientExamples.java    From xian with Apache License 2.0 5 votes vote down vote up
public static CuratorFramework  createWithOptions(String connectionString, RetryPolicy retryPolicy, int connectionTimeoutMs, int sessionTimeoutMs)
{
    // using the CuratorFrameworkFactory.builder() gives fine grained control
    // over creation options. See the CuratorFrameworkFactory.Builder javadoc
    // details
    return CuratorFrameworkFactory.builder()
        .connectString(connectionString)
        .retryPolicy(retryPolicy)
        .connectionTimeoutMs(connectionTimeoutMs)
        .sessionTimeoutMs(sessionTimeoutMs)
        // etc. etc.
        .build();
}
 
Example #15
Source File: LogSearchConfigZKHelper.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
private static RetryPolicy getRetryPolicy(String zkConnectionRetryTimeoutValue) {
  if (zkConnectionRetryTimeoutValue == null)
    return new RetryForever(RETRY_INTERVAL_MS);
  int maxElapsedTimeMs = Integer.parseInt(zkConnectionRetryTimeoutValue);
  if (maxElapsedTimeMs == 0)
    return new RetryForever(RETRY_INTERVAL_MS);
  return new RetryUntilElapsed(maxElapsedTimeMs, RETRY_INTERVAL_MS);
}
 
Example #16
Source File: ZkDistributedSequence.java    From Mykit with Apache License 2.0 5 votes vote down vote up
public ZkDistributedSequence(String zookeeperAddress){
    try{
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(baseSleepTimeMs, maxRetries);
        client = CuratorFrameworkFactory.newClient(zookeeperAddress, retryPolicy);
        client.start();
    }catch (Exception e){
        log.error(e.getMessage(),e);
    }catch (Throwable ex){
        ex.printStackTrace();
        log.error(ex.getMessage(),ex);
    }
}
 
Example #17
Source File: ZkReentrantLockCleanerTask.java    From Mykit with Apache License 2.0 5 votes vote down vote up
public ZkReentrantLockCleanerTask(String zookeeperAddress) {
	try {
		RetryPolicy retryPolicy = new ExponentialBackoffRetry(baseSleepTimeMs, maxRetries);
		client = CuratorFrameworkFactory.newClient(zookeeperAddress, retryPolicy);
		client.start();
	} catch (Exception e) {
		log.error(e.getMessage(), e);
	} catch (Throwable ex) {
		ex.printStackTrace();
		log.error(ex.getMessage(), ex);
	}
}
 
Example #18
Source File: CuratorFrameworkFactory.java    From xian with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new client
 *
 * @param connectString       list of servers to connect to
 * @param sessionTimeoutMs    session timeout
 * @param connectionTimeoutMs connection timeout
 * @param retryPolicy         retry policy to use
 * @return client
 */
public static CuratorFramework newClient(String connectString, int sessionTimeoutMs, int connectionTimeoutMs, RetryPolicy retryPolicy)
{
    return builder().
        connectString(connectString).
        sessionTimeoutMs(sessionTimeoutMs).
        connectionTimeoutMs(connectionTimeoutMs).
        retryPolicy(retryPolicy).
        build();
}
 
Example #19
Source File: ZookeeperAuthBoltServerTest.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
protected void createPathWithAuth() {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFrameworkFactory.Builder zkClientuilder = CuratorFrameworkFactory.builder()
        .connectString("127.0.0.1:2181")
        .sessionTimeoutMs(20000 * 3)
        .connectionTimeoutMs(20000)
        .canBeReadOnly(false)
        .retryPolicy(retryPolicy)
        .defaultData(null);

    //是否需要添加zk的认证信息
    Map authMap = new HashMap<String, String>();
    authMap.put("scheme", "digest");
    //如果存在多个认证信息,则在参数形式为为user1:passwd1,user2:passwd2
    authMap.put("addAuth", "sofazk:rpc1");

    List<AuthInfo> authInfos = buildAuthInfo(authMap);
    if (CommonUtils.isNotEmpty(authInfos)) {
        zkClientuilder = zkClientuilder.aclProvider(getDefaultAclProvider())
            .authorization(authInfos);
    }

    try {
        zkClient = zkClientuilder.build();
        zkClient.start();
        zkClient.create().withMode(CreateMode.PERSISTENT).forPath("/authtest");
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
 
Example #20
Source File: ZkDistributedSequence.java    From javatech with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
public ZkDistributedSequence(String zookeeperAddress) {
    try {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(baseSleepTimeMs, maxRetries);
        client = CuratorFrameworkFactory.newClient(zookeeperAddress, retryPolicy);
        client.start();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    } catch (Throwable ex) {
        ex.printStackTrace();
        log.error(ex.getMessage(), ex);
    }
}
 
Example #21
Source File: PromotedToLock.java    From xian with Apache License 2.0 5 votes vote down vote up
private PromotedToLock(String path, long maxLockTime, TimeUnit maxLockTimeUnit, RetryPolicy retryPolicy)
{
    this.path = path;
    this.maxLockTime = maxLockTime;
    this.maxLockTimeUnit = maxLockTimeUnit;
    this.retryPolicy = retryPolicy;
}
 
Example #22
Source File: ZkReentrantLockCleanerTask.java    From javatech with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
public ZkReentrantLockCleanerTask(String zookeeperAddress) {
    try {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(baseSleepTimeMs, maxRetries);
        client = CuratorFrameworkFactory.newClient(zookeeperAddress, retryPolicy);
        client.start();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    } catch (Throwable ex) {
        ex.printStackTrace();
        log.error(ex.getMessage(), ex);
    }
}
 
Example #23
Source File: ZookeeperRuleConfigSource.java    From ratelimiter4j with Apache License 2.0 5 votes vote down vote up
private void initialClient() {
  if (!isInitialized.compareAndSet(false, true)) {
    return;
  }

  if (client == null && StringUtils.isEmpty(address)) {
    throw new RuntimeException("zookeeper server address is not set.");
  }

  boolean connected = false;
  try {
    if (client == null) {
      RetryPolicy retryPolicy = new ExponentialBackoffRetry(baseSleepTimeMs, maxRetries);
      client = CuratorFrameworkFactory.builder().connectString(address).retryPolicy(retryPolicy)
          .connectionTimeoutMs(connectionTimeout).sessionTimeoutMs(sessionTimeout).build();
    }
    client.start();
    connected = client.blockUntilConnected(connectionTimeout, TimeUnit.MILLISECONDS);
    if (!connected) {
      throw new RuntimeException("connect zookeeper failed.");
    }
  } catch (Exception e) {
    CloseableUtils.closeQuietly(client);
    isInitialized.compareAndSet(true, false);
    throw new RuntimeException("init zookeeper client error.", e);
  }
}
 
Example #24
Source File: CuratorConfiguration.java    From kafka-graphs with Apache License 2.0 5 votes vote down vote up
@Bean(initMethod = "start", destroyMethod = "close")
public CuratorFramework curatorFramework(RetryPolicy retryPolicy) {

    final CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
    builder
        .connectString(props.getZookeeperConnect())
        .retryPolicy(retryPolicy);
    return builder.build();
}
 
Example #25
Source File: CuratorLeaderElectionManager.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private CuratorFramework createClient() {
    // Create a new client because we don't want to try indefinitely for this to occur.
    final RetryPolicy retryPolicy = new RetryNTimes(1, 100);

    final CuratorFramework client = CuratorFrameworkFactory.builder()
        .connectString(zkConfig.getConnectString())
        .sessionTimeoutMs(zkConfig.getSessionTimeoutMillis())
        .connectionTimeoutMs(zkConfig.getConnectionTimeoutMillis())
        .retryPolicy(retryPolicy)
        .defaultData(new byte[0])
        .build();

    client.start();
    return client;
}
 
Example #26
Source File: Cluster.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
public CuratorFramework createCuratorClient() {
    final RetryPolicy retryPolicy = new RetryNTimes(20, 500);
    final CuratorFramework curatorClient = CuratorFrameworkFactory.builder()
        .connectString(getZooKeeperConnectString())
        .sessionTimeoutMs(3000)
        .connectionTimeoutMs(3000)
        .retryPolicy(retryPolicy)
        .defaultData(new byte[0])
        .build();

    curatorClient.start();
    return curatorClient;
}
 
Example #27
Source File: CuratorWatcher.java    From BigData-In-Practice with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    final String nodePath = "/testZK";
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(10000, 5);
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(zkServerIps)
            .sessionTimeoutMs(10000).retryPolicy(retryPolicy).build();
    try {
        client.start();
        client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(nodePath, "this is a test data".getBytes());

        final NodeCache cacheNode = new NodeCache(client, nodePath, false);
        cacheNode.start(true);  // true 表示启动时立即从Zookeeper上获取节点
        cacheNode.getListenable().addListener(new NodeCacheListener() {
            @Override
            public void nodeChanged() throws Exception {
                System.out.println("节点数据更新,新的内容是: " + new String(cacheNode.getCurrentData().getData()));
            }
        });
        for (int i = 0; i < 5; i++) {
            client.setData().forPath(nodePath, ("new test data " + i).getBytes());
            Thread.sleep(1000);
        }
        Thread.sleep(10000); // 等待100秒,手动在 zkCli 客户端操作节点,触发事件
    } finally {
        client.delete().deletingChildrenIfNeeded().forPath(nodePath);
        client.close();
        System.out.println("客户端关闭......");
    }
}
 
Example #28
Source File: ZkConfig.java    From spring-boot-demo with MIT License 5 votes vote down vote up
@Bean
public CuratorFramework curatorFramework() {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(zkProps.getTimeout(), zkProps.getRetry());
    CuratorFramework client = CuratorFrameworkFactory.newClient(zkProps.getUrl(), retryPolicy);
    client.start();
    return client;
}
 
Example #29
Source File: ZkConfig.java    From spring-boot-demo with MIT License 5 votes vote down vote up
@Bean
public CuratorFramework curatorFramework() {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(zkProps.getTimeout(), zkProps.getRetry());
    CuratorFramework client = CuratorFrameworkFactory.newClient(zkProps.getUrl(), retryPolicy);
    client.start();
    return client;
}
 
Example #30
Source File: SnowflakeZookeeperHolder.java    From Leaf with Apache License 2.0 5 votes vote down vote up
private CuratorFramework createWithOptions(String connectionString, RetryPolicy retryPolicy, int connectionTimeoutMs, int sessionTimeoutMs) {
    return CuratorFrameworkFactory.builder().connectString(connectionString)
            .retryPolicy(retryPolicy)
            .connectionTimeoutMs(connectionTimeoutMs)
            .sessionTimeoutMs(sessionTimeoutMs)
            .build();
}