diff --git a/ruoyi-visual/ruoyi-monitor/src/main/java/com/ruoyi/modules/monitor/controller/PrometheusController.java b/ruoyi-visual/ruoyi-monitor/src/main/java/com/ruoyi/modules/monitor/controller/PrometheusController.java new file mode 100644 index 00000000..3d438efc --- /dev/null +++ b/ruoyi-visual/ruoyi-monitor/src/main/java/com/ruoyi/modules/monitor/controller/PrometheusController.java @@ -0,0 +1,67 @@ +package com.ruoyi.modules.monitor.controller; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * 对接 prometheus + * + * @author Lion Li + */ +@Slf4j +@RestController +@RequestMapping("/actuator/prometheus") +public class PrometheusController { + + @Autowired + private DiscoveryClient discoveryClient; + + /** + * 从注册中心获取所有服务组装成 prometheus 的数据结构 + */ + @GetMapping("/sd") + public List> sd() { + List services = discoveryClient.getServices(); + if (services == null || services.isEmpty()) { + return new ArrayList<>(0); + } + List> list = new ArrayList<>(); + for (String service : services) { + List instances = discoveryClient.getInstances(service); + List targets = instances.stream().map(i -> i.getHost() + ":" + i.getPort()).collect(Collectors.toList()); + + Map labels = new HashMap<>(2); + // 数据来源(区分异地使用) + // labels.put("__meta_datacenter", "beijing"); + // 服务名 + labels.put("__meta_prometheus_job", service); + Map group = new HashMap<>(2); + group.put("targets", targets); + group.put("labels", labels); + list.add(group); + } + return list; + } + + /** + * 接收 prometheus 报警消息 + * + * @param message 消息体 + */ + @PostMapping("/alerts") + public ResponseEntity alerts(@RequestBody String message) { + log.info("[prometheus] alert =>" + message); + return ResponseEntity.ok().build(); + } + +}