org.apache.curator.framework.CuratorFrameworkFactory Java Examples
The following examples show how to use
org.apache.curator.framework.CuratorFrameworkFactory.
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: ZkConnection.java From xian with Apache License 2.0 | 6 votes |
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 #2
Source File: TestServiceDiscoveryBuilder.java From xian with Apache License 2.0 | 6 votes |
@Test public void testSetSerializer() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); ServiceDiscoveryBuilder<Object> builder = ServiceDiscoveryBuilder.builder(Object.class).client(client); builder.serializer(new InstanceSerializer<Object>() { @Override public byte[] serialize(ServiceInstance<Object> instance) { return null; } @Override public ServiceInstance<Object> deserialize(byte[] bytes) { return null; } }); ServiceDiscoveryImpl<?> discovery = (ServiceDiscoveryImpl<?>) builder.basePath("/path").build(); Assert.assertNotNull(discovery.getSerializer(), "default serializer not set"); Assert.assertFalse(discovery.getSerializer() instanceof JsonInstanceSerializer, "set serializer is JSON"); }
Example #3
Source File: TestFramework.java From xian with Apache License 2.0 | 6 votes |
@Test public void testCreatingParentsTheSame() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); try { client.start(); Assert.assertNull(client.checkExists().forPath("/one/two")); client.create().creatingParentContainersIfNeeded().forPath("/one/two/three"); Assert.assertNotNull(client.checkExists().forPath("/one/two")); client.delete().deletingChildrenIfNeeded().forPath("/one"); Assert.assertNull(client.checkExists().forPath("/one")); Assert.assertNull(client.checkExists().forPath("/one/two")); client.checkExists().creatingParentContainersIfNeeded().forPath("/one/two/three"); Assert.assertNotNull(client.checkExists().forPath("/one/two")); Assert.assertNull(client.checkExists().forPath("/one/two/three")); } finally { CloseableUtils.closeQuietly(client); } }
Example #4
Source File: ZKGuidGeneratorTest.java From fastjgame with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws InterruptedException { final CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder() .namespace("test") .connectString("127.0.0.1:2181") .connectionTimeoutMs(30 * 1000) .sessionTimeoutMs(30 * 1000) .retryPolicy(new BackoffRetryForever()); final CuratorClientMgr curatorClientMgr = new CuratorClientMgr(builder, new DefaultThreadFactory("CURATOR_BACKGROUD")); try { doTest(curatorClientMgr, "playerGuidGenerator"); doTest(curatorClientMgr, "monsterGuidGenerator"); } finally { curatorClientMgr.shutdown(); } }
Example #5
Source File: TestFrameworkEdges.java From xian with Apache License 2.0 | 6 votes |
@Test public void testFailure() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), 100, 100, new RetryOneTime(1)); client.start(); try { client.checkExists().forPath("/hey"); client.checkExists().inBackground().forPath("/hey"); server.stop(); client.checkExists().forPath("/hey"); Assert.fail(); } catch ( KeeperException.ConnectionLossException e ) { // correct } finally { CloseableUtils.closeQuietly(client); } }
Example #6
Source File: TestFramework.java From xian with Apache License 2.0 | 6 votes |
@Test public void testExistsCreatingParents() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); try { client.start(); Assert.assertNull(client.checkExists().forPath("/one/two")); client.checkExists().creatingParentContainersIfNeeded().forPath("/one/two/three"); Assert.assertNotNull(client.checkExists().forPath("/one/two")); Assert.assertNull(client.checkExists().forPath("/one/two/three")); Assert.assertNull(client.checkExists().creatingParentContainersIfNeeded().forPath("/one/two/three")); } finally { CloseableUtils.closeQuietly(client); } }
Example #7
Source File: ServiceManageControllerTest.java From sofa-dashboard with Apache License 2.0 | 6 votes |
@Before public void before() throws Exception { restTemplate = new RestTemplate(); // 初始化 zk 节点 client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry( 1000, 3)); client.start(); int index = 0; while (registryDataCache.fetchService().size() == 0 && index++ < tryTimes) { initZookeeperRpcData(); } if (registryDataCache.fetchService().size() == 0) { List<RpcService> providerList = new ArrayList<>(); RpcService rpcService = new RpcService(); rpcService.setServiceName("serviceId1"); providerList.add(rpcService); registryDataCache.addService(providerList); } }
Example #8
Source File: ZookeeperOffsetHandler.java From flink with Apache License 2.0 | 6 votes |
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 #9
Source File: TestFrameworkEdges.java From xian with Apache License 2.0 | 6 votes |
@Test public void testStopped() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); try { client.start(); client.getData(); } finally { CloseableUtils.closeQuietly(client); } try { client.getData(); Assert.fail(); } catch ( Exception e ) { // correct } }
Example #10
Source File: HelloServerConfig.java From jigsaw-payment with Apache License 2.0 | 6 votes |
@Bean(name = "curator-framework") public CuratorFramework curatorFramework() { return CuratorFrameworkFactory .builder() .connectString( env.getProperty("rpc.server.zookeeper.connect.string")) .sessionTimeoutMs( Integer.parseInt(env.getProperty( "rpc.server.zookeeper.session.timeout.ms", "10000"))) .connectionTimeoutMs( Integer.parseInt(env.getProperty( "rpc.server.zookeeper.connection.timeout.ms", "10000"))).retryPolicy(this.retryPolicy()) .aclProvider(this.aclProvider()).authorization(this.authInfo()) .build(); }
Example #11
Source File: TestFramework.java From xian with Apache License 2.0 | 6 votes |
@Test public void testDeleteWithChildren() throws Exception { CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); CuratorFramework client = builder.connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).build(); client.start(); try { client.create().creatingParentsIfNeeded().forPath("/one/two/three/four/five/six", "foo".getBytes()); client.delete().deletingChildrenIfNeeded().forPath("/one/two/three/four/five"); Assert.assertNull(client.checkExists().forPath("/one/two/three/four/five")); client.delete().deletingChildrenIfNeeded().forPath("/one/two"); Assert.assertNull(client.checkExists().forPath("/one/two")); } finally { CloseableUtils.closeQuietly(client); } }
Example #12
Source File: TestFrameworkEdges.java From xian with Apache License 2.0 | 6 votes |
@Test public void testGetAclNoStat() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); client.start(); try { try { client.getACL().forPath("/"); } catch ( NullPointerException e ) { Assert.fail(); } } finally { CloseableUtils.closeQuietly(client); } }
Example #13
Source File: TestFramework.java From xian with Apache License 2.0 | 6 votes |
@Test public void testSequentialWithTrailingSeparator() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start(); try { client.create().forPath("/test"); //This should create a node in the form of "/test/00000001" String path = client.create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath("/test/"); Assert.assertTrue(path.startsWith("/test/")); } finally { CloseableUtils.closeQuietly(client); } }
Example #14
Source File: ZkUtils.java From DDMQ with Apache License 2.0 | 6 votes |
public static void init() { try { curatorClient = CuratorFrameworkFactory .builder() .connectString(zkConfig.getZkAddrs()) .sessionTimeoutMs(zkConfig.getZkSessionTimeoutMs()) .retryPolicy(new BoundedExponentialBackoffRetry(zkConfig.getBaseSleepTimeMs(), zkConfig.getMaxSleepMs(), zkConfig.getMaxRetries())) .build(); if (curatorClient.getState() == CuratorFrameworkState.LATENT) { curatorClient.start(); } ZooKeeperConfigurationSource zkConfigSource = new ZooKeeperConfigurationSource(curatorClient, Constants.META_BASE_ZK_PATH); zkConfigSource.start(); DynamicWatchedConfiguration zkDynamicConfig = new DynamicWatchedConfiguration(zkConfigSource); ConfigurationManager.install(zkDynamicConfig); } catch (Exception e) { LOGGER.error("ZkUtils getCuratorClient err:{}", e.getMessage(), e); } }
Example #15
Source File: ZkDiscoveryService.java From iotplatform with Apache License 2.0 | 6 votes |
@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 #16
Source File: TestDistributedDelayQueue.java From xian with Apache License 2.0 | 6 votes |
@Test public void testBasic() throws Exception { Timing timing = new Timing(); DistributedDelayQueue<Long> queue = null; CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); client.start(); try { BlockingQueueConsumer<Long> consumer = new BlockingQueueConsumer<Long>(Mockito.mock(ConnectionStateListener.class)); queue = QueueBuilder.builder(client, consumer, new LongSerializer(), "/test").buildDelayQueue(); queue.start(); queue.put(1L, System.currentTimeMillis() + 1000); Thread.sleep(100); Assert.assertEquals(consumer.size(), 0); // delay hasn't been reached Long value = consumer.take(timing.forWaiting().seconds(), TimeUnit.SECONDS); Assert.assertEquals(value, Long.valueOf(1)); } finally { CloseableUtils.closeQuietly(queue); CloseableUtils.closeQuietly(client); } }
Example #17
Source File: TestSharedCount.java From xian with Apache License 2.0 | 6 votes |
@Test public void testSimple() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); SharedCount count = new SharedCount(client, "/count", 0); try { client.start(); count.start(); Assert.assertTrue(count.trySetCount(1)); Assert.assertTrue(count.trySetCount(2)); Assert.assertTrue(count.trySetCount(10)); Assert.assertEquals(count.getCount(), 10); } finally { CloseableUtils.closeQuietly(count); CloseableUtils.closeQuietly(client); } }
Example #18
Source File: TestTempFramework.java From xian with Apache License 2.0 | 6 votes |
@Test public void testBasic() throws Exception { CuratorTempFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).buildTemp(); try { client.inTransaction().create().forPath("/foo", "data".getBytes()).and().commit(); byte[] bytes = client.getData().forPath("/foo"); Assert.assertEquals(bytes, "data".getBytes()); } finally { CloseableUtils.closeQuietly(client); } }
Example #19
Source File: ZookeeperNamingService.java From brpc-java with Apache License 2.0 | 6 votes |
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 #20
Source File: TestBlockUntilConnected.java From xian with Apache License 2.0 | 6 votes |
/** * Test the case where we are not currently connected and time out before a * connection becomes available. */ @Test public void testBlockUntilConnectedConnectTimeout() { //Kill the server CloseableUtils.closeQuietly(server); CuratorFramework client = CuratorFrameworkFactory.builder(). connectString(server.getConnectString()). retryPolicy(new RetryOneTime(1)). build(); try { client.start(); Assert.assertFalse(client.blockUntilConnected(5, TimeUnit.SECONDS), "Connected"); } catch ( InterruptedException e ) { Assert.fail("Unexpected interruption"); } finally { CloseableUtils.closeQuietly(client); } }
Example #21
Source File: TestPathChildrenCache.java From xian with Apache License 2.0 | 6 votes |
@Test public void testModes() throws Exception { Timing timing = new Timing(); CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); client.start(); try { client.create().forPath("/test"); for ( boolean cacheData : new boolean[]{false, true} ) { internalTestMode(client, cacheData); client.delete().forPath("/test/one"); client.delete().forPath("/test/two"); } } finally { client.close(); } }
Example #22
Source File: TestFramework.java From xian with Apache License 2.0 | 6 votes |
@Test public void testDelete() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start(); try { client.create().forPath("/head"); Assert.assertNotNull(client.checkExists().forPath("/head")); client.delete().forPath("/head"); Assert.assertNull(client.checkExists().forPath("/head")); } finally { CloseableUtils.closeQuietly(client); } }
Example #23
Source File: SimpleLeaderManager.java From sdmq with Apache License 2.0 | 6 votes |
public void init() { CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder() .connectString(properties.getServerList()) .retryPolicy(new ExponentialBackoffRetry(properties.getBaseSleepTimeMilliseconds(), properties.getMaxRetries(), properties.getMaxSleepTimeMilliseconds())) .namespace(ServerNode.NAMESPACE); framework = builder.build(); framework.start(); leaderLatch = new LeaderLatch(framework, ServerNode.LEADERLATCH, serverName, LeaderLatch.CloseMode.NOTIFY_LEADER); for (LeaderLatchListener listener : listeners) { leaderLatch.addListener(listener); } LOGGER.info("starting Queue Master Slave Model ..."); start(); }
Example #24
Source File: CuratorServiceImpl.java From titus-control-plane with Apache License 2.0 | 6 votes |
@Inject public CuratorServiceImpl(ZookeeperConfiguration configs, ZookeeperClusterResolver clusterResolver, Registry registry) { isConnectedGauge = PolledMeter.using(registry).withName("titusMaster.curator.isConnected").monitorValue(new AtomicInteger()); Optional<String> connectString = clusterResolver.resolve(); if (!connectString.isPresent()) { // Fail early if connection to zookeeper not defined LOG.error("Zookeeper connectivity details not found"); throw new IllegalStateException("Zookeeper connectivity details not found"); } curator = CuratorFrameworkFactory.builder() .compressionProvider(new GzipCompressionProvider()) .connectionTimeoutMs(configs.getZkConnectionTimeoutMs()) .retryPolicy(new ExponentialBackoffRetry(configs.getZkConnectionRetrySleepMs(), configs.getZkConnectionMaxRetries())) .connectString(connectString.get()) .build(); }
Example #25
Source File: TestChildReaper.java From xian with Apache License 2.0 | 5 votes |
@Test public void testSomeNodes() throws Exception { Timing timing = new Timing(); ChildReaper reaper = null; CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); try { client.start(); Random r = new Random(); int nonEmptyNodes = 0; for ( int i = 0; i < 10; ++i ) { client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i)); if ( r.nextBoolean() ) { client.create().forPath("/test/" + Integer.toString(i) + "/foo"); ++nonEmptyNodes; } } reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1); reaper.start(); timing.forWaiting().sleepABit(); Stat stat = client.checkExists().forPath("/test"); Assert.assertEquals(stat.getNumChildren(), nonEmptyNodes); } finally { CloseableUtils.closeQuietly(reaper); CloseableUtils.closeQuietly(client); } }
Example #26
Source File: ZooKeeperRegister.java From turbo-rpc with Apache License 2.0 | 5 votes |
@Override public void init(List<HostPort> hostPorts) { watcherMap = new ConcurrentHashMap<>(); String connectString = hostPorts.stream().map(i -> i.toString()).collect(Collectors.joining(",")); RetryPolicy retryPolicy = new ForeverRetryPolicy(1000, 60 * 1000); client = CuratorFrameworkFactory.newClient(connectString, 1000 * 10, 1000 * 3, retryPolicy); client.start(); }
Example #27
Source File: ZkDistributedSequence.java From javatech with Creative Commons Attribution Share Alike 4.0 International | 5 votes |
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 #28
Source File: ZooKeeperDiscover.java From turbo-rpc with Apache License 2.0 | 5 votes |
@Override public void init(List<HostPort> hostPorts) { watchers = new ConcurrentArrayList<>(); String connectString = hostPorts.stream().map(i -> i.toString()).collect(Collectors.joining(",")); RetryPolicy retryPolicy = new ForeverRetryPolicy(1000, 60 * 1000); client = CuratorFrameworkFactory.newClient(connectString, 1000 * 10, 1000 * 3, retryPolicy); client.start(); }
Example #29
Source File: CuratorClientMgr.java From fastjgame with Apache License 2.0 | 5 votes |
private static void checkThreadFactory(CuratorFrameworkFactory.Builder builder) { if (builder.getThreadFactory() == null) { return; } // Curator有一点非常坑爹 // 内部使用的是守护线程,如果用户指定了线程工厂,设置错误的化,则可能导致JVM无法退出。 // 我们在此拦截,以保证安全性 final ThreadFactory daemonThreadFactory = new ThreadFactoryBuilder() .setThreadFactory(builder.getThreadFactory()) .setDaemon(true) .build(); builder.threadFactory(daemonThreadFactory); }
Example #30
Source File: ZkLock.java From tunnel with Apache License 2.0 | 5 votes |
private static CuratorFramework startZkClient(String address) { CuratorFramework zkClient = CuratorFrameworkFactory.builder() .connectString(address) .connectionTimeoutMs(Integer.MAX_VALUE) .sessionTimeoutMs(Integer.MAX_VALUE) .retryPolicy(new RetryNTimes(5, 1000)) .build(); zkClient.start(); log.info("ZkLog ---- ZkClient Started"); return zkClient; }