本文整理汇总了Java中org.apache.curator.framework.CuratorFrameworkFactory.newClient方法的典型用法代码示例。如果您正苦于以下问题:Java CuratorFrameworkFactory.newClient方法的具体用法?Java CuratorFrameworkFactory.newClient怎么用?Java CuratorFrameworkFactory.newClient使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.curator.framework.CuratorFrameworkFactory
的用法示例。
在下文中一共展示了CuratorFrameworkFactory.newClient方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setUp
import org.apache.curator.framework.CuratorFrameworkFactory; //导入方法依赖的package包/类
@Before
public void setUp() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
zookeeper = new ZooKeeper("127.0.0.1:2181", 10000, event -> {
if (event.getState() == Watcher.Event.KeeperState.SyncConnected) {
System.out.println("Zookeeper connected.");
} else {
throw new RuntimeException("Error connecting to zookeeper");
}
latch.countDown();
});
latch.await();
CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));
curatorFramework.start();
AsyncCuratorFramework asyncCuratorFramework = AsyncCuratorFramework.wrap(curatorFramework);
logInfoStorage = new LogInfoStorageImpl(asyncCuratorFramework);
}
示例2: checkAndGetZK
import org.apache.curator.framework.CuratorFrameworkFactory; //导入方法依赖的package包/类
/**
* 检查ZooKeeper的连接状态和它的Znode目录树
*
* @param
* @return
*/
public static CuratorFramework checkAndGetZK() {
CuratorFramework client;
try {
RetryPolicy retryPolicy =
new ExponentialBackoffRetry(configuration.ZK_RETRY_INTERVAL, configuration.ZK_RETRY_TIMES);
client = CuratorFrameworkFactory
.newClient(configuration.ZK_CONNECT_STRING
, configuration.ZK_SESSION_TIMEOUT, configuration.ZK_INIT_TIMEOUT, retryPolicy);
client.start();
client.checkExists().forPath(ZNodeStaticSetting.TASKS_PATH);
client.checkExists().forPath(ZNodeStaticSetting.MANAGERS_PATH);
client.checkExists().forPath(ZNodeStaticSetting.WORKERS_PATH);
client.checkExists().forPath(ZNodeStaticSetting.FILTERS_ROOT);
} catch (Throwable e) {
client = null;
logger.error(e.getMessage());
}
return client;
}
示例3: assertPersistEphemeralSequential
import org.apache.curator.framework.CuratorFrameworkFactory; //导入方法依赖的package包/类
@Test
public void assertPersistEphemeralSequential() throws Exception {
zkRegCenter.persistEphemeralSequential("/sequential/test_ephemeral_sequential");
zkRegCenter.persistEphemeralSequential("/sequential/test_ephemeral_sequential");
CuratorFramework client = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000));
client.start();
client.blockUntilConnected();
List<String> actual = client.getChildren().forPath("/" + ZookeeperRegistryCenterModifyTest.class.getName() + "/sequential");
assertThat(actual.size(), is(2));
for (String each : actual) {
assertThat(each, startsWith("test_ephemeral_sequential"));
}
zkRegCenter.close();
actual = client.getChildren().forPath("/" + ZookeeperRegistryCenterModifyTest.class.getName() + "/sequential");
assertTrue(actual.isEmpty());
zkRegCenter.init();
}
示例4: createConnection
import org.apache.curator.framework.CuratorFrameworkFactory; //导入方法依赖的package包/类
private static CuratorFramework createConnection() {
String url = ZkConfig.getInstance().getZkURL();
CuratorFramework framework = CuratorFrameworkFactory.newClient(url, new ExponentialBackoffRetry(100, 6));
// start connection
framework.start();
// wait 3 second to establish connect
try {
framework.blockUntilConnected(3, TimeUnit.SECONDS);
if (framework.getZookeeperClient().isConnected()) {
LOGGER.info("CuratorFramework createConnection success");
return framework;
}
} catch (InterruptedException ignored) {
LOGGER.info("CuratorFramework createConnection error", ignored);
Thread.currentThread().interrupt();
}
// fail situation
framework.close();
throw new RuntimeException("failed to connect to zookeeper service : " + url);
}
示例5: get
import org.apache.curator.framework.CuratorFrameworkFactory; //导入方法依赖的package包/类
@Override
public CuratorFramework get() {
String quorum = zookeeperConfig.getQuorum();
String statsPath = zookeeperConfig.getPropertyServicePath();
String connectionString = quorum + (statsPath == null ? "" : statsPath);
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
LOGGER.info("Initiating Curator connection to Zookeeper using: [{}]", connectionString);
// Use chroot so all subsequent paths are below /stroom-stats to avoid conflicts with hbase/zookeeper/kafka etc.
CuratorFramework client = CuratorFrameworkFactory.newClient(connectionString, retryPolicy);
client.start();
try {
// Ensure the chrooted root path exists (i.e. /stroom-stats)
Stat stat = client.checkExists().forPath("/");
if (stat == null) {
LOGGER.info("Creating chroot-ed root node inside " + statsPath);
client.create().forPath("/");
}
} catch (Exception e) {
throw new RuntimeException("Error connecting to zookeeper using connection String: " + connectionString, e);
}
return client;
}
示例6: assertContend
import org.apache.curator.framework.CuratorFrameworkFactory; //导入方法依赖的package包/类
@Test
@Ignore
public void assertContend() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000));
client.start();
client.blockUntilConnected();
ZookeeperElectionService service = new ZookeeperElectionService(HOST_AND_PORT, client, ELECTION_PATH, electionCandidate);
service.start();
ElectionCandidate anotherElectionCandidate = mock(ElectionCandidate.class);
CuratorFramework anotherClient = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000));
ZookeeperElectionService anotherService = new ZookeeperElectionService("ANOTHER_CLIENT:8899", anotherClient, ELECTION_PATH, anotherElectionCandidate);
anotherClient.start();
anotherClient.blockUntilConnected();
anotherService.start();
KillSession.kill(client.getZookeeperClient().getZooKeeper(), EmbedTestingServer.getConnectionString());
service.stop();
verify(anotherElectionCandidate).startLeadership();
}
示例7: ZookeeperOffsetHandler
import org.apache.curator.framework.CuratorFrameworkFactory; //导入方法依赖的package包/类
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();
}
示例8: setup
import org.apache.curator.framework.CuratorFrameworkFactory; //导入方法依赖的package包/类
@Before
public void setup() {
try {
zkServer = new TestingServerStarter().start();
} catch (Exception e) {
log.error("Error starting ZK server", e);
}
zkClient = CuratorFrameworkFactory.newClient(zkServer.getConnectString(),
new ExponentialBackoffRetry(200, 10, 5000));
zkClient.start();
streamStore = StreamStoreFactory.createZKStore(zkClient, executor);
TaskMetadataStore taskMetadataStore = TaskStoreFactory.createZKStore(zkClient, executor);
hostStore = HostStoreFactory.createInMemoryStore(HostMonitorConfigImpl.dummyConfig());
segmentHelperMock = SegmentHelperMock.getSegmentHelperMock();
connectionFactory = Mockito.mock(ConnectionFactory.class);
streamMetadataTasks = new StreamMetadataTasks(streamStore, hostStore, taskMetadataStore, segmentHelperMock,
executor, "host", connectionFactory);
}
示例9: ServiceRegistry
import org.apache.curator.framework.CuratorFrameworkFactory; //导入方法依赖的package包/类
/**
* <p>Retrieves the Zookeeper service URI from the environemnt and initiates the client.</p>
*/
@Inject
public ServiceRegistry() {
try {
String zookeeperUri = System.getenv("ZOOKEEPER_URI");
zookeeper = CuratorFrameworkFactory.newClient(zookeeperUri, new RetryNTimes(5, 1000));
zookeeper.start();
zonePaths = new ConcurrentHashMap<>();
} catch (Exception ex) {
throw new RuntimeException(ex.getLocalizedMessage());
}
}
示例10: createProperties
import org.apache.curator.framework.CuratorFrameworkFactory; //导入方法依赖的package包/类
@Override
protected Properties createProperties() throws IOException {
Properties prop = super.createProperties();
if (zkLocation == null || "".equals(zkLocation))
zkLocation = environment.getProperty(this.zkLocationEnvName);
if (zkLocation == null) {
logger.warn("ZK connection string env var does not exist, skip zk based configuration.");
return prop;
}
try {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient(zkLocation, retryPolicy);
client.start();
int count = CuratorUtils.loadToProperties(client, prop, "/");
logger.info(String.format("Loaded %d properties from zookeeper.", count));
client.close();
} catch (Exception ex) {
logger.warn("Failed to load configuration from ZK.", ex);
}
return prop;
}
示例11: CacheClusterService
import org.apache.curator.framework.CuratorFrameworkFactory; //导入方法依赖的package包/类
private CacheClusterService(Config config) {
try {
if (config == null) {
config = ConfigFactory.load();
}
this.zookeeperConnectionUrl = config.getString("zookeeper.connection_url");
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
zkClient = CuratorFrameworkFactory.newClient(zookeeperConnectionUrl, retryPolicy);
zkClient.start();
clusterGlobalLock = new InterProcessReadWriteLock(zkClient, Constants.CACHE_CLUSTER_PATH);
cacheClusterViewer = CacheClusterViewerFactory.getCacheClusterViewer();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例12: connect
import org.apache.curator.framework.CuratorFrameworkFactory; //导入方法依赖的package包/类
/**
* 获取curator对象
* @return
*/
public CuratorFramework connect(){
//声明一个重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient(CuratorConfiguration.address, retryPolicy);
client.start();
return client;
}
示例13: testSomeNodes
import org.apache.curator.framework.CuratorFrameworkFactory; //导入方法依赖的package包/类
@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);
}
}
示例14: setUp
import org.apache.curator.framework.CuratorFrameworkFactory; //导入方法依赖的package包/类
@Before
public void setUp() throws Exception {
Configurator
.initialize("FastMQ", Thread.currentThread().getContextClassLoader(), "log4j2.xml");
CountDownLatch initLatch = new CountDownLatch(1);
CuratorFramework curatorFramework = CuratorFrameworkFactory
.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));
curatorFramework.start();
AsyncCuratorFramework asyncCuratorFramework = AsyncCuratorFramework.wrap(curatorFramework);
when(offsetStorage.queryOffset(any())).thenReturn(new Offset());
ledgerManager = new LogManagerImpl("ledger-manager-read-test-name", new BookKeeperConfig(),
new BookKeeper("127.0.0.1:2181"), asyncCuratorFramework,
new LogInfoStorageImpl(asyncCuratorFramework), this.offsetStorage);
ledgerManager.init(new CommonCallback<Void, LedgerStorageException>() {
@Override
public void onCompleted(Void data, Version version) {
initLatch.countDown();
}
@Override
public void onThrowable(LedgerStorageException throwable) {
throwable.printStackTrace();
initLatch.countDown();
}
});
initLatch.await();
}
示例15: setUp
import org.apache.curator.framework.CuratorFrameworkFactory; //导入方法依赖的package包/类
@Before
public void setUp() throws Exception {
zkServer = new TestingServer();
client = CuratorFrameworkFactory
.newClient("localhost:" + zkServer.getPort(),
new ExponentialBackoffRetry(1000, 3));
client.start();
EnsurePath ensurePath = new EnsurePath(AGENT_PATH);
ensurePath.ensure(client.getZookeeperClient());
doSetUp();
}
开发者ID:moueimei,项目名称:flume-release-1.7.0,代码行数:13,代码来源:TestAbstractZooKeeperConfigurationProvider.java