elasticJob 容器化部署节点实例化问题
elastic-job会将作业的配置信息、服务器信息等等数据写到zk,其中有个servers节点,其子节点存储作业服务器的IP地址
容器化部署的时候,每次重启之后容器ip地址变更,而目前elastic-job写到zk上的是容器地址,不断重启应用之后会导致 servers子节点不断增多,且子节点都是永久节点,不会随着容器的停掉而删除。需要考虑优化自动删除
ElasticJob的zk每次都会把serverIp持久化到server节点,容器的ip是变化的,重新部署次数多了以后会导致server节点上持久化ip很多,控制台虽然可以手动删除,但是不便捷,所有考虑到代码优化
可以在重写CoordinatorRegistryCenter的时候,其中close方法可以实现我们的需求
直接上代码
@Override public void close() {// servers节点上的ip实例化没有刷新问题 List<String> jobNameList = getChildrenKeys("/"); logger.info("refreshServerIp jobNameList = {}", jobNameList); if (CollectionUtils.isNotEmpty(jobNameList)) { Set<String> outIpList = new HashSet<>(); for (String jobName : jobNameList) { JobNodePath jobNodePath = new JobNodePath(jobName); List<String> serverList = getChildrenKeys(jobNodePath.getServerNodePath()); List<String> instanceList = getChildrenKeys(jobNodePath.getInstancesNodePath()); Set<String> instanceIpList = new HashSet<>(); instanceList.forEach(instance -> instanceIpList.add(instance.split("@-@")[0])); // 求差集 for (String item : serverList) { if (!instanceIpList.contains(item)) { outIpList.add(item); } } } logger.info("refreshServerIp outIpList = {}", outIpList); if (CollectionUtils.isNotEmpty(outIpList)) { for (String serverIp : outIpList) { // shutdown for (String jobName : jobNameList) { JobNodePath jobNodePath = new JobNodePath(jobName); List<String> instances = getChildrenKeys(jobNodePath.getInstancesNodePath()); for (String each : instances) { if (serverIp.equals(each.split("@-@")[0])) { remove(jobNodePath.getInstanceNodePath(each)); } } } // remove for (String jobName : jobNameList) { remove(new JobNodePath(jobName).getServerNodePath(serverIp)); } } } } for (Entry<String, TreeCache> each : caches.entrySet()) { each.getValue().close(); } waitForCacheClose(); CloseableUtils.closeQuietly(client); }
不积跬步,无以至千里;不积小流,无以成江海