feat(cloud): 增加prometheus动态获取实例端口

This commit is contained in:
2024-01-31 12:03:13 +08:00
parent eb6fbc7fc5
commit af27d1772d
4 changed files with 124 additions and 71 deletions

View File

@@ -1,8 +1,11 @@
package com.lanyuanxiaoyao.service.cloud;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.cloud.entity.PrometheusTarget;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.map.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.discovery.DiscoveryClient;
@@ -49,4 +52,15 @@ public class CloudController {
.toSortedList()
.toImmutable();
}
@GetMapping("targets")
public ImmutableList<PrometheusTarget> prometheusTargets() {
return Lists.immutable.ofAll(client.getServices())
.collect(name -> {
ImmutableList<String> targets = Lists.immutable.ofAll(client.getInstances(name))
.collect(instance -> instance.getUri().toString());
ImmutableMap<String, String> labels = Maps.immutable.of("application", name);
return new PrometheusTarget(targets, labels);
});
}
}

View File

@@ -0,0 +1,36 @@
package com.lanyuanxiaoyao.service.cloud.entity;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.map.ImmutableMap;
/**
* Prometheus服务发现
*
* @author lanyuanxiaoyao
* @date 2024-01-31
*/
public class PrometheusTarget {
private final ImmutableList<String> targets;
private final ImmutableMap<String, String> labels;
public PrometheusTarget(ImmutableList<String> targets, ImmutableMap<String, String> labels) {
this.targets = targets;
this.labels = labels;
}
public ImmutableList<String> getTargets() {
return targets;
}
public ImmutableMap<String, String> getLabels() {
return labels;
}
@Override
public String toString() {
return "PrometheusTarget{" +
"ips=" + targets +
", labels=" + labels +
'}';
}
}