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);
}
}
Aggregations