Search in sources :

Example 1 with RegistryConfig

use of com.alipay.sofa.ark.config.RegistryConfig in project sofa-ark by alipay.

the class ZookeeperConfigActivator method start.

@Override
public void start(final PluginContext context) {
    if (!enableZkServer) {
        LOGGER.warn("config server is disabled.");
        return;
    }
    String config = ArkConfigs.getStringValue(Constants.CONFIG_SERVER_ADDRESS);
    RegistryConfig registryConfig = ZookeeperConfigurator.buildConfig(config);
    String address = registryConfig.getAddress();
    int idx = address.indexOf(Constants.ZOOKEEPER_CONTEXT_SPLIT);
    if (idx != -1) {
        rootPath = address.substring(idx);
        if (!rootPath.endsWith(Constants.ZOOKEEPER_CONTEXT_SPLIT)) {
            rootPath += Constants.ZOOKEEPER_CONTEXT_SPLIT;
        }
        address = address.substring(0, idx);
    }
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFrameworkFactory.Builder zkClientBuilder = CuratorFrameworkFactory.builder().connectString(address).sessionTimeoutMs(3 * registryConfig.getConnectTimeout()).connectionTimeoutMs(registryConfig.getConnectTimeout()).canBeReadOnly(false).retryPolicy(retryPolicy).defaultData(null);
    List<AuthInfo> authInfos = buildAuthInfo(registryConfig);
    if (!authInfos.isEmpty()) {
        zkClientBuilder = zkClientBuilder.aclProvider(getDefaultAclProvider()).authorization(authInfos);
    }
    zkClient = zkClientBuilder.build();
    zkClient.getConnectionStateListenable().addListener(new ConnectionStateListener() {

        @Override
        public void stateChanged(CuratorFramework client, ConnectionState newState) {
            LOGGER.info("Reconnect to zookeeper, re-register config resource.");
            if (newState == ConnectionState.RECONNECTED) {
                unSubscribeIpConfig();
                registryResource(ipResourcePath, CreateMode.EPHEMERAL);
                subscribeIpConfig();
            }
        }
    });
    zkClient.start();
    registryResource(bizResourcePath, CreateMode.PERSISTENT);
    registryResource(ipResourcePath, CreateMode.EPHEMERAL);
    subscribeIpConfig();
    subscribeBizConfig();
    registerEventHandler(context);
}
Also used : RegistryConfig(com.alipay.sofa.ark.config.RegistryConfig) AuthInfo(org.apache.curator.framework.AuthInfo) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) CuratorFramework(org.apache.curator.framework.CuratorFramework) ConnectionState(org.apache.curator.framework.state.ConnectionState) RetryPolicy(org.apache.curator.RetryPolicy) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener)

Example 2 with RegistryConfig

use of com.alipay.sofa.ark.config.RegistryConfig in project sofa-ark by alipay.

the class ZookeeperConfigurator method buildConfig.

public static RegistryConfig buildConfig(String config) {
    String zkAddress = parseAddress(config);
    Map<String, String> parameters = parseParam(config);
    return new RegistryConfig().setAddress(zkAddress).setProtocol(Constants.CONFIG_PROTOCOL_ZOOKEEPER).setParameters(parameters);
}
Also used : RegistryConfig(com.alipay.sofa.ark.config.RegistryConfig)

Aggregations

RegistryConfig (com.alipay.sofa.ark.config.RegistryConfig)2 RetryPolicy (org.apache.curator.RetryPolicy)1 AuthInfo (org.apache.curator.framework.AuthInfo)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)1 ConnectionState (org.apache.curator.framework.state.ConnectionState)1 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)1 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)1