@Override public void destroy() { close(); } }
public static void start() { start(getZkConnStr()); }
@Override public void init() { start(); }
@Override public Properties getAll(String pluginName) { Properties properties = new Properties(); if (!ZkConnection.isConnected()) { return properties; } for (ChildData childData : resCaches.getUnchecked(pluginName).getCurrentData()) { properties.put(childData.getPath().substring(childData.getPath().lastIndexOf("/") + 1), new String(childData.getData())); } return properties; }
@Override public void execute(UnitRequest msg, Handler<UnitResponse> handler) throws Exception { /** * 请设置此变量来删除指定路径下的脏节点 */ final String PATH = msg.get("basePath", "/xian_runtime_dev/unit"); try { ZkConnection.start(); for (String s : ZkConnection.client.getChildren().forPath(PATH)) { String fullPath = PATH.concat("/").concat(s); String data = new String(ZkConnection.client.getData().forPath(fullPath)); System.out.println(data); if (StringUtil.isEmpty(data)) { LOG.debug("实现原理是xian服务注册会在unit和group节点data上写入其定义数据,如果没有定义数据的,那么一定是脏节点"); ZkConnection.client.delete().forPath(fullPath); } } } finally { ZkConnection.close(); } handler.handle(UnitResponse.createSuccess()); }
@Override public String get(String pluginName, String key) { if (!ZkConnection.isConnected()) { return null; } ChildData childData = resCaches.getUnchecked(pluginName).getCurrentData(fullPath(pluginName + "/" + key)); if (childData == null) return null; return new String(childData.getData()); }
@Override public String getVersion(String pluginName) { if (!ZkConnection.isConnected()) { return null; } LOG.debug("注意:查询版本号操作是实时查询zk,没有做缓存,不允许高频操作:" + pluginName); try { byte[] versionData = ZkConnection.client.getData().forPath(fullPath(pluginName)); LOG.debug("if path exits but no data found, then empty byte array is returned. Thus here we need a empty array checking."); ResPluginDataBean resPluginDataBean = Reflection.toType(new String(versionData), ResPluginDataBean.class); if (resPluginDataBean != null) return resPluginDataBean.getVersion(); return null; } catch (KeeperException.NoNodeException notExists) { return null; } catch (Exception e) { throw new RuntimeException(e); } }