Skip to content

Commit

Permalink
Add two getMetadataMap interfaces on console and admincenter modules
Browse files Browse the repository at this point in the history
  • Loading branch information
HaojunRen committed Sep 24, 2024
1 parent 9335633 commit b6e08a9
Show file tree
Hide file tree
Showing 10 changed files with 428 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.nepxion.discovery.common.entity;

/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @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<String> metadataKeys;
private List<String> serviceIds;
private String separate = SEPARATE;

public List<String> getMetadataKeys() {
return metadataKeys;
}

public void setMetadataKeys(List<String> metadataKeys) {
this.metadataKeys = metadataKeys;
}

public List<String> getServiceIds() {
return serviceIds;
}

public void setServiceIds(List<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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();
Expand Down Expand Up @@ -241,4 +256,24 @@ private ResponseEntity<?> doInstanceMap(List<String> groups) {
return ResponseUtil.getFailureResponse(e);
}
}

private ResponseEntity<?> doMetadataMap(String metadataKey, List<String> serviceIds) {
try {
Map<String, List<String>> metadataMap = serviceResource.getMetadataMap(metadataKey, serviceIds);

return ResponseUtil.getSuccessResponse(metadataMap);
} catch (Exception e) {
return ResponseUtil.getFailureResponse(e);
}
}

private ResponseEntity<?> doMetadataMap(MetadataParameter metadataParameter) {
try {
Map<String, List<String>> metadataMap = serviceResource.getMetadataMap(metadataParameter);

return ResponseUtil.getSuccessResponse(metadataMap);
} catch (Exception e) {
return ResponseUtil.getFailureResponse(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -41,4 +42,8 @@ public interface ServiceResource {
List<InstanceEntity> getInstanceList(String serviceId);

Map<String, List<InstanceEntity>> getInstanceMap(List<String> groups);

Map<String, List<String>> getMetadataMap(String metadataKey, List<String> serviceIds);

Map<String, List<String>> getMetadataMap(MetadataParameter metadataParameter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -78,9 +80,8 @@ public List<String> getGroups() {
for (String service : services) {
List<InstanceEntity> 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);
}
}
Expand Down Expand Up @@ -275,9 +276,8 @@ public Map<String, List<InstanceEntity>> getInstanceMap(List<String> groups) {
List<InstanceEntity> 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<InstanceEntity> instanceList = instanceMap.get(service);
if (instanceList == null) {
instanceList = new ArrayList<InstanceEntity>();
Expand All @@ -293,4 +293,81 @@ public Map<String, List<InstanceEntity>> getInstanceMap(List<String> groups) {

return instanceMap;
}

@Override
public Map<String, List<String>> getMetadataMap(String metadataKey, List<String> serviceIds) {
Map<String, List<String>> metadataMap = new LinkedHashMap<String, List<String>>(serviceIds.size());
for (String serviceId : serviceIds) {
List<InstanceEntity> instanceEntityList = getInstanceList(serviceId);
for (InstanceEntity instance : instanceEntityList) {
List<String> metadataList = metadataMap.get(serviceId);
if (metadataList == null) {
metadataList = new ArrayList<String>();
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<String, List<String>> getMetadataMap(MetadataParameter metadataParameter) {
List<String> metadataKeys = metadataParameter.getMetadataKeys();
List<String> serviceIds = metadataParameter.getServiceIds();
String separate = metadataParameter.getSeparate();
Map<String, List<String>> metadataMap = new LinkedHashMap<String, List<String>>(serviceIds.size());
for (String serviceId : serviceIds) {
List<InstanceEntity> instanceEntityList = getInstanceList(serviceId);
for (InstanceEntity instance : instanceEntityList) {
List<String> metadataList = metadataMap.get(serviceId);
if (metadataList == null) {
metadataList = new ArrayList<String>();
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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();
Expand Down Expand Up @@ -241,4 +256,24 @@ private ResponseEntity<?> doInstanceMap(List<String> groups) {
return ResponseUtil.getFailureResponse(e);
}
}

private ResponseEntity<?> doMetadataMap(String metadataKey, List<String> serviceIds) {
try {
Map<String, List<String>> metadataMap = serviceResource.getMetadataMap(metadataKey, serviceIds);

return ResponseUtil.getSuccessResponse(metadataMap);
} catch (Exception e) {
return ResponseUtil.getFailureResponse(e);
}
}

private ResponseEntity<?> doMetadataMap(MetadataParameter metadataParameter) {
try {
Map<String, List<String>> metadataMap = serviceResource.getMetadataMap(metadataParameter);

return ResponseUtil.getSuccessResponse(metadataMap);
} catch (Exception e) {
return ResponseUtil.getFailureResponse(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -41,4 +42,8 @@ public interface ServiceResource {
List<InstanceEntity> getInstanceList(String serviceId);

Map<String, List<InstanceEntity>> getInstanceMap(List<String> groups);

Map<String, List<String>> getMetadataMap(String metadataKey, List<String> serviceIds);

Map<String, List<String>> getMetadataMap(MetadataParameter metadataParameter);
}
Loading

0 comments on commit b6e08a9

Please sign in to comment.