|
@@ -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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|