Search in sources :

Example 1 with Builder

use of org.apache.curator.framework.CuratorFrameworkFactory.Builder in project Saturn by vipshop.

the class ZookeeperRegistryCenter method buildZkClient.

private CuratorFramework buildZkClient() {
    if (zkConfig.isUseNestedZookeeper()) {
        NestedZookeeperServers.getInstance().startServerIfNotStarted(zkConfig.getNestedPort(), zkConfig.getNestedDataDir());
    }
    Builder builder = CuratorFrameworkFactory.builder().connectString(zkConfig.getServerLists()).retryPolicy(new ExponentialBackoffRetry(zkConfig.getBaseSleepTimeMilliseconds(), zkConfig.getMaxRetries(), zkConfig.getMaxSleepTimeMilliseconds())).namespace(zkConfig.getNamespace());
    if (0 != zkConfig.getSessionTimeoutMilliseconds()) {
        sessionTimeout = zkConfig.getSessionTimeoutMilliseconds();
    } else {
        sessionTimeout = calculateSessionTimeout();
    }
    builder.sessionTimeoutMs(sessionTimeout);
    int connectionTimeout;
    if (0 != zkConfig.getConnectionTimeoutMilliseconds()) {
        connectionTimeout = zkConfig.getConnectionTimeoutMilliseconds();
    } else {
        connectionTimeout = calculateConnectionTimeout();
    }
    builder.connectionTimeoutMs(connectionTimeout);
    if (!Strings.isNullOrEmpty(zkConfig.getDigest())) {
        builder.authorization("digest", zkConfig.getDigest().getBytes(Charset.forName(Constant.CHARSET_UTF8))).aclProvider(new ACLProvider() {

            @Override
            public List<ACL> getDefaultAcl() {
                return ZooDefs.Ids.CREATOR_ALL_ACL;
            }

            @Override
            public List<ACL> getAclForPath(final String path) {
                return ZooDefs.Ids.CREATOR_ALL_ACL;
            }
        });
    }
    LogUtils.info(log, LogEvents.ExecutorEvent.COMMON, "Saturn job: zookeeper registry center init, server lists is: {}, connection_timeout: {}, session_timeout: {}, retry_times: {}", zkConfig.getServerLists(), connectionTimeout, sessionTimeout, zkConfig.getMaxRetries());
    return builder.build();
}
Also used : ACLProvider(org.apache.curator.framework.api.ACLProvider) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Builder(org.apache.curator.framework.CuratorFrameworkFactory.Builder) List(java.util.List)

Example 2 with Builder

use of org.apache.curator.framework.CuratorFrameworkFactory.Builder in project Saturn by vipshop.

the class CuratorRepositoryImpl method connect.

@Override
public CuratorFramework connect(final String connectString, final String namespace, final String digest) {
    Builder builder = CuratorFrameworkFactory.builder().connectString(connectString).sessionTimeoutMs(SESSION_TIMEOUT).connectionTimeoutMs(CONNECTION_TIMEOUT).retryPolicy(new ExponentialBackoffRetry(1000, 3, 3000));
    if (namespace != null) {
        builder.namespace(namespace);
    }
    if (!Strings.isNullOrEmpty(digest)) {
        builder.authorization("digest", digest.getBytes(Charset.forName("UTF-8"))).aclProvider(new ACLProvider() {

            @Override
            public List<ACL> getDefaultAcl() {
                return ZooDefs.Ids.CREATOR_ALL_ACL;
            }

            @Override
            public List<ACL> getAclForPath(final String path) {
                return ZooDefs.Ids.CREATOR_ALL_ACL;
            }
        });
    }
    CuratorFramework client = builder.build();
    client.start();
    boolean established = false;
    try {
        established = client.blockUntilConnected(WAITING_SECONDS, TimeUnit.SECONDS);
    } catch (final InterruptedException ex) {
        Thread.currentThread().interrupt();
    }
    if (established) {
        return client;
    }
    CloseableUtils.closeQuietly(client);
    return null;
}
Also used : ACLProvider(org.apache.curator.framework.api.ACLProvider) CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Builder(org.apache.curator.framework.CuratorFrameworkFactory.Builder) List(java.util.List)

Example 3 with Builder

use of org.apache.curator.framework.CuratorFrameworkFactory.Builder in project x-pipe by ctripcorp.

the class MetaServerPrepareResourcesAndStart method connectToZk.

private CuratorFramework connectToZk(String connectString) throws InterruptedException {
    Builder builder = CuratorFrameworkFactory.builder();
    builder.connectionTimeoutMs(3000);
    builder.connectString(connectString);
    builder.maxCloseWaitMs(3000);
    builder.namespace("xpipe");
    builder.retryPolicy(new RetryNTimes(3, 1000));
    builder.sessionTimeoutMs(5000);
    CuratorFramework client = builder.build();
    client.start();
    client.blockUntilConnected();
    return client;
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) Builder(org.apache.curator.framework.CuratorFrameworkFactory.Builder)

Example 4 with Builder

use of org.apache.curator.framework.CuratorFrameworkFactory.Builder in project x-pipe by ctripcorp.

the class DefaultZkConfig method create.

@Override
public CuratorFramework create(String address) throws InterruptedException {
    Builder builder = CuratorFrameworkFactory.builder();
    builder.connectionTimeoutMs(getZkConnectionTimeoutMillis());
    builder.connectString(address);
    builder.maxCloseWaitMs(getZkCloseWaitMillis());
    builder.namespace(getZkNamespace());
    builder.retryPolicy(new RetryNTimes(getZkRetries(), getSleepMsBetweenRetries()));
    builder.sessionTimeoutMs(getZkSessionTimeoutMillis());
    builder.threadFactory(XpipeThreadFactory.create("Xpipe-ZK-" + address, true));
    logger.info("[create]{}, {}", Codec.DEFAULT.encode(this), address);
    CuratorFramework curatorFramework = builder.build();
    curatorFramework.start();
    curatorFramework.blockUntilConnected(waitForZkConnectedMillis(), TimeUnit.MILLISECONDS);
    return curatorFramework;
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) Builder(org.apache.curator.framework.CuratorFrameworkFactory.Builder)

Aggregations

Builder (org.apache.curator.framework.CuratorFrameworkFactory.Builder)4 CuratorFramework (org.apache.curator.framework.CuratorFramework)3 List (java.util.List)2 ACLProvider (org.apache.curator.framework.api.ACLProvider)2 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)2 RetryNTimes (org.apache.curator.retry.RetryNTimes)2