本文整理汇总了Java中org.apache.curator.framework.CuratorFrameworkFactory类的典型用法代码示例。如果您正苦于以下问题:Java CuratorFrameworkFactory类的具体用法?Java CuratorFrameworkFactory怎么用?Java CuratorFrameworkFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CuratorFrameworkFactory类属于org.apache.curator.framework包,在下文中一共展示了CuratorFrameworkFactory类的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: init
import org.apache.curator.framework.CuratorFrameworkFactory; //导入依赖的package包/类
@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);
}
}
示例3: get
import org.apache.curator.framework.CuratorFrameworkFactory; //导入依赖的package包/类
@Override
public CuratorFramework get() {
String quorum = zookeeperConfig.getQuorum();
String serviceDiscoveryPath = zookeeperConfig.getServiceDiscoveryPath();
String connectionString = quorum + (serviceDiscoveryPath == null ? "" : serviceDiscoveryPath);
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
LOGGER.info("Initiating Curator connection to Zookeeper using: [{}]", connectionString);
// Use chroot so all subsequent paths are below /stroom-services to avoid conflicts with hbase/zookeeper/kafka etc.
CuratorFramework client = CuratorFrameworkFactory.newClient(connectionString, retryPolicy);
client.start();
try {
//Ensure the chrooted path for stroom-services exists
Stat stat = client.checkExists().forPath("/");
if (stat == null) {
LOGGER.info("Creating chroot-ed root node inside " + serviceDiscoveryPath);
client.create().forPath("/");
}
} catch (Exception e) {
throw new RuntimeException("Error connecting to zookeeper using connection String: " + connectionString, e);
}
return client;
}
示例4: buildConnection
import org.apache.curator.framework.CuratorFrameworkFactory; //导入依赖的package包/类
private static CuratorFramework buildConnection(String url) {
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.usingNamespace("");
}
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
// fail situation
curatorFramework.close();
throw new RuntimeException("failed to connect to zookeeper service : " + url);
}
示例5: testServer
import org.apache.curator.framework.CuratorFrameworkFactory; //导入依赖的package包/类
public void testServer(){
try {
TestingServer server=new TestingServer(2181,new File("/"));
server.start();
CuratorFramework curatorFramework = CuratorFrameworkFactory.
builder().
connectString(server.getConnectString()).
sessionTimeoutMs(1000).
retryPolicy(new RetryNTimes(3, 1000)).
build();
curatorFramework.start();
System.out.println(curatorFramework.getChildren().forPath("/"));
curatorFramework.close();
server.stop();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
示例6: SingularityCuratorProvider
import org.apache.curator.framework.CuratorFrameworkFactory; //导入依赖的package包/类
@Inject
public SingularityCuratorProvider(final SingularityConfiguration configuration, final Set<ConnectionStateListener> connectionStateListeners) {
checkNotNull(configuration, "configuration is null");
checkNotNull(connectionStateListeners, "connectionStateListeners is null");
ZooKeeperConfiguration zookeeperConfig = configuration.getZooKeeperConfiguration();
this.curatorFramework = CuratorFrameworkFactory.builder()
.defaultData(null)
.sessionTimeoutMs(zookeeperConfig.getSessionTimeoutMillis())
.connectionTimeoutMs(zookeeperConfig.getConnectTimeoutMillis())
.connectString(zookeeperConfig.getQuorum())
.retryPolicy(new ExponentialBackoffRetry(zookeeperConfig.getRetryBaseSleepTimeMilliseconds(), zookeeperConfig.getRetryMaxTries()))
.namespace(zookeeperConfig.getZkNamespace()).build();
for (ConnectionStateListener connectionStateListener : connectionStateListeners) {
curatorFramework.getConnectionStateListenable().addListener(connectionStateListener);
}
}
示例7: run
import org.apache.curator.framework.CuratorFrameworkFactory; //导入依赖的package包/类
@Override
public void run(String... strings) throws Exception {
client = CuratorFrameworkFactory.newClient(zookeeperConnString,
new ExponentialBackoffRetry(1000, Integer.MAX_VALUE));
client.start();
client.getZookeeperClient().blockUntilConnectedOrTimedOut();
leaderLatch = new LeaderLatch(client, "/http-job-scheduler/leader",
ManagementFactory.getRuntimeMXBean().getName());
leaderLatch.addListener(new LeaderLatchListener() {
@Override
public void isLeader() {
setMaster(true);
masterJobScheduler.resume();
}
@Override
public void notLeader() {
setMaster(false);
masterJobScheduler.pause();
}
});
leaderLatch.start();
}
示例8: setUp
import org.apache.curator.framework.CuratorFrameworkFactory; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
Configurator
.initialize("FastMQ", Thread.currentThread().getContextClassLoader(), "log4j2.xml");
Log log = new Log();
LogSegment logSegment = new LogSegment();
logSegment.setLedgerId(ledgerId);
logSegment.setTimestamp(System.currentTimeMillis());
log.setSegments(Collections.singletonList(logSegment));
when(logInfoStorage.getLogInfo(any())).thenReturn(log);
CuratorFramework curatorFramework = CuratorFrameworkFactory
.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));
curatorFramework.start();
asyncCuratorFramework = AsyncCuratorFramework.wrap(curatorFramework);
offsetStorage = new ZkOffsetStorageImpl(logInfoStorage, asyncCuratorFramework);
}
示例9: CuratorUtils
import org.apache.curator.framework.CuratorFrameworkFactory; //导入依赖的package包/类
public CuratorUtils(String connectString, Host host) {
this.host = host;
client = CuratorFrameworkFactory.builder()
.connectString(connectString)
.sessionTimeoutMs(10000)
.connectionTimeoutMs(10000)
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.build();
client.start();
try {
path = generatePath();
if (null == path) {
log.error("init curator failed due to path is null!");
throw new DriverException("init curator failed due to path is null, servie will down");
}
} catch (Exception e) {
log.error("CuratorUtils construct failed, service will down!");
client.close();
System.exit(-1);
}
}
示例10: curatorFramework
import org.apache.curator.framework.CuratorFrameworkFactory; //导入依赖的package包/类
@Bean(initMethod = "start", destroyMethod = "close")
@Lazy(false)
public CuratorFramework curatorFramework() {
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory
.builder();
int sessionTimeoutMs = Integer.parseInt(env.getProperty(
"rpc.client.zookeeper.session.timeout.ms", "5000"));
int connectionTimeoutMs = Integer.parseInt(env.getProperty(
"rpc.client.zookeeper.connection.timeout.ms", "5000"));
builder.connectString(
env.getProperty("rpc.client.zookeeper.connect.string"))
.sessionTimeoutMs(sessionTimeoutMs)
.connectionTimeoutMs(connectionTimeoutMs)
.retryPolicy(this.retryPolicy());
//.aclProvider(this.aclProvider()).authorization(this.authInfo());
return builder.build();
}
示例11: curatorFramework
import org.apache.curator.framework.CuratorFrameworkFactory; //导入依赖的package包/类
@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();
}
示例12: curatorFramework
import org.apache.curator.framework.CuratorFrameworkFactory; //导入依赖的package包/类
@Bean(initMethod = "start", destroyMethod = "close")
public CuratorFramework curatorFramework() {
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory
.builder();
int sessionTimeoutMs = Integer.parseInt(env.getProperty(
"rpc.client.zookeeper.session.timeout.ms", "5000"));
int connectionTimeoutMs = Integer.parseInt(env.getProperty(
"rpc.client.zookeeper.connection.timeout.ms", "5000"));
builder.connectString(
env.getProperty("rpc.client.zookeeper.connect.string"))
.sessionTimeoutMs(sessionTimeoutMs)
.connectionTimeoutMs(connectionTimeoutMs)
.retryPolicy(this.retryPolicy())
.aclProvider(this.aclProvider()).authorization(this.authInfo());
return builder.build();
}
示例13: ZkService
import org.apache.curator.framework.CuratorFrameworkFactory; //导入依赖的package包/类
/**
* 创建ZK连接
* @param connectString ZK服务器地址列表
* @param sessionTimeout Session超时时间
*/
public ZkService(String connectString, int sessionTimeout) throws Exception {
CuratorFrameworkFactory.Builder builder;
builder = CuratorFrameworkFactory.builder()
.connectString(connectString)
.namespace("")
.authorization("digest", auth.getBytes())
.retryPolicy(new RetryNTimes(Integer.MAX_VALUE, 1000))
.connectionTimeoutMs(sessionTimeout);
client = builder.build();
client.start();
if(!client.blockUntilConnected(20, TimeUnit.SECONDS)) {
throw new Exception("zookeeper connected failed!");
}
tableVersions = new HashMap<>();
cache = new HashMap<>();
}
示例14: 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;
}
示例15: createConnection
import org.apache.curator.framework.CuratorFrameworkFactory; //导入依赖的package包/类
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);
}