From 7fbbb2a0dbe981f8769770949f6f6b7a77462e6e Mon Sep 17 00:00:00 2001 From: HaojunRen <1394997@qq.com> Date: Tue, 24 Sep 2024 14:14:59 +0800 Subject: [PATCH] Add two getMetadataMap interfaces on console and admincenter modules --- .../common/entity/MetadataParameter.java | 67 +++++++++++++++ .../console/endpoint/ServiceEndpoint.java | 35 ++++++++ .../console/resource/ServiceResource.java | 5 ++ .../console/resource/ServiceResourceImpl.java | 85 ++++++++++++++++++- .../admincenter/endpoint/ServiceEndpoint.java | 35 ++++++++ .../admincenter/resource/ServiceResource.java | 5 ++ .../resource/ServiceResourceImpl.java | 85 ++++++++++++++++++- .../admincenter/endpoint/ServiceEndpoint.java | 33 +++++++ .../admincenter/resource/ServiceResource.java | 5 ++ .../resource/ServiceResourceImpl.java | 85 ++++++++++++++++++- 10 files changed, 428 insertions(+), 12 deletions(-) create mode 100644 discovery-commons/discovery-common/src/main/java/com/nepxion/discovery/common/entity/MetadataParameter.java diff --git a/discovery-commons/discovery-common/src/main/java/com/nepxion/discovery/common/entity/MetadataParameter.java b/discovery-commons/discovery-common/src/main/java/com/nepxion/discovery/common/entity/MetadataParameter.java new file mode 100644 index 0000000000..d158e47f05 --- /dev/null +++ b/discovery-commons/discovery-common/src/main/java/com/nepxion/discovery/common/entity/MetadataParameter.java @@ -0,0 +1,67 @@ +package com.nepxion.discovery.common.entity; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import java.io.Serializable; +import java.util.List; + +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + +public class MetadataParameter implements Serializable { + private static final long serialVersionUID = -8708814947706738426L; + + public static final String SEPARATE = " | "; + + private List metadataKeys; + private List serviceIds; + private String separate = SEPARATE; + + public List getMetadataKeys() { + return metadataKeys; + } + + public void setMetadataKeys(List metadataKeys) { + this.metadataKeys = metadataKeys; + } + + public List getServiceIds() { + return serviceIds; + } + + public void setServiceIds(List serviceIds) { + this.serviceIds = serviceIds; + } + + public String getSeparate() { + return separate; + } + + public void setSeparate(String separate) { + this.separate = separate; + } + + @Override + public int hashCode() { + return HashCodeBuilder.reflectionHashCode(this); + } + + @Override + public boolean equals(Object object) { + return EqualsBuilder.reflectionEquals(this, object); + } + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); + } +} \ No newline at end of file diff --git a/discovery-console/discovery-console-starter/src/main/java/com/nepxion/discovery/console/endpoint/ServiceEndpoint.java b/discovery-console/discovery-console-starter/src/main/java/com/nepxion/discovery/console/endpoint/ServiceEndpoint.java index a188e70da4..141bcd5708 100644 --- a/discovery-console/discovery-console-starter/src/main/java/com/nepxion/discovery/console/endpoint/ServiceEndpoint.java +++ b/discovery-console/discovery-console-starter/src/main/java/com/nepxion/discovery/console/endpoint/ServiceEndpoint.java @@ -29,6 +29,7 @@ import com.nepxion.discovery.common.entity.GatewayType; import com.nepxion.discovery.common.entity.InstanceEntity; +import com.nepxion.discovery.common.entity.MetadataParameter; import com.nepxion.discovery.common.entity.ServiceType; import com.nepxion.discovery.common.util.ResponseUtil; import com.nepxion.discovery.console.resource.ServiceResource; @@ -117,6 +118,20 @@ public ResponseEntity instanceMap(@RequestBody @ApiParam(value = "服务组 return doInstanceMap(groups); } + @RequestMapping(path = "/metadata-map/{metadataKey}", method = RequestMethod.POST) + @ApiOperation(value = "获取注册中心的服务实例元数据值列表的Map(值包含单个元数据)", notes = "", response = ResponseEntity.class, httpMethod = "POST") + @ResponseBody + public ResponseEntity metadataMap(@PathVariable(value = "metadataKey") @ApiParam(value = "元数据键名", required = true) String metadataKey, @RequestBody @ApiParam(value = "服务名列表", required = true) List serviceIds) { + return doMetadataMap(metadataKey, serviceIds); + } + + @RequestMapping(path = "/metadata-map", method = RequestMethod.POST) + @ApiOperation(value = "获取注册中心的服务实例元数据值列表的Map(值包含多个元数据,通过分隔符分隔)", notes = "", response = ResponseEntity.class, httpMethod = "POST") + @ResponseBody + public ResponseEntity metadataMap(@RequestBody @ApiParam(value = "元数据查询参数对象", required = true) MetadataParameter metadataParameter) { + return doMetadataMap(metadataParameter); + } + private ResponseEntity doDiscoveryType() { try { String discoveryType = serviceResource.getDiscoveryType().toString(); @@ -241,4 +256,24 @@ private ResponseEntity doInstanceMap(List groups) { return ResponseUtil.getFailureResponse(e); } } + + private ResponseEntity doMetadataMap(String metadataKey, List serviceIds) { + try { + Map> metadataMap = serviceResource.getMetadataMap(metadataKey, serviceIds); + + return ResponseUtil.getSuccessResponse(metadataMap); + } catch (Exception e) { + return ResponseUtil.getFailureResponse(e); + } + } + + private ResponseEntity doMetadataMap(MetadataParameter metadataParameter) { + try { + Map> metadataMap = serviceResource.getMetadataMap(metadataParameter); + + return ResponseUtil.getSuccessResponse(metadataMap); + } catch (Exception e) { + return ResponseUtil.getFailureResponse(e); + } + } } \ No newline at end of file diff --git a/discovery-console/discovery-console-starter/src/main/java/com/nepxion/discovery/console/resource/ServiceResource.java b/discovery-console/discovery-console-starter/src/main/java/com/nepxion/discovery/console/resource/ServiceResource.java index 589a8c3da9..c6e63a2e61 100644 --- a/discovery-console/discovery-console-starter/src/main/java/com/nepxion/discovery/console/resource/ServiceResource.java +++ b/discovery-console/discovery-console-starter/src/main/java/com/nepxion/discovery/console/resource/ServiceResource.java @@ -17,6 +17,7 @@ import com.nepxion.discovery.common.entity.DiscoveryType; import com.nepxion.discovery.common.entity.GatewayType; import com.nepxion.discovery.common.entity.InstanceEntity; +import com.nepxion.discovery.common.entity.MetadataParameter; import com.nepxion.discovery.common.entity.ServiceType; public interface ServiceResource { @@ -41,4 +42,8 @@ public interface ServiceResource { List getInstanceList(String serviceId); Map> getInstanceMap(List groups); + + Map> getMetadataMap(String metadataKey, List serviceIds); + + Map> getMetadataMap(MetadataParameter metadataParameter); } \ No newline at end of file diff --git a/discovery-console/discovery-console-starter/src/main/java/com/nepxion/discovery/console/resource/ServiceResourceImpl.java b/discovery-console/discovery-console-starter/src/main/java/com/nepxion/discovery/console/resource/ServiceResourceImpl.java index b6bb7fc8e4..99b941592b 100644 --- a/discovery-console/discovery-console-starter/src/main/java/com/nepxion/discovery/console/resource/ServiceResourceImpl.java +++ b/discovery-console/discovery-console-starter/src/main/java/com/nepxion/discovery/console/resource/ServiceResourceImpl.java @@ -22,11 +22,13 @@ import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient; +import com.nepxion.discovery.common.constant.DiscoveryConstant; import com.nepxion.discovery.common.delegate.DiscoveryClientDelegate; import com.nepxion.discovery.common.entity.DiscoveryType; import com.nepxion.discovery.common.entity.GatewayType; import com.nepxion.discovery.common.entity.InstanceEntity; import com.nepxion.discovery.common.entity.InstanceEntityWrapper; +import com.nepxion.discovery.common.entity.MetadataParameter; import com.nepxion.discovery.common.entity.ServiceType; public class ServiceResourceImpl implements ServiceResource { @@ -78,9 +80,8 @@ public List getGroups() { for (String service : services) { List instanceEntityList = getInstanceList(service); for (InstanceEntity instance : instanceEntityList) { - String plugin = instance.getPlugin(); String group = instance.getGroup(); - if (StringUtils.isNotEmpty(plugin) && !groupList.contains(group)) { + if (!groupList.contains(group)) { groupList.add(group); } } @@ -275,9 +276,8 @@ public Map> getInstanceMap(List groups) { List instanceEntityList = getInstanceList(service); if (CollectionUtils.isNotEmpty(groups)) { for (InstanceEntity instance : instanceEntityList) { - String plugin = instance.getPlugin(); String group = instance.getGroup(); - if (StringUtils.isNotEmpty(plugin) && groups.contains(group)) { + if (groups.contains(group)) { List instanceList = instanceMap.get(service); if (instanceList == null) { instanceList = new ArrayList(); @@ -293,4 +293,81 @@ public Map> getInstanceMap(List groups) { return instanceMap; } + + @Override + public Map> getMetadataMap(String metadataKey, List serviceIds) { + Map> metadataMap = new LinkedHashMap>(serviceIds.size()); + for (String serviceId : serviceIds) { + List instanceEntityList = getInstanceList(serviceId); + for (InstanceEntity instance : instanceEntityList) { + List metadataList = metadataMap.get(serviceId); + if (metadataList == null) { + metadataList = new ArrayList(); + metadataMap.put(serviceId, metadataList); + } + String metadataValue = null; + if (StringUtils.equals(metadataKey, DiscoveryConstant.HOST + ":" + DiscoveryConstant.PORT)) { + metadataValue = instance.getHost() + ":" + instance.getPort(); + } else if (StringUtils.equals(metadataKey, DiscoveryConstant.HOST)) { + metadataValue = instance.getHost(); + } else if (StringUtils.equals(metadataKey, DiscoveryConstant.PORT)) { + metadataValue = String.valueOf(instance.getPort()); + } else { + metadataValue = instance.getMetadata().get(metadataKey); + } + if (!metadataList.contains(metadataValue)) { + if (StringUtils.isNotEmpty(metadataValue)) { + metadataList.add(metadataValue); + } + } + } + } + + return metadataMap; + } + + @Override + public Map> getMetadataMap(MetadataParameter metadataParameter) { + List metadataKeys = metadataParameter.getMetadataKeys(); + List serviceIds = metadataParameter.getServiceIds(); + String separate = metadataParameter.getSeparate(); + Map> metadataMap = new LinkedHashMap>(serviceIds.size()); + for (String serviceId : serviceIds) { + List instanceEntityList = getInstanceList(serviceId); + for (InstanceEntity instance : instanceEntityList) { + List metadataList = metadataMap.get(serviceId); + if (metadataList == null) { + metadataList = new ArrayList(); + metadataMap.put(serviceId, metadataList); + } + StringBuilder stringBuilder = new StringBuilder(); + int index = 0; + for (String metadataKey : metadataKeys) { + if (StringUtils.equals(metadataKey, DiscoveryConstant.HOST + ":" + DiscoveryConstant.PORT)) { + stringBuilder.append(instance.getHost() + ":" + instance.getPort()); + } else if (StringUtils.equals(metadataKey, DiscoveryConstant.HOST)) { + stringBuilder.append(instance.getHost()); + } else if (StringUtils.equals(metadataKey, DiscoveryConstant.PORT)) { + stringBuilder.append(instance.getPort()); + } else { + stringBuilder.append(instance.getMetadata().get(metadataKey)); + } + if (index < metadataKeys.size() - 1) { + stringBuilder.append(separate); + } + + index++; + } + + String metadataValues = stringBuilder.toString(); + if (!metadataList.contains(metadataValues)) { + if (StringUtils.isNotEmpty(metadataValues)) { + metadataList.add(metadataValues); + } + } + } + } + + return metadataMap; + } } \ No newline at end of file diff --git a/discovery-plugin-admin-center/discovery-plugin-admin-center-starter-swagger/src/main/java/com/nepxion/discovery/plugin/admincenter/endpoint/ServiceEndpoint.java b/discovery-plugin-admin-center/discovery-plugin-admin-center-starter-swagger/src/main/java/com/nepxion/discovery/plugin/admincenter/endpoint/ServiceEndpoint.java index fa15e23266..e6ffac6095 100644 --- a/discovery-plugin-admin-center/discovery-plugin-admin-center-starter-swagger/src/main/java/com/nepxion/discovery/plugin/admincenter/endpoint/ServiceEndpoint.java +++ b/discovery-plugin-admin-center/discovery-plugin-admin-center-starter-swagger/src/main/java/com/nepxion/discovery/plugin/admincenter/endpoint/ServiceEndpoint.java @@ -29,6 +29,7 @@ import com.nepxion.discovery.common.entity.GatewayType; import com.nepxion.discovery.common.entity.InstanceEntity; +import com.nepxion.discovery.common.entity.MetadataParameter; import com.nepxion.discovery.common.entity.ServiceType; import com.nepxion.discovery.common.util.ResponseUtil; import com.nepxion.discovery.plugin.admincenter.resource.ServiceResource; @@ -117,6 +118,20 @@ public ResponseEntity instanceMap(@RequestBody @ApiParam(value = "服务组 return doInstanceMap(groups); } + @RequestMapping(path = "/metadata-map/{metadataKey}", method = RequestMethod.POST) + @ApiOperation(value = "获取注册中心的服务实例元数据值列表的Map(值包含单个元数据)", notes = "", response = ResponseEntity.class, httpMethod = "POST") + @ResponseBody + public ResponseEntity metadataMap(@PathVariable(value = "metadataKey") @ApiParam(value = "元数据键名", required = true) String metadataKey, @RequestBody @ApiParam(value = "服务名列表", required = true) List serviceIds) { + return doMetadataMap(metadataKey, serviceIds); + } + + @RequestMapping(path = "/metadata-map", method = RequestMethod.POST) + @ApiOperation(value = "获取注册中心的服务实例元数据值列表的Map(值包含多个元数据,通过分隔符分隔)", notes = "", response = ResponseEntity.class, httpMethod = "POST") + @ResponseBody + public ResponseEntity metadataMap(@RequestBody @ApiParam(value = "元数据查询参数对象", required = true) MetadataParameter metadataParameter) { + return doMetadataMap(metadataParameter); + } + private ResponseEntity doDiscoveryType() { try { String discoveryType = serviceResource.getDiscoveryType().toString(); @@ -241,4 +256,24 @@ private ResponseEntity doInstanceMap(List groups) { return ResponseUtil.getFailureResponse(e); } } + + private ResponseEntity doMetadataMap(String metadataKey, List serviceIds) { + try { + Map> metadataMap = serviceResource.getMetadataMap(metadataKey, serviceIds); + + return ResponseUtil.getSuccessResponse(metadataMap); + } catch (Exception e) { + return ResponseUtil.getFailureResponse(e); + } + } + + private ResponseEntity doMetadataMap(MetadataParameter metadataParameter) { + try { + Map> metadataMap = serviceResource.getMetadataMap(metadataParameter); + + return ResponseUtil.getSuccessResponse(metadataMap); + } catch (Exception e) { + return ResponseUtil.getFailureResponse(e); + } + } } \ No newline at end of file diff --git a/discovery-plugin-admin-center/discovery-plugin-admin-center-starter-swagger/src/main/java/com/nepxion/discovery/plugin/admincenter/resource/ServiceResource.java b/discovery-plugin-admin-center/discovery-plugin-admin-center-starter-swagger/src/main/java/com/nepxion/discovery/plugin/admincenter/resource/ServiceResource.java index aa8227b2a7..0f170c59ee 100644 --- a/discovery-plugin-admin-center/discovery-plugin-admin-center-starter-swagger/src/main/java/com/nepxion/discovery/plugin/admincenter/resource/ServiceResource.java +++ b/discovery-plugin-admin-center/discovery-plugin-admin-center-starter-swagger/src/main/java/com/nepxion/discovery/plugin/admincenter/resource/ServiceResource.java @@ -17,6 +17,7 @@ import com.nepxion.discovery.common.entity.DiscoveryType; import com.nepxion.discovery.common.entity.GatewayType; import com.nepxion.discovery.common.entity.InstanceEntity; +import com.nepxion.discovery.common.entity.MetadataParameter; import com.nepxion.discovery.common.entity.ServiceType; public interface ServiceResource { @@ -41,4 +42,8 @@ public interface ServiceResource { List getInstanceList(String serviceId); Map> getInstanceMap(List groups); + + Map> getMetadataMap(String metadataKey, List serviceIds); + + Map> getMetadataMap(MetadataParameter metadataParameter); } \ No newline at end of file diff --git a/discovery-plugin-admin-center/discovery-plugin-admin-center-starter-swagger/src/main/java/com/nepxion/discovery/plugin/admincenter/resource/ServiceResourceImpl.java b/discovery-plugin-admin-center/discovery-plugin-admin-center-starter-swagger/src/main/java/com/nepxion/discovery/plugin/admincenter/resource/ServiceResourceImpl.java index 3835a3c816..ab2e975356 100644 --- a/discovery-plugin-admin-center/discovery-plugin-admin-center-starter-swagger/src/main/java/com/nepxion/discovery/plugin/admincenter/resource/ServiceResourceImpl.java +++ b/discovery-plugin-admin-center/discovery-plugin-admin-center-starter-swagger/src/main/java/com/nepxion/discovery/plugin/admincenter/resource/ServiceResourceImpl.java @@ -22,11 +22,13 @@ import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient; +import com.nepxion.discovery.common.constant.DiscoveryConstant; import com.nepxion.discovery.common.delegate.DiscoveryClientDelegate; import com.nepxion.discovery.common.entity.DiscoveryType; import com.nepxion.discovery.common.entity.GatewayType; import com.nepxion.discovery.common.entity.InstanceEntity; import com.nepxion.discovery.common.entity.InstanceEntityWrapper; +import com.nepxion.discovery.common.entity.MetadataParameter; import com.nepxion.discovery.common.entity.ServiceType; public class ServiceResourceImpl implements ServiceResource { @@ -78,9 +80,8 @@ public List getGroups() { for (String service : services) { List instanceEntityList = getInstanceList(service); for (InstanceEntity instance : instanceEntityList) { - String plugin = instance.getPlugin(); String group = instance.getGroup(); - if (StringUtils.isNotEmpty(plugin) && !groupList.contains(group)) { + if (!groupList.contains(group)) { groupList.add(group); } } @@ -275,9 +276,8 @@ public Map> getInstanceMap(List groups) { List instanceEntityList = getInstanceList(service); if (CollectionUtils.isNotEmpty(groups)) { for (InstanceEntity instance : instanceEntityList) { - String plugin = instance.getPlugin(); String group = instance.getGroup(); - if (StringUtils.isNotEmpty(plugin) && groups.contains(group)) { + if (groups.contains(group)) { List instanceList = instanceMap.get(service); if (instanceList == null) { instanceList = new ArrayList(); @@ -293,4 +293,81 @@ public Map> getInstanceMap(List groups) { return instanceMap; } + + @Override + public Map> getMetadataMap(String metadataKey, List serviceIds) { + Map> metadataMap = new LinkedHashMap>(serviceIds.size()); + for (String serviceId : serviceIds) { + List instanceEntityList = getInstanceList(serviceId); + for (InstanceEntity instance : instanceEntityList) { + List metadataList = metadataMap.get(serviceId); + if (metadataList == null) { + metadataList = new ArrayList(); + metadataMap.put(serviceId, metadataList); + } + String metadataValue = null; + if (StringUtils.equals(metadataKey, DiscoveryConstant.HOST + ":" + DiscoveryConstant.PORT)) { + metadataValue = instance.getHost() + ":" + instance.getPort(); + } else if (StringUtils.equals(metadataKey, DiscoveryConstant.HOST)) { + metadataValue = instance.getHost(); + } else if (StringUtils.equals(metadataKey, DiscoveryConstant.PORT)) { + metadataValue = String.valueOf(instance.getPort()); + } else { + metadataValue = instance.getMetadata().get(metadataKey); + } + if (!metadataList.contains(metadataValue)) { + if (StringUtils.isNotEmpty(metadataValue)) { + metadataList.add(metadataValue); + } + } + } + } + + return metadataMap; + } + + @Override + public Map> getMetadataMap(MetadataParameter metadataParameter) { + List metadataKeys = metadataParameter.getMetadataKeys(); + List serviceIds = metadataParameter.getServiceIds(); + String separate = metadataParameter.getSeparate(); + Map> metadataMap = new LinkedHashMap>(serviceIds.size()); + for (String serviceId : serviceIds) { + List instanceEntityList = getInstanceList(serviceId); + for (InstanceEntity instance : instanceEntityList) { + List metadataList = metadataMap.get(serviceId); + if (metadataList == null) { + metadataList = new ArrayList(); + metadataMap.put(serviceId, metadataList); + } + StringBuilder stringBuilder = new StringBuilder(); + int index = 0; + for (String metadataKey : metadataKeys) { + if (StringUtils.equals(metadataKey, DiscoveryConstant.HOST + ":" + DiscoveryConstant.PORT)) { + stringBuilder.append(instance.getHost() + ":" + instance.getPort()); + } else if (StringUtils.equals(metadataKey, DiscoveryConstant.HOST)) { + stringBuilder.append(instance.getHost()); + } else if (StringUtils.equals(metadataKey, DiscoveryConstant.PORT)) { + stringBuilder.append(instance.getPort()); + } else { + stringBuilder.append(instance.getMetadata().get(metadataKey)); + } + if (index < metadataKeys.size() - 1) { + stringBuilder.append(separate); + } + + index++; + } + + String metadataValues = stringBuilder.toString(); + if (!metadataList.contains(metadataValues)) { + if (StringUtils.isNotEmpty(metadataValues)) { + metadataList.add(metadataValues); + } + } + } + } + + return metadataMap; + } } \ No newline at end of file diff --git a/discovery-plugin-admin-center/discovery-plugin-admin-center-starter/src/main/java/com/nepxion/discovery/plugin/admincenter/endpoint/ServiceEndpoint.java b/discovery-plugin-admin-center/discovery-plugin-admin-center-starter/src/main/java/com/nepxion/discovery/plugin/admincenter/endpoint/ServiceEndpoint.java index ee30ed3510..8b6fbbb2b4 100644 --- a/discovery-plugin-admin-center/discovery-plugin-admin-center-starter/src/main/java/com/nepxion/discovery/plugin/admincenter/endpoint/ServiceEndpoint.java +++ b/discovery-plugin-admin-center/discovery-plugin-admin-center-starter/src/main/java/com/nepxion/discovery/plugin/admincenter/endpoint/ServiceEndpoint.java @@ -25,6 +25,7 @@ import com.nepxion.discovery.common.entity.GatewayType; import com.nepxion.discovery.common.entity.InstanceEntity; +import com.nepxion.discovery.common.entity.MetadataParameter; import com.nepxion.discovery.common.entity.ServiceType; import com.nepxion.discovery.common.util.ResponseUtil; import com.nepxion.discovery.plugin.admincenter.resource.ServiceResource; @@ -101,6 +102,18 @@ public ResponseEntity instanceMap(@RequestBody List groups) { return doInstanceMap(groups); } + @RequestMapping(path = "/metadata-map/{metadataKey}", method = RequestMethod.POST) + @ResponseBody + public ResponseEntity metadataMap(@PathVariable(value = "metadataKey") String metadataKey, @RequestBody List serviceIds) { + return doMetadataMap(metadataKey, serviceIds); + } + + @RequestMapping(path = "/metadata-map", method = RequestMethod.POST) + @ResponseBody + public ResponseEntity metadataMap(@RequestBody MetadataParameter metadataParameter) { + return doMetadataMap(metadataParameter); + } + private ResponseEntity doDiscoveryType() { try { String discoveryType = serviceResource.getDiscoveryType().toString(); @@ -225,4 +238,24 @@ private ResponseEntity doInstanceMap(List groups) { return ResponseUtil.getFailureResponse(e); } } + + private ResponseEntity doMetadataMap(String metadataKey, List serviceIds) { + try { + Map> metadataMap = serviceResource.getMetadataMap(metadataKey, serviceIds); + + return ResponseUtil.getSuccessResponse(metadataMap); + } catch (Exception e) { + return ResponseUtil.getFailureResponse(e); + } + } + + private ResponseEntity doMetadataMap(MetadataParameter metadataParameter) { + try { + Map> metadataMap = serviceResource.getMetadataMap(metadataParameter); + + return ResponseUtil.getSuccessResponse(metadataMap); + } catch (Exception e) { + return ResponseUtil.getFailureResponse(e); + } + } } \ No newline at end of file diff --git a/discovery-plugin-admin-center/discovery-plugin-admin-center-starter/src/main/java/com/nepxion/discovery/plugin/admincenter/resource/ServiceResource.java b/discovery-plugin-admin-center/discovery-plugin-admin-center-starter/src/main/java/com/nepxion/discovery/plugin/admincenter/resource/ServiceResource.java index aa8227b2a7..0f170c59ee 100644 --- a/discovery-plugin-admin-center/discovery-plugin-admin-center-starter/src/main/java/com/nepxion/discovery/plugin/admincenter/resource/ServiceResource.java +++ b/discovery-plugin-admin-center/discovery-plugin-admin-center-starter/src/main/java/com/nepxion/discovery/plugin/admincenter/resource/ServiceResource.java @@ -17,6 +17,7 @@ import com.nepxion.discovery.common.entity.DiscoveryType; import com.nepxion.discovery.common.entity.GatewayType; import com.nepxion.discovery.common.entity.InstanceEntity; +import com.nepxion.discovery.common.entity.MetadataParameter; import com.nepxion.discovery.common.entity.ServiceType; public interface ServiceResource { @@ -41,4 +42,8 @@ public interface ServiceResource { List getInstanceList(String serviceId); Map> getInstanceMap(List groups); + + Map> getMetadataMap(String metadataKey, List serviceIds); + + Map> getMetadataMap(MetadataParameter metadataParameter); } \ No newline at end of file diff --git a/discovery-plugin-admin-center/discovery-plugin-admin-center-starter/src/main/java/com/nepxion/discovery/plugin/admincenter/resource/ServiceResourceImpl.java b/discovery-plugin-admin-center/discovery-plugin-admin-center-starter/src/main/java/com/nepxion/discovery/plugin/admincenter/resource/ServiceResourceImpl.java index 3835a3c816..ab2e975356 100644 --- a/discovery-plugin-admin-center/discovery-plugin-admin-center-starter/src/main/java/com/nepxion/discovery/plugin/admincenter/resource/ServiceResourceImpl.java +++ b/discovery-plugin-admin-center/discovery-plugin-admin-center-starter/src/main/java/com/nepxion/discovery/plugin/admincenter/resource/ServiceResourceImpl.java @@ -22,11 +22,13 @@ import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient; +import com.nepxion.discovery.common.constant.DiscoveryConstant; import com.nepxion.discovery.common.delegate.DiscoveryClientDelegate; import com.nepxion.discovery.common.entity.DiscoveryType; import com.nepxion.discovery.common.entity.GatewayType; import com.nepxion.discovery.common.entity.InstanceEntity; import com.nepxion.discovery.common.entity.InstanceEntityWrapper; +import com.nepxion.discovery.common.entity.MetadataParameter; import com.nepxion.discovery.common.entity.ServiceType; public class ServiceResourceImpl implements ServiceResource { @@ -78,9 +80,8 @@ public List getGroups() { for (String service : services) { List instanceEntityList = getInstanceList(service); for (InstanceEntity instance : instanceEntityList) { - String plugin = instance.getPlugin(); String group = instance.getGroup(); - if (StringUtils.isNotEmpty(plugin) && !groupList.contains(group)) { + if (!groupList.contains(group)) { groupList.add(group); } } @@ -275,9 +276,8 @@ public Map> getInstanceMap(List groups) { List instanceEntityList = getInstanceList(service); if (CollectionUtils.isNotEmpty(groups)) { for (InstanceEntity instance : instanceEntityList) { - String plugin = instance.getPlugin(); String group = instance.getGroup(); - if (StringUtils.isNotEmpty(plugin) && groups.contains(group)) { + if (groups.contains(group)) { List instanceList = instanceMap.get(service); if (instanceList == null) { instanceList = new ArrayList(); @@ -293,4 +293,81 @@ public Map> getInstanceMap(List groups) { return instanceMap; } + + @Override + public Map> getMetadataMap(String metadataKey, List serviceIds) { + Map> metadataMap = new LinkedHashMap>(serviceIds.size()); + for (String serviceId : serviceIds) { + List instanceEntityList = getInstanceList(serviceId); + for (InstanceEntity instance : instanceEntityList) { + List metadataList = metadataMap.get(serviceId); + if (metadataList == null) { + metadataList = new ArrayList(); + metadataMap.put(serviceId, metadataList); + } + String metadataValue = null; + if (StringUtils.equals(metadataKey, DiscoveryConstant.HOST + ":" + DiscoveryConstant.PORT)) { + metadataValue = instance.getHost() + ":" + instance.getPort(); + } else if (StringUtils.equals(metadataKey, DiscoveryConstant.HOST)) { + metadataValue = instance.getHost(); + } else if (StringUtils.equals(metadataKey, DiscoveryConstant.PORT)) { + metadataValue = String.valueOf(instance.getPort()); + } else { + metadataValue = instance.getMetadata().get(metadataKey); + } + if (!metadataList.contains(metadataValue)) { + if (StringUtils.isNotEmpty(metadataValue)) { + metadataList.add(metadataValue); + } + } + } + } + + return metadataMap; + } + + @Override + public Map> getMetadataMap(MetadataParameter metadataParameter) { + List metadataKeys = metadataParameter.getMetadataKeys(); + List serviceIds = metadataParameter.getServiceIds(); + String separate = metadataParameter.getSeparate(); + Map> metadataMap = new LinkedHashMap>(serviceIds.size()); + for (String serviceId : serviceIds) { + List instanceEntityList = getInstanceList(serviceId); + for (InstanceEntity instance : instanceEntityList) { + List metadataList = metadataMap.get(serviceId); + if (metadataList == null) { + metadataList = new ArrayList(); + metadataMap.put(serviceId, metadataList); + } + StringBuilder stringBuilder = new StringBuilder(); + int index = 0; + for (String metadataKey : metadataKeys) { + if (StringUtils.equals(metadataKey, DiscoveryConstant.HOST + ":" + DiscoveryConstant.PORT)) { + stringBuilder.append(instance.getHost() + ":" + instance.getPort()); + } else if (StringUtils.equals(metadataKey, DiscoveryConstant.HOST)) { + stringBuilder.append(instance.getHost()); + } else if (StringUtils.equals(metadataKey, DiscoveryConstant.PORT)) { + stringBuilder.append(instance.getPort()); + } else { + stringBuilder.append(instance.getMetadata().get(metadataKey)); + } + if (index < metadataKeys.size() - 1) { + stringBuilder.append(separate); + } + + index++; + } + + String metadataValues = stringBuilder.toString(); + if (!metadataList.contains(metadataValues)) { + if (StringUtils.isNotEmpty(metadataValues)) { + metadataList.add(metadataValues); + } + } + } + } + + return metadataMap; + } } \ No newline at end of file