feat(cli): 副本数变化时重新生成部署主机列表
This commit is contained in:
@@ -2,6 +2,7 @@ package com.lanyuanxiaoyao.service.cli.runner;
|
||||
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.template.Template;
|
||||
@@ -65,6 +66,23 @@ public class RunnerApplication implements ApplicationRunner {
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
}
|
||||
|
||||
private List<String> selectHosts(ServiceInfo serviceInfo) {
|
||||
return serviceInfo.getReplicas() == 0
|
||||
? deployInformationProperties.getHosts()
|
||||
.stream()
|
||||
.map(HostInfo::getIp)
|
||||
.sorted(Comparator.naturalOrder())
|
||||
.collect(Collectors.toList())
|
||||
: ListUtil.sort(
|
||||
RandomUtil.randomEleList(
|
||||
deployInformationProperties.getHosts()
|
||||
.stream()
|
||||
.map(HostInfo::getIp)
|
||||
.collect(Collectors.toList()
|
||||
), serviceInfo.getReplicas()
|
||||
), Comparator.naturalOrder());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws IOException {
|
||||
Path planPath = Paths.get("deploy.plan");
|
||||
@@ -85,21 +103,12 @@ public class RunnerApplication implements ApplicationRunner {
|
||||
List<String> selectedHosts;
|
||||
if (deployPlans.containsKey(serviceInfo.getName())) {
|
||||
selectedHosts = deployPlans.get(serviceInfo.getName());
|
||||
if (ObjectUtil.notEqual(selectedHosts.size(), serviceInfo.getReplicas())) {
|
||||
selectedHosts = selectHosts(serviceInfo);
|
||||
deployPlans.put(serviceInfo.getName(), selectedHosts);
|
||||
}
|
||||
} else {
|
||||
selectedHosts = serviceInfo.getReplicas() == 0
|
||||
? deployInformationProperties.getHosts()
|
||||
.stream()
|
||||
.map(HostInfo::getIp)
|
||||
.sorted(Comparator.naturalOrder())
|
||||
.collect(Collectors.toList())
|
||||
: ListUtil.sort(
|
||||
RandomUtil.randomEleList(
|
||||
deployInformationProperties.getHosts()
|
||||
.stream()
|
||||
.map(HostInfo::getIp)
|
||||
.collect(Collectors.toList()
|
||||
), serviceInfo.getReplicas()
|
||||
), Comparator.naturalOrder());
|
||||
selectedHosts = selectHosts(serviceInfo);
|
||||
deployPlans.put(serviceInfo.getName(), selectedHosts);
|
||||
}
|
||||
String deployScript = deployTemplate.render(MapUtil.builder()
|
||||
|
||||
Reference in New Issue
Block a user