浏览代码

更新程序监控

DESKTOP-VAEGFGM\zqc 3 月之前
父节点
当前提交
9d57030c0f

+ 6 - 0
citu-module-infra/citu-module-infra-biz/pom.xml

@@ -156,6 +156,12 @@
             <groupId>org.apache.tika</groupId>
             <artifactId>tika-core</artifactId> <!-- 文件客户端:文件类型的识别 -->
         </dependency>
+        <dependency>
+            <groupId>com.citu</groupId>
+            <artifactId>menduner-common</artifactId>
+            <version>2.4.0-SNAPSHOT</version>
+            <scope>compile</scope>
+        </dependency>
 
     </dependencies>
     <build>

+ 31 - 0
citu-module-infra/citu-module-infra-biz/src/main/java/com/citu/module/infra/controller/admin/server/ServerController.java

@@ -0,0 +1,31 @@
+package com.citu.module.infra.controller.admin.server;
+
+import com.citu.framework.common.pojo.CommonResult;
+import com.citu.framework.common.pojo.PageResult;
+import com.citu.module.infra.controller.admin.server.vo.ServerMonitorInfoReqVo;
+import com.citu.module.infra.controller.admin.server.vo.ServerMonitorRespVO;
+import com.citu.module.infra.service.server.ServerService;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.*;
+
+@Tag(name = "管理后台 - 服务 监控")
+@RestController
+@RequestMapping("/infra/server")
+public class ServerController {
+
+    @Autowired
+    ServerService service;
+
+    @GetMapping("/page")
+    @Operation(summary = "获得API 访问日志分页")
+    @PreAuthorize("@ss.hasPermission('infra:server:page')")
+    @PostMapping("/list")
+    public CommonResult<PageResult<ServerMonitorRespVO>> getServerMonitorInfo(@RequestBody ServerMonitorInfoReqVo reqVo  ) {
+        return CommonResult.success(service.getServerMonitorInfo(reqVo));
+    }
+
+
+}

+ 66 - 0
citu-module-infra/citu-module-infra-biz/src/main/java/com/citu/module/infra/controller/admin/server/vo/ServerMonitorInfoReqVo.java

@@ -0,0 +1,66 @@
+package com.citu.module.infra.controller.admin.server.vo;
+
+import com.citu.framework.common.pojo.PageParam;
+import io.swagger.v3.oas.annotations.media.Schema;
+
+import javax.validation.constraints.Min;
+import javax.validation.constraints.NotNull;
+
+public class ServerMonitorInfoReqVo extends PageParam {
+
+    @Schema(description = "服务名称", requiredMode = Schema.RequiredMode.REQUIRED,example = "menduner-system")
+    private String serviceNameParam;
+
+    @Schema(description = "分组", requiredMode = Schema.RequiredMode.REQUIRED,example = "DEFUALT_GROUP")
+    private String groupNameParam;
+
+    @Schema(description = "命名空间", requiredMode = Schema.RequiredMode.REQUIRED,example = "dev")
+    private String namespaceId="dev";
+
+
+    private Boolean hasIpCount=false;
+
+
+    private Boolean withInstances=false;
+
+
+    public String getServiceNameParam() {
+        return serviceNameParam;
+    }
+
+    public void setServiceNameParam(String serviceNameParam) {
+        this.serviceNameParam = serviceNameParam;
+    }
+
+    public String getGroupNameParam() {
+        return groupNameParam;
+    }
+
+    public void setGroupNameParam(String groupNameParam) {
+        this.groupNameParam = groupNameParam;
+    }
+
+    public String getNamespaceId() {
+        return namespaceId;
+    }
+
+    public void setNamespaceId(String namespaceId) {
+        this.namespaceId = namespaceId;
+    }
+
+    public Boolean getHasIpCount() {
+        return hasIpCount;
+    }
+
+    public void setHasIpCount(Boolean hasIpCount) {
+        this.hasIpCount = hasIpCount;
+    }
+
+    public Boolean getWithInstances() {
+        return withInstances;
+    }
+
+    public void setWithInstances(Boolean withInstances) {
+        this.withInstances = withInstances;
+    }
+}

+ 73 - 0
citu-module-infra/citu-module-infra-biz/src/main/java/com/citu/module/infra/controller/admin/server/vo/ServerMonitorRespVO.java

@@ -0,0 +1,73 @@
+package com.citu.module.infra.controller.admin.server.vo;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+
+public class ServerMonitorRespVO {
+
+    @Schema(description = "服务名称", requiredMode = Schema.RequiredMode.REQUIRED)
+    private String name;
+
+    @Schema(description = "服务分组", requiredMode = Schema.RequiredMode.REQUIRED)
+    private String groupName;
+
+    @Schema(description = "服务集群数量", requiredMode = Schema.RequiredMode.REQUIRED)
+    private Integer clusterCount;
+
+    @Schema(description = "服务IP数量", requiredMode = Schema.RequiredMode.REQUIRED)
+    private Integer ipCount;
+
+    @Schema(description = "健康实例数量", requiredMode = Schema.RequiredMode.REQUIRED)
+    private Integer healthyInstanceCount;
+
+    @Schema(description = "触发告警标记", requiredMode = Schema.RequiredMode.REQUIRED)
+    private String triggerFlag;
+
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getGroupName() {
+        return groupName;
+    }
+
+    public void setGroupName(String groupName) {
+        this.groupName = groupName;
+    }
+
+    public Integer getClusterCount() {
+        return clusterCount;
+    }
+
+    public void setClusterCount(Integer clusterCount) {
+        this.clusterCount = clusterCount;
+    }
+
+    public Integer getIpCount() {
+        return ipCount;
+    }
+
+    public void setIpCount(Integer ipCount) {
+        this.ipCount = ipCount;
+    }
+
+    public Integer getHealthyInstanceCount() {
+        return healthyInstanceCount;
+    }
+
+    public void setHealthyInstanceCount(Integer healthyInstanceCount) {
+        this.healthyInstanceCount = healthyInstanceCount;
+    }
+
+    public String getTriggerFlag() {
+        return triggerFlag;
+    }
+
+    public void setTriggerFlag(String triggerFlag) {
+        this.triggerFlag = triggerFlag;
+    }
+}

+ 2 - 1
citu-module-infra/citu-module-infra-biz/src/main/java/com/citu/module/infra/framework/rpc/config/RpcConfiguration.java

@@ -3,8 +3,9 @@ package com.citu.module.infra.framework.rpc.config;
 import com.citu.module.system.api.user.AdminUserApi;
 import org.springframework.cloud.openfeign.EnableFeignClients;
 import org.springframework.context.annotation.Configuration;
+import com.citu.module.system.api.sms.SmsSendApi;
 
 @Configuration(proxyBeanMethods = false)
-@EnableFeignClients(clients = AdminUserApi.class)
+@EnableFeignClients(clients ={ AdminUserApi.class,SmsSendApi.class })
 public class RpcConfiguration {
 }

+ 9 - 0
citu-module-infra/citu-module-infra-biz/src/main/java/com/citu/module/infra/service/server/ServerService.java

@@ -0,0 +1,9 @@
+package com.citu.module.infra.service.server;
+
+import com.citu.framework.common.pojo.PageResult;
+import com.citu.module.infra.controller.admin.server.vo.ServerMonitorInfoReqVo;
+import com.citu.module.infra.controller.admin.server.vo.ServerMonitorRespVO;
+
+public interface ServerService {
+    PageResult<ServerMonitorRespVO> getServerMonitorInfo(ServerMonitorInfoReqVo reqVo  );
+}

+ 138 - 0
citu-module-infra/citu-module-infra-biz/src/main/java/com/citu/module/infra/service/server/ServerServiceImpl.java

@@ -0,0 +1,138 @@
+package com.citu.module.infra.service.server;
+
+import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.collection.ConcurrentHashSet;
+import cn.hutool.core.map.MapUtil;
+import cn.hutool.http.HttpUtil;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.citu.framework.common.pojo.PageResult;
+import com.citu.module.infra.controller.admin.server.vo.ServerMonitorInfoReqVo;
+import com.citu.module.infra.controller.admin.server.vo.ServerMonitorRespVO;
+import com.citu.module.system.api.sms.SmsSendApi;
+import com.citu.module.system.api.sms.dto.send.SmsSendSingleToUserReqDTO;
+import lombok.Setter;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.env.Environment;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+
+@Service
+public class ServerServiceImpl implements ServerService{
+
+    @Value("${citu.monitor.nacos.url:http://nacos.menduner.com/}")
+    private volatile String monitorNacosUrl="http://192.168.3.80:8848";
+
+
+    @Value("${citu.monitor.nacos.error.phone:18908679620}")
+    private volatile String monitorErrorPhone;
+
+    @Resource
+    SmsSendApi smsSendApi;
+
+    @Value("${:trade-server,infra-server,gateway-server,pay-server," +
+            "member-server,im-server,system-server,promotion-server,statistics-server,xxl-job-admin,menduner-reward-server,menduner-system-server,product-server}")
+    String defMonitorServerNames;
+
+    @Resource
+    private Environment environment;
+
+
+    @Override
+    public PageResult<ServerMonitorRespVO> getServerMonitorInfo(ServerMonitorInfoReqVo reqVo) {
+        String resp = HttpUtil.get(monitorNacosUrl+"/nacos/v1/ns/catalog/services", BeanUtil.beanToMap(reqVo));
+        JSONObject jsonObject = JSON.parseObject(resp);
+        Long count = jsonObject.getLong("count");
+        JSONArray serviceList = jsonObject.getJSONArray("serviceList");
+        List<ServerMonitorRespVO> serverMonitorRespVOList =  new ArrayList<>(serviceList.size()+1);
+        for (int i=0;i<serviceList.size();i++){
+
+            ServerMonitorRespVO service =    serviceList.getJSONObject(i).toJavaObject(ServerMonitorRespVO.class);
+            serverMonitorRespVOList.add(service);
+        }
+        return new PageResult(serverMonitorRespVOList,count);
+    }
+
+
+    private ConcurrentHashSet errorSet =new ConcurrentHashSet();
+
+
+
+
+    @Scheduled(cron = "0 0/5 * * * ? ")
+    public void checkServiceHealthy(){
+        ServerMonitorInfoReqVo reqVo = new ServerMonitorInfoReqVo();
+        reqVo.setPageSize(100);
+        List<ServerMonitorRespVO> list = getServerMonitorInfo(reqVo).getList();
+         String serverNames ="";
+        String monitorServerNames = environment.getProperty("citu.monitor.nacos.server", defMonitorServerNames);
+        HashSet<String> monitorServerSets = new HashSet<>(Arrays.asList(monitorServerNames.split(",")));
+        for (ServerMonitorRespVO serverMonitorRespVO : list) {
+            if (serverMonitorRespVO.getHealthyInstanceCount()==0 && monitorServerSets.contains(serverMonitorRespVO.getName())){
+                // 加入到异常中
+                errorSet.add(serverMonitorRespVO.getName());
+                serverNames+=serverMonitorRespVO.getName()+",";
+            }else {
+                monitorServerSets.remove(serverMonitorRespVO.getName());
+                // 恢复正常,关闭加入异常
+                errorSet.remove(serverMonitorRespVO.getName());
+            }
+        }
+        // 微服务列表不存在的服务
+        if(monitorServerSets.size()>0){
+            for (String serverName : monitorServerSets) {
+                if(!errorSet.contains(serverName)){
+                    serverNames=serverName+",";
+                }
+                errorSet.add(serverName);
+            }
+        }
+
+        if(StringUtils.isNotBlank(serverNames) ) {
+
+            serverNames=serverNames.substring(0,serverNames.length()-1);
+            SmsSendSingleToUserReqDTO smsReqDTO = new SmsSendSingleToUserReqDTO();
+            smsReqDTO.setMobile(monitorErrorPhone);
+            smsReqDTO.setUserId(1L);
+            smsReqDTO.setTemplateCode("menduner-sys-notify");
+            smsReqDTO.setTemplateParams(MapUtil.<String, Object>builder()
+                    .put("1", serverNames)
+                    .build());
+            // 发送短信 进行通知
+            smsSendApi.sendSingleSmsToAdmin(smsReqDTO);
+//            System.out.println("============== error servers ================");
+//            System.out.println(serverNames);
+//            System.out.println("============== error servers ================");
+
+        }
+    }
+
+
+
+
+
+    public String getMonitorNacosUrl() {
+        return monitorNacosUrl;
+    }
+
+    public String getMonitorErrorPhone() {
+        return monitorErrorPhone;
+    }
+
+    public void setMonitorErrorPhone(String monitorErrorPhone) {
+        this.monitorErrorPhone = monitorErrorPhone;
+    }
+
+
+
+
+
+}