From 477c05ae31c32e335ca21cc84356f5e0cdf0a92a Mon Sep 17 00:00:00 2001 From: nm_duanzhixin <1015654585@qq.com> Date: Thu, 20 Feb 2025 17:46:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=90=8E=E7=AB=AF=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E4=BF=A1=E6=81=AF=E6=9F=A5=E7=9C=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../risesoft/service/StatisticsService.java | 2 + .../service/impl/StatisticsServiceImpl.java | 33 +++ .../y9public/dto/InterfaceManageDTO.java | 6 + .../risesoft/y9public/entity/InstanceNum.java | 30 +++ .../y9public/entity/InterfaceManage.java | 9 + .../repository/CallApiLogRepository.java | 3 + .../repository/InterfaceManageRepository.java | 3 + .../pom.xml | 4 + .../service/impl/UseInterfaceServiceImpl.java | 43 ++++ .../src/main/resources/application.yml | 11 +- .../rest/RestInterfaceManageController.java | 33 +++ .../rest/RestStatisticsController.java | 17 ++ .../service/InterfaceManageService.java | 4 + .../impl/InterfaceManageServiceImpl.java | 213 +++++++++++++++++- .../src/main/resources/application.yml | 2 + 15 files changed, 411 insertions(+), 2 deletions(-) create mode 100644 y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/entity/InstanceNum.java diff --git a/y9-module-interface/risenet-y9boot-support-interface-approve/src/main/java/net/risesoft/service/StatisticsService.java b/y9-module-interface/risenet-y9boot-support-interface-approve/src/main/java/net/risesoft/service/StatisticsService.java index e8b96c7..d280e54 100644 --- a/y9-module-interface/risenet-y9boot-support-interface-approve/src/main/java/net/risesoft/service/StatisticsService.java +++ b/y9-module-interface/risenet-y9boot-support-interface-approve/src/main/java/net/risesoft/service/StatisticsService.java @@ -24,4 +24,6 @@ public interface StatisticsService { Map getRunningCount(); Map getLogMonitoringOptions(); + + Map getInvokeNumToday(String id); } diff --git a/y9-module-interface/risenet-y9boot-support-interface-approve/src/main/java/net/risesoft/service/impl/StatisticsServiceImpl.java b/y9-module-interface/risenet-y9boot-support-interface-approve/src/main/java/net/risesoft/service/impl/StatisticsServiceImpl.java index 51d9b1f..051e6e9 100644 --- a/y9-module-interface/risenet-y9boot-support-interface-approve/src/main/java/net/risesoft/service/impl/StatisticsServiceImpl.java +++ b/y9-module-interface/risenet-y9boot-support-interface-approve/src/main/java/net/risesoft/service/impl/StatisticsServiceImpl.java @@ -24,6 +24,9 @@ import javax.persistence.criteria.Root; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.stream.Collectors; @@ -143,6 +146,36 @@ public class StatisticsServiceImpl implements StatisticsService { return res; } + @Override + public Map getInvokeNumToday(String id) { + Map map = new HashMap<>(); + List interfaceIds = interfaceManageRepository.getAllByExecuteInstanceId(id); + if(interfaceIds!=null && interfaceIds.size()!=0){ + // 获取当天的开始时间和结束时间 + LocalDateTime startTime = LocalDateTime.of(LocalDateTime.now().toLocalDate(), LocalTime.MIDNIGHT); + LocalDateTime endTime = LocalDateTime.of(LocalDateTime.now().toLocalDate(), LocalTime.MAX); + + // 指定时区,将LocalDateTime转换为ZonedDateTime + ZonedDateTime startZonedDateTime = startTime.atZone(ZoneId.systemDefault()); + ZonedDateTime endZonedDateTime = endTime.atZone(ZoneId.systemDefault()); + + // 将ZonedDateTime转换为Date + Date startDate = Date.from(startZonedDateTime.toInstant()); + Date endDate = Date.from(endZonedDateTime.toInstant()); + + List interfaceIdsStr = new ArrayList<>(); + for(Long interfaceId : interfaceIds){ + interfaceIdsStr.add(interfaceId.toString()); + } + Long count = callApiLogRepository.countByRequestStartTimeBetweenAndInterfaceIdIn(startDate,endDate,interfaceIdsStr); + map.put("data",count); + return map; + }else { + return null; + } + } + + @Override public Map getLogMonitoringInfo(Map conditionMap) { Map res = new HashMap<>(); diff --git a/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/dto/InterfaceManageDTO.java b/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/dto/InterfaceManageDTO.java index 32d6467..fd0c628 100644 --- a/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/dto/InterfaceManageDTO.java +++ b/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/dto/InterfaceManageDTO.java @@ -105,6 +105,10 @@ public class InterfaceManageDTO extends BaseEntity implements Serializable{ private Boolean isResponseFile; + private String executeInstanceId; + + private String executeInstanceIdBack; + public InterfaceManageDTO(InterfaceManage dto) { this.id = dto.getId(); this.interfaceName = dto.getInterfaceName(); @@ -138,6 +142,8 @@ public class InterfaceManageDTO extends BaseEntity implements Serializable{ this.systemId = dto.getSystemId(); this.systemName = dto.getSystemName(); this.isResponseFile = dto.getIsResponseFile(); + this.executeInstanceId = dto.getExecuteInstanceId(); + this.executeInstanceIdBack = dto.getExecuteInstanceIdBack(); } } diff --git a/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/entity/InstanceNum.java b/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/entity/InstanceNum.java new file mode 100644 index 0000000..6459160 --- /dev/null +++ b/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/entity/InstanceNum.java @@ -0,0 +1,30 @@ +package net.risesoft.y9public.entity; + +public class InstanceNum { + private Integer num; + private String instanceId; + + public InstanceNum() { + } + + public InstanceNum(Long num, String instanceId) { + this.num = num.intValue(); + this.instanceId = instanceId; + } + + public Integer getNum() { + return num; + } + + public void setNum(Integer num) { + this.num = num; + } + + public String getInstanceId() { + return instanceId; + } + + public void setInstanceId(String instanceId) { + this.instanceId = instanceId; + } +} diff --git a/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/entity/InterfaceManage.java b/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/entity/InterfaceManage.java index d8b15aa..d4bdbe4 100644 --- a/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/entity/InterfaceManage.java +++ b/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/entity/InterfaceManage.java @@ -156,6 +156,13 @@ public class InterfaceManage extends BaseEntity implements Serializable{ @Comment(value = "是否返回文件") private Boolean isResponseFile; + @Column(name = "EXECUTE_INSTANCE_ID", columnDefinition = "varchar(500) default '' comment '执行端实例ID'") + @Comment(value = "执行实例ID") + private String executeInstanceId; + + @Column(name = "EXECUTE_INSTANCE_ID_BACK", columnDefinition = "varchar(500) default '' comment '备选执行端实例ID'") + @Comment(value = "备用实例ID") + private String executeInstanceIdBack; public InterfaceManage(InterfaceManageDTO dto) { this.id = dto.getId(); @@ -190,6 +197,8 @@ public class InterfaceManage extends BaseEntity implements Serializable{ this.systemId = dto.getSystemId(); this.systemName = dto.getSystemName(); this.isResponseFile = dto.getIsResponseFile(); + this.executeInstanceId = dto.getExecuteInstanceId(); + this.executeInstanceIdBack = dto.getExecuteInstanceIdBack(); } } diff --git a/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/repository/CallApiLogRepository.java b/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/repository/CallApiLogRepository.java index 186e907..129db9b 100644 --- a/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/repository/CallApiLogRepository.java +++ b/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/repository/CallApiLogRepository.java @@ -6,6 +6,7 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; +import java.util.Date; import java.util.List; import java.util.Map; @@ -20,4 +21,6 @@ public interface CallApiLogRepository extends JpaRepository> getDataGroupByDeptName(); + + Long countByRequestStartTimeBetweenAndInterfaceIdIn(Date start, Date end, List interfaceIds); } diff --git a/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/repository/InterfaceManageRepository.java b/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/repository/InterfaceManageRepository.java index 5c3cf82..4c31c7a 100644 --- a/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/repository/InterfaceManageRepository.java +++ b/y9-module-interface/risenet-y9boot-support-interface-jpa-repository/src/main/java/net/risesoft/y9public/repository/InterfaceManageRepository.java @@ -34,4 +34,7 @@ public interface InterfaceManageRepository extends JpaRepository getAllByExecuteInstanceId(String id); } diff --git a/y9-module-interface/risenet-y9boot-webapp-interface-platform-execute/pom.xml b/y9-module-interface/risenet-y9boot-webapp-interface-platform-execute/pom.xml index f286330..54b8bdc 100644 --- a/y9-module-interface/risenet-y9boot-webapp-interface-platform-execute/pom.xml +++ b/y9-module-interface/risenet-y9boot-webapp-interface-platform-execute/pom.xml @@ -161,6 +161,10 @@ org.springframework.cloud spring-cloud-starter-netflix-eureka-client + + org.springframework.boot + spring-boot-starter-actuator + net.risesoft diff --git a/y9-module-interface/risenet-y9boot-webapp-interface-platform-execute/src/main/java/net/risesoft/service/impl/UseInterfaceServiceImpl.java b/y9-module-interface/risenet-y9boot-webapp-interface-platform-execute/src/main/java/net/risesoft/service/impl/UseInterfaceServiceImpl.java index 6dbdd8a..77cb6e4 100644 --- a/y9-module-interface/risenet-y9boot-webapp-interface-platform-execute/src/main/java/net/risesoft/service/impl/UseInterfaceServiceImpl.java +++ b/y9-module-interface/risenet-y9boot-webapp-interface-platform-execute/src/main/java/net/risesoft/service/impl/UseInterfaceServiceImpl.java @@ -1,6 +1,8 @@ package net.risesoft.service.impl; import com.alibaba.fastjson.JSONArray; +import com.netflix.appinfo.InstanceInfo; +import com.netflix.discovery.EurekaClient; import net.risesoft.model.user.UserInfo; import net.risesoft.service.UseInterfaceService; import net.risesoft.util.*; @@ -15,6 +17,8 @@ import net.risesoft.y9public.repository.*; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.Resource; import org.springframework.http.*; @@ -58,6 +62,11 @@ public class UseInterfaceServiceImpl implements UseInterfaceService { private CallLogUtil calllogUtil; @Autowired private BlacklistingRepository blacklistingRepository; + @Qualifier("eurekaClient") + @Autowired + private EurekaClient eurekaClient; + @Value("${eureka.server-application}") + private String serviceName; public static final String regx = "@-@"; @@ -75,7 +84,34 @@ public class UseInterfaceServiceImpl implements UseInterfaceService { Map body = new HashMap<>(); try { + InstanceInfo instanceInfo = null; try { + instanceInfo = eurekaClient.getNextServerFromEureka(serviceName, false); + }catch (Exception e){ + map.put("msg", "请求失败!未查询到实例"); + + logMap.put("status", "未查询到实例"); + logMap.put("errMsg", "请求失败!未查询到实例"); + logMap.put("code", "401"); + return ResponseEntity.ok().body(map); + } + try { + if (instanceInfo == null || StringUtils.isBlank(instanceInfo.getInstanceId())) { + map.put("msg", "请求失败!未注册的实例"); + + logMap.put("status", "未注册的实例"); + logMap.put("errMsg", "请求失败!未注册的实例"); + logMap.put("code", "401"); + return ResponseEntity.ok().body(map); + } + if(!InstanceInfo.InstanceStatus.UP.equals(instanceInfo.getStatus())){ + map.put("msg", "请求失败!当前实例未在线"); + + logMap.put("status", "实例未在线"); + logMap.put("errMsg", "请求失败!"+instanceInfo.getInstanceId()+"实例未在线"); + logMap.put("code", "401"); + return ResponseEntity.ok().body(map); + } interfaceApply = interfaceApplyRepository.findDataByUserKey(id); if (StringUtils.isBlank(interfaceApply.getIsEffective()) || "N".equals(interfaceApply.getIsEffective())) { map.put("msg", "请求失败!申请信息已经过期请重新申请"); @@ -124,6 +160,13 @@ public class UseInterfaceServiceImpl implements UseInterfaceService { logMap.put("code", "501"); return ResponseEntity.ok().body(map); } + if(!(instanceInfo.getInstanceId().equals(interfaceManage.getExecuteInstanceId()) || instanceInfo.getInstanceId().equals(interfaceManage.getExecuteInstanceIdBack()))){ + map.put("msg", "请求失败!该接口未注册到当前实例"); + logMap.put("status", "请求失败"); + logMap.put("errMsg", "请求失败!该接口未注册到当前实例"); + logMap.put("code", "401"); + return ResponseEntity.ok().body(map); + } InterfaceManageDTO interfaceManageDTO = new InterfaceManageDTO(interfaceManage); //判断开启限流 if ("是".equals(interfaceManageDTO.getIsLimit())) { diff --git a/y9-module-interface/risenet-y9boot-webapp-interface-platform-execute/src/main/resources/application.yml b/y9-module-interface/risenet-y9boot-webapp-interface-platform-execute/src/main/resources/application.yml index 3c8cd35..cf44ba0 100644 --- a/y9-module-interface/risenet-y9boot-webapp-interface-platform-execute/src/main/resources/application.yml +++ b/y9-module-interface/risenet-y9boot-webapp-interface-platform-execute/src/main/resources/application.yml @@ -16,12 +16,21 @@ server: tomcat: uri-encoding: UTF-8 eureka: + server-application: "interfaceplatform" #eureka服务端服务名 client: enabled: true # 启用Eureka客户端 service-url: defaultZone: http://admin:123@localhost:7055/interfaceManager/eureka/ # Eureka服务器地址 fetch-registry: true # 是否从Eureka服务器获取注册信息 register-with-eureka: true # 是否注册自身到Eureka服务 +management: + endpoints: + web: + exposure: + include: "*" # 暴露所有端点,或者你可以指定具体要暴露的端点名称,用逗号分隔 + endpoint: + health: + show-details: always spring: application: name: interfaceExecute @@ -183,7 +192,7 @@ y9: allowBasicAuthentication: true allowFormEncodedBodyParameter: true allowUriQueryParameter: true - protectedUrlPatterns: /api/* + protectedUrlPatterns: /api/*,/actuator/* opaque: client-id: clientid client-secret: secret diff --git a/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/java/net/risesoft/controller/rest/RestInterfaceManageController.java b/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/java/net/risesoft/controller/rest/RestInterfaceManageController.java index ea7e176..9d5453d 100644 --- a/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/java/net/risesoft/controller/rest/RestInterfaceManageController.java +++ b/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/java/net/risesoft/controller/rest/RestInterfaceManageController.java @@ -269,6 +269,39 @@ public class RestInterfaceManageController { } + //获取注册接口数 + @RequestMapping("/getRegisterNum") + @ResponseBody + @RiseLog(operationType = OperationTypeEnum.BROWSE, operationName = "获取执行端注册接口数量") + public Map getRegisterNum(String id) { + Map map = new HashMap<>(); + List list = interfaceManageService.getRegisterNum(id); + if (list != null) { + map.put("status", "success"); + } else { + map.put("status", "error"); + } + map.put("code", "0"); + map.put("data", list); + return map; + } + + //根据接口id获取实例ip端口 + @RequestMapping("/getIpPortByInterfaceId") + @ResponseBody + @RiseLog(operationType = OperationTypeEnum.BROWSE, operationName = "根据接口id获取实例ip端口") + public Map getIpPortByInterfaceId(String id) { + Map map = new HashMap<>(); + Map data = interfaceManageService.getIpPortByInterfaceId(id); + if (data != null) { + map.put("status", "success"); + } else { + map.put("status", "error"); + } + map.put("code", "0"); + map.put("data", data); + return map; + } } diff --git a/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/java/net/risesoft/controller/rest/RestStatisticsController.java b/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/java/net/risesoft/controller/rest/RestStatisticsController.java index 4c89640..464458d 100644 --- a/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/java/net/risesoft/controller/rest/RestStatisticsController.java +++ b/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/java/net/risesoft/controller/rest/RestStatisticsController.java @@ -109,4 +109,21 @@ public class RestStatisticsController { public Map getRealTimeLog(int page, int limit){ return statisticsService.getRealTimeLog(page, limit); } + + //获取今日调用次数 + @RequestMapping("/getInvokeNumToday") + @ResponseBody + @RiseLog(operationType = OperationTypeEnum.BROWSE, operationName = "获取执行端今日调用接口数量") + public Map getInvokeNumToday(String id) { + Map map = new HashMap<>(); + Map data = statisticsService.getInvokeNumToday(id); + if (data != null) { + map.put("status", "success"); + } else { + map.put("status", "error"); + } + map.put("code", "0"); + map.put("data", data!=null?data.get("data"):0); + return map; + } } diff --git a/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/java/net/risesoft/service/InterfaceManageService.java b/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/java/net/risesoft/service/InterfaceManageService.java index bba3909..5873ce1 100644 --- a/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/java/net/risesoft/service/InterfaceManageService.java +++ b/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/java/net/risesoft/service/InterfaceManageService.java @@ -64,4 +64,8 @@ public interface InterfaceManageService { //下载接口文档 void downLoadInterfaceFile(String sameId,String version,String fileName, HttpServletResponse response); + + List getRegisterNum(String instanceId); + + Map getIpPortByInterfaceId(String instanceId); } diff --git a/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/java/net/risesoft/service/impl/InterfaceManageServiceImpl.java b/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/java/net/risesoft/service/impl/InterfaceManageServiceImpl.java index 772455f..52a22af 100644 --- a/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/java/net/risesoft/service/impl/InterfaceManageServiceImpl.java +++ b/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/java/net/risesoft/service/impl/InterfaceManageServiceImpl.java @@ -1,6 +1,7 @@ package net.risesoft.service.impl; import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import net.risesoft.enums.platform.ManagerLevelEnum; @@ -24,10 +25,16 @@ import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.*; import org.springframework.data.jpa.domain.Specification; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.transaction.interceptor.TransactionAspectSupport; +import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; import javax.persistence.criteria.CriteriaBuilder; @@ -41,6 +48,7 @@ import java.io.*; import java.lang.reflect.Field; import java.net.URL; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @@ -65,6 +73,13 @@ public class InterfaceManageServiceImpl implements InterfaceManageService { @Autowired private AuthDictRepository authDictRepository; + @Value("${eureka.client.service-url.defaultZone}") + private String eurekaUrl; + @Value("${spring.eureka.token}") + private String token; + + private final RestTemplate restTemplate = new RestTemplate(); + @Override public Page getInterfaceList(InterfaceManageDTO interfaceManageDTO) { Specification spec = new Specification() { @@ -186,9 +201,60 @@ public class InterfaceManageServiceImpl implements InterfaceManageService { interfaceManage.setPersonName(person.getName()); } + String base64Token = Base64.getEncoder().encodeToString(token.getBytes(StandardCharsets.UTF_8)); + HttpHeaders headers = new HttpHeaders(); + headers.add("Authorization", "Basic "+base64Token); + HttpEntity entity = new HttpEntity<>(null, headers); + ResponseEntity response = restTemplate.exchange(eurekaUrl+"apps", HttpMethod.GET, entity, String.class); + String jsonData = response.getBody(); + HashMap apps = JSONArray.parseObject(jsonData,HashMap.class); + List instanceIds = getInstanceIds(apps); + if(instanceIds.size()==0){ + map.put("status", false); + map.put("errMsg","保存失败,实例已经下线"); + return map; + }else { + Boolean isUp = false; + for(String instanceId : instanceIds){ + if(instanceId.equals(interfaceManage.getExecuteInstanceId())){ + isUp = true; + break; + } + } + if(!isUp){ + map.put("status", false); + map.put("errMsg","实例已经下线"); + return map; + } + List instances = interfaceManageRepository.getAllCountGroupByInstanceID(); + int min = 100000; + String instanceId = ""; + HashMap instanceMap = new HashMap<>(); + for(InstanceNum instance : instances){ + instanceMap.put(instance.getInstanceId(),instance.getNum()); + } + for(String id : instanceIds){ + if(instanceMap.get(id)!=null){ + try { + if(instanceMap.get(id) getRegisterNum(String instanceId) { + List list = new ArrayList<>(); + if(StringUtils.isNotBlank(instanceId)){ + List instances = interfaceManageRepository.getAllCountGroupByInstanceID(); + InstanceNum instanceNum = null; + for(InstanceNum instance : instances){ + if(instanceId.equals(instance.getInstanceId())){ + instanceNum = instance; + } + } + if(instanceNum == null){ + instanceNum = new InstanceNum(); + instanceNum.setNum(0); + instanceNum.setInstanceId(instanceId); + } + list.add(instanceNum); + }else { + String base64Token = Base64.getEncoder().encodeToString(token.getBytes(StandardCharsets.UTF_8)); + HttpHeaders headers = new HttpHeaders(); + headers.add("Authorization", "Basic "+base64Token); + HttpEntity entity = new HttpEntity<>(null, headers); + ResponseEntity response = restTemplate.exchange(eurekaUrl+"apps", HttpMethod.GET, entity, String.class); + String jsonData = response.getBody(); + HashMap apps = JSONArray.parseObject(jsonData,HashMap.class); + List instanceIds = getInstanceIds(apps); + HashMap map = new HashMap<>(); + List instances = interfaceManageRepository.getAllCountGroupByInstanceID(); + for(InstanceNum instance : instances){ + map.put(instance.getInstanceId(),instance.getNum()); + } + for(String instance : instanceIds){ + InstanceNum instanceNum = new InstanceNum(); + if(map.get(instance)!=null){ + instanceNum.setNum(map.get(instance)); + instanceNum.setInstanceId(instance); + }else { + instanceNum.setInstanceId(instance); + instanceNum.setNum(0); + } + list.add(instanceNum); + } + } + return list; + } + + @Override + public Map getIpPortByInterfaceId(String instanceId) { + Map ipPortMap = null; + InterfaceManage interfaceManage = interfaceManageRepository.findById(instanceId).orElse(null); + if(interfaceManage!=null){ + String base64Token = Base64.getEncoder().encodeToString(token.getBytes(StandardCharsets.UTF_8)); + HttpHeaders headers = new HttpHeaders(); + headers.add("Authorization", "Basic "+base64Token); + HttpEntity entity = new HttpEntity<>(null, headers); + ResponseEntity response = restTemplate.exchange(eurekaUrl+"apps", HttpMethod.GET, entity, String.class); + String jsonData = response.getBody(); + HashMap apps = JSONArray.parseObject(jsonData,HashMap.class); + List> list = getInstances(apps); + for(Map map : list){ + if(StringUtils.isNotBlank(interfaceManage.getExecuteInstanceId())){ + if(interfaceManage.getExecuteInstanceId().equals(map.get("instanceId"))){ + ipPortMap = new HashMap<>(); + ipPortMap.put("ip",map.get("ip")); + ipPortMap.put("port",map.get("port")); + ipPortMap.put("hostName",map.get("hostName")); + return ipPortMap; + } + } + } + if(ipPortMap==null){ + for(Map map : list){ + if(StringUtils.isNotBlank(interfaceManage.getExecuteInstanceIdBack())){ + if(interfaceManage.getExecuteInstanceIdBack().equals(map.get("instanceId"))){ + ipPortMap = new HashMap<>(); + ipPortMap.put("ip",map.get("ip")); + ipPortMap.put("port",map.get("port")); + ipPortMap.put("hostName",map.get("hostName")); + return ipPortMap; + } + } + } + } + } + return null; + } + + private InterfaceManageDTO readJson(InputStream inputStream) throws Exception { ObjectMapper objectMapper = new ObjectMapper(); InterfaceManageDTO jsonStr = objectMapper.readValue(inputStream, InterfaceManageDTO.class); @@ -1089,4 +1243,61 @@ public class InterfaceManageServiceImpl implements InterfaceManageService { } return true; } + //解析实例信息,获取全部实例ID + private List getInstanceIds(Map data){ + List instanceIds = new ArrayList<>(); + if(data.get("applications") instanceof Map){ + HashMap applications = ((JSONObject)data.get("applications")).toJavaObject(HashMap.class); + if(applications.get("application") instanceof List){ + for(Object obj : (JSONArray)applications.get("application")){ + if(obj instanceof Map){ + HashMap appMap = ((JSONObject) obj).toJavaObject(HashMap.class); + if(appMap.get("instance") instanceof List){ + for(Object o : (JSONArray)appMap.get("instance")){ + if(o instanceof Map){ + HashMap map = ((JSONObject) o).toJavaObject(HashMap.class); + if(map.get("status").toString().toUpperCase().equals("UP")){ + instanceIds.add(String.valueOf(map.get("instanceId"))); + } + } + } + } + } + } + } + } + return instanceIds; + } + + //解析实例信息,获取全部实例ID + private List> getInstances(Map data){ + List> instances = new ArrayList<>(); + if(data.get("applications") instanceof Map){ + HashMap applications = ((JSONObject)data.get("applications")).toJavaObject(HashMap.class); + if(applications.get("application") instanceof List){ + for(Object obj : (JSONArray)applications.get("application")){ + if(obj instanceof Map){ + HashMap appMap = ((JSONObject) obj).toJavaObject(HashMap.class); + if(appMap.get("instance") instanceof List){ + for(Object o : (JSONArray)appMap.get("instance")){ + if(o instanceof Map){ + HashMap map = ((JSONObject) o).toJavaObject(HashMap.class); + if(map.get("status").toString().toUpperCase().equals("UP")){ + HashMap app = new HashMap<>(); + app.put("instanceId",String.valueOf(map.get("instanceId"))); + app.put("ip",String.valueOf(map.get("ipAddr"))); + app.put("hostName",String.valueOf(map.get("hostName"))); + HashMap portMap = ((JSONObject) map.get("port")).toJavaObject(HashMap.class); + app.put("port",String.valueOf(portMap.get("$"))); + instances.add(app); + } + } + } + } + } + } + } + } + return instances; + } } diff --git a/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/resources/application.yml b/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/resources/application.yml index 45e4488..8387d48 100644 --- a/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/resources/application.yml +++ b/y9-module-interface/risenet-y9boot-webapp-interface-platform-manager/src/main/resources/application.yml @@ -24,6 +24,8 @@ eureka: enabled: false # 启用服务面板 path: / # 服务面板路径 client: + service-url: + defaultZone: http://localhost:8080/interfaceManager/eureka/ # Eureka服务器地址本机地址 fetch-registry: false # 是否从Eureka服务器获取注册信息 register-with-eureka: false # 是否注册自身到Eureka服务器 spring: