Java Code Examples for org.apache.curator.framework.CuratorFrameworkFactory#newClient()

The following examples show how to use org.apache.curator.framework.CuratorFrameworkFactory#newClient() . 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: TestNamespaceFacade.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void     testBasic() throws Exception
{
    CuratorFramework    client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    try
    {
        client.start();

        client.create().forPath("/one");
        Assert.assertNotNull(client.getZookeeperClient().getZooKeeper().exists("/one", false));

        client.usingNamespace("space").create().forPath("/one");
        Assert.assertNotNull(client.getZookeeperClient().getZooKeeper().exists("/space", false));

        client.usingNamespace("name").create().forPath("/one");
        Assert.assertNotNull(client.getZookeeperClient().getZooKeeper().exists("/name", false));
        Assert.assertNotNull(client.getZookeeperClient().getZooKeeper().exists("/name/one", false));
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 2
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 3
Source File: ZKUtils.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
private static CuratorFramework createConnection() {
    String url = ZkConfig.getInstance().getZkURL();

    CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(url, new ExponentialBackoffRetry(100, 6));

    // start connection
    curatorFramework.start();
    // wait 3 second to establish connect
    try {
        curatorFramework.blockUntilConnected(3, TimeUnit.SECONDS);
        if (curatorFramework.getZookeeperClient().isConnected()) {
            return curatorFramework;
        }
    } catch (InterruptedException ignored) {
        Thread.currentThread().interrupt();
    }

    // fail situation
    curatorFramework.close();
    throw new RuntimeException("failed to connect to zookeeper service : " + url);
}
 
Example 4
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 5
Source File: ZkDiscoveryService.java    From iotplatform with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
  log.info("Initializing...");
  Assert.hasLength(zkUrl, MiscUtils.missingProperty("zk.url"));
  Assert.notNull(zkRetryInterval, MiscUtils.missingProperty("zk.retry_interval_ms"));
  Assert.notNull(zkConnectionTimeout, MiscUtils.missingProperty("zk.connection_timeout_ms"));
  Assert.notNull(zkSessionTimeout, MiscUtils.missingProperty("zk.session_timeout_ms"));

  log.info("Initializing discovery service using ZK connect string: {}", zkUrl);

  zkNodesDir = zkDir + "/nodes";
  try {
    client = CuratorFrameworkFactory.newClient(zkUrl, zkSessionTimeout, zkConnectionTimeout,
        new RetryForever(zkRetryInterval));
    client.start();
    client.blockUntilConnected();
    cache = new PathChildrenCache(client, zkNodesDir, true);
    cache.getListenable().addListener(this);
    cache.start();
  } catch (Exception e) {
    log.error("Failed to connect to ZK: {}", e.getMessage(), e);
    CloseableUtils.closeQuietly(client);
    throw new RuntimeException(e);
  }
}
 
Example 6
Source File: TestNamespaceFacade.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void     testGetNamespace() throws Exception
{
    CuratorFramework    client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    CuratorFramework    client2 = CuratorFrameworkFactory.builder().namespace("snafu").retryPolicy(new RetryOneTime(1)).connectString("").build();
    try
    {
        client.start();

        CuratorFramework fooClient = client.usingNamespace("foo");
        CuratorFramework barClient = client.usingNamespace("bar");

        Assert.assertEquals(client.getNamespace(), "");
        Assert.assertEquals(client2.getNamespace(), "snafu");
        Assert.assertEquals(fooClient.getNamespace(), "foo");
        Assert.assertEquals(barClient.getNamespace(), "bar");
    }
    finally
    {
        CloseableUtils.closeQuietly(client2);
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 7
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 8
Source File: CuratorTest.java    From liteFlow with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
        // 1.Connect to zk
        CuratorFramework client = CuratorFrameworkFactory.newClient(
                ZK_ADDRESS,
                new RetryNTimes(10, 5000)
        );
        client.start();

        checkNode(client);

//        childNodeListen(client);

//        removeNodeData(client);

//        createNode(client);

//        nodeListen(client);
//
//        modifyNodeData(client);

        System.in.read();

//        getNodeData(client);
//
//

    }
 
Example 9
Source File: TestDistributedBarrier.java    From xian with Apache License 2.0 5 votes vote down vote up
@Test
public void     testBasic() throws Exception
{
    CuratorFramework            client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    try
    {
        client.start();

        final DistributedBarrier      barrier = new DistributedBarrier(client, "/barrier");
        barrier.setBarrier();

        ExecutorService               service = Executors.newSingleThreadExecutor();
        service.submit
        (
            new Callable<Object>()
            {
                @Override
                public Object call() throws Exception
                {
                    Thread.sleep(1000);
                    barrier.removeBarrier();
                    return null;
                }
            }
        );

        Assert.assertTrue(barrier.waitOnBarrier(10, TimeUnit.SECONDS));
    }
    finally
    {
        client.close();
    }
}
 
Example 10
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 11
Source File: KafkaTestEnvironmentImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Only for the 0.8 server we need access to the zk client.
 */
public CuratorFramework createCuratorClient() {
	RetryPolicy retryPolicy = new ExponentialBackoffRetry(100, 10);
	CuratorFramework curatorClient = CuratorFrameworkFactory.newClient(standardProps.getProperty("zookeeper.connect"), retryPolicy);
	curatorClient.start();
	return curatorClient;
}
 
Example 12
Source File: TestFrameworkEdges.java    From xian with Apache License 2.0 5 votes vote down vote up
@Test
public void testSessionKilled() throws Exception
{
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();
    try
    {
        client.create().forPath("/sessionTest");

        final AtomicBoolean sessionDied = new AtomicBoolean(false);
        Watcher watcher = new Watcher()
        {
            @Override
            public void process(WatchedEvent event)
            {
                if ( event.getState() == Event.KeeperState.Expired )
                {
                    sessionDied.set(true);
                }
            }
        };
        client.checkExists().usingWatcher(watcher).forPath("/sessionTest");
        KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString());
        Assert.assertNotNull(client.checkExists().forPath("/sessionTest"));
        Assert.assertTrue(sessionDied.get());
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 13
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 14
Source File: SemaphoreClient.java    From xian with Apache License 2.0 5 votes vote down vote up
SemaphoreClient(String connectionString, String semaphorePath, Callable<Void> operation) throws IOException
{
    Timing timing = new Timing();
    this.client = CuratorFrameworkFactory.newClient(connectionString, timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
    client.start();

    this.semaphorePath = semaphorePath;
    this.operation = operation;
}
 
Example 15
Source File: ZkHelper.java    From AthenaServing with Apache License 2.0 5 votes vote down vote up
/**
 * 创建一个客户端连接
 *
 * @param connectionString
 * @return
 */
private CuratorFramework createSimpleClient(String connectionString) {
    // these are reasonable arguments for the ExponentialBackoffRetry.
    // The first retry will wait 1 second - the second will wait up to 2 seconds - the
    // third will wait up to 4 seconds.
    ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3);
    // The simplest way to get a CuratorFramework instance. This will use default values.
    // The only required arguments are the connection string and the retry policy
    CuratorFramework client = CuratorFrameworkFactory.newClient(connectionString, retryPolicy);
    client.start();
    return client;
}
 
Example 16
Source File: TestQueueSharder.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void     testSimpleDistributedQueue() throws Exception
{
    Timing                  timing = new Timing();
    CuratorFramework        client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));

    final CountDownLatch                    latch = new CountDownLatch(1);
    final BlockingQueueConsumer<String>     consumer = makeConsumer(latch);
    QueueAllocator<String, DistributedQueue<String>>    distributedQueueAllocator = makeAllocator(consumer);
    QueueSharderPolicies        policies = QueueSharderPolicies.builder().newQueueThreshold(2).thresholdCheckMs(1).build();
    QueueSharder<String, DistributedQueue<String>>  sharder = new QueueSharder<String, DistributedQueue<String>>(client, distributedQueueAllocator, "/queues", "/leader", policies);
    try
    {
        client.start();
        sharder.start();

        sharder.getQueue().put("one");
        sharder.getQueue().put("two");
        sharder.getQueue().put("three");
        sharder.getQueue().put("four");
        latch.countDown();
        timing.sleepABit();
        sharder.getQueue().put("five");
        sharder.getQueue().put("six");
        sharder.getQueue().put("seven");
        sharder.getQueue().put("eight");
        timing.sleepABit();

        Assert.assertTrue(sharder.getShardQty() > 1);

        Set<String>             consumed = Sets.newHashSet();
        for ( int i = 0; i < 8; ++i )
        {
            String s = consumer.take(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS);
            Assert.assertNotNull(s);
            consumed.add(s);
        }

        Assert.assertEquals(consumed, Sets.newHashSet("one", "two", "three", "four", "five", "six", "seven", "eight"));

        int         shardQty = sharder.getShardQty();
        sharder.close();

        // check re-open

        sharder = new QueueSharder<String, DistributedQueue<String>>(client, distributedQueueAllocator, "/queues", "/leader", policies);
        sharder.start();
        Assert.assertEquals(sharder.getShardQty(), shardQty);
    }
    finally
    {
        CloseableUtils.closeQuietly(sharder);
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 17
Source File: TestNodeCache.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void     testRebuildAgainstOtherProcesses() throws Exception
{
    NodeCache               cache = null;
    final CuratorFramework  client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();
    try
    {
        client.create().forPath("/test");
        client.create().forPath("/test/snafu", "original".getBytes());

        final CountDownLatch    latch = new CountDownLatch(1);
        cache = new NodeCache(client, "/test/snafu");
        cache.getListenable().addListener
        (
            new NodeCacheListener()
            {
                @Override
                public void nodeChanged() throws Exception
                {
                    latch.countDown();
                }
            }
        );
        cache.rebuildTestExchanger = new Exchanger<Object>();

        ExecutorService                 service = Executors.newSingleThreadExecutor();
        final NodeCache                 finalCache = cache;
        Future<Object>                  future = service.submit
        (
            new Callable<Object>()
            {
                @Override
                public Object call() throws Exception
                {
                    finalCache.rebuildTestExchanger.exchange(new Object(), 10, TimeUnit.SECONDS);

                    // simulate another process updating the node while we're rebuilding
                    client.setData().forPath("/test/snafu", "other".getBytes());

                    ChildData       currentData = finalCache.getCurrentData();
                    Assert.assertNotNull(currentData);

                    finalCache.rebuildTestExchanger.exchange(new Object(), 10, TimeUnit.SECONDS);

                    return null;
                }
            }
        );
        cache.start(false);
        future.get();

        Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
        Assert.assertNotNull(cache.getCurrentData());
        Assert.assertEquals(cache.getCurrentData().getData(), "other".getBytes());
    }
    finally
    {
        CloseableUtils.closeQuietly(cache);
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 18
Source File: TestFramework.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateModes() throws Exception
{
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();
    try
    {
        byte[] writtenBytes = {1, 2, 3};
        client.create().forPath("/test", writtenBytes); // should be persistent

        client.close();
        client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        client.start();

        byte[] readBytes = client.getData().forPath("/test");
        Assert.assertEquals(writtenBytes, readBytes);

        client.create().withMode(CreateMode.EPHEMERAL).forPath("/ghost", writtenBytes);

        client.close();
        client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        client.start();

        readBytes = client.getData().forPath("/test");
        Assert.assertEquals(writtenBytes, readBytes);
        Stat stat = client.checkExists().forPath("/ghost");
        Assert.assertNull(stat);

        String realPath = client.create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath("/pseq", writtenBytes);
        Assert.assertNotSame(realPath, "/pseq");

        client.close();
        client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        client.start();

        readBytes = client.getData().forPath(realPath);
        Assert.assertEquals(writtenBytes, readBytes);

        realPath = client.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/eseq", writtenBytes);
        Assert.assertNotSame(realPath, "/eseq");

        client.close();
        client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        client.start();

        stat = client.checkExists().forPath(realPath);
        Assert.assertNull(stat);
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 19
Source File: TestLeaderLatch.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void testProperCloseWithoutConnectionEstablished() throws Exception
{
    server.stop();

    Timing timing = new Timing();
    LeaderLatch latch = null;
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try
    {
        client.start();

        final AtomicBoolean resetCalled = new AtomicBoolean(false);
        final CountDownLatch cancelStartTaskLatch = new CountDownLatch(1);
        latch = new LeaderLatch(client, PATH_NAME)
        {
            @Override
            void reset() throws Exception
            {
                resetCalled.set(true);
                super.reset();
            }

            @Override
            protected boolean cancelStartTask()
            {
                if ( super.cancelStartTask() )
                {
                    cancelStartTaskLatch.countDown();
                    return true;
                }
                return false;
            }
        };

        latch.start();
        latch.close();
        latch = null;

        Assert.assertTrue(timing.awaitLatch(cancelStartTaskLatch));
        Assert.assertFalse(resetCalled.get());
    }
    finally
    {
        CloseableUtils.closeQuietly(latch);
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 20
Source File: ZKTools.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
public static void initClient() {
    client = CuratorFrameworkFactory.newClient(zookeeperHost + ":2181", 60 * 1000, 60 * 1000,
            new ExponentialBackoffRetry(1000, 3));
    client.start();
}