Search in sources :

Example 1 with ZkClientSpec

use of com.shulie.instrument.module.register.zk.impl.ZkClientSpec in project LinkAgent by shulieTech.

the class ZookeeperRegister method init.

@Override
public void init(RegisterOptions registerOptions) {
    if (registerOptions == null) {
        throw new NullPointerException("RegisterOptions is null");
    }
    this.basePath = registerOptions.getRegisterBasePath();
    this.appName = registerOptions.getAppName();
    this.md5 = registerOptions.getMd5();
    this.simulatorConfig = registerOptions.getSimulatorConfig();
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("[pradar-register] prepare to init register zookeeper node. {}", Pradar.AGENT_ID_CONTAIN_USER_INFO);
    }
    String registerBasePath = null;
    if (StringUtils.endsWith(basePath, "/")) {
        registerBasePath = this.basePath + appName;
    } else {
        registerBasePath = this.basePath + '/' + appName;
    }
    try {
        ZkClientSpec zkClientSpec = new ZkClientSpec();
        zkClientSpec.setZkServers(registerOptions.getZkServers());
        zkClientSpec.setConnectionTimeoutMillis(registerOptions.getConnectionTimeoutMillis());
        zkClientSpec.setSessionTimeoutMillis(registerOptions.getSessionTimeoutMillis());
        zkClientSpec.setThreadName("heartbeat");
        this.zkClient = NetflixCuratorZkClientFactory.getInstance().create(zkClientSpec);
    } catch (PradarException e) {
        LOGGER.error("[pradar-register] ZookeeperRegister init error.", e);
        throw e;
    } catch (Throwable e) {
        LOGGER.error("[pradar-register] ZookeeperRegister init error.", e);
        throw new PradarException(e);
    }
    String client = Pradar.AGENT_ID_CONTAIN_USER_INFO;
    try {
        this.zkClient.ensureDirectoryExists(registerBasePath);
    } catch (Throwable e) {
        LOGGER.error("[register] ensureDirectoryExists err:{}", registerBasePath, e);
    }
    this.heartbeatPath = registerBasePath + '/' + client;
    try {
        if (this.zkClient.exists(this.heartbeatPath)) {
            this.zkClient.deleteQuietly(this.heartbeatPath);
        }
    } catch (Throwable e) {
    }
    cleanExpiredNodes(registerBasePath);
    this.heartbeatNode = this.zkClient.createHeartbeatNode(this.heartbeatPath);
    PradarSwitcher.registerListener(new PradarSwitcher.PradarSwitcherListener() {

        @Override
        public void onListen(Event event) {
            if (event instanceof PradarSwitchEvent || event instanceof ErrorEvent) {
                refresh();
            }
        }
    });
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("[pradar-register] init register zookeeper node successful. {}", Pradar.AGENT_ID_CONTAIN_USER_INFO);
    }
}
Also used : PradarException(com.pamirs.pradar.exception.PradarException) ZkClientSpec(com.shulie.instrument.module.register.zk.impl.ZkClientSpec) ErrorEvent(com.pamirs.pradar.event.ErrorEvent) ErrorEvent(com.pamirs.pradar.event.ErrorEvent) Event(com.pamirs.pradar.event.Event) PradarSwitchEvent(com.pamirs.pradar.event.PradarSwitchEvent) PradarSwitchEvent(com.pamirs.pradar.event.PradarSwitchEvent)

Aggregations

ErrorEvent (com.pamirs.pradar.event.ErrorEvent)1 Event (com.pamirs.pradar.event.Event)1 PradarSwitchEvent (com.pamirs.pradar.event.PradarSwitchEvent)1 PradarException (com.pamirs.pradar.exception.PradarException)1 ZkClientSpec (com.shulie.instrument.module.register.zk.impl.ZkClientSpec)1