Fix test failed bug

This commit is contained in:
nkorange 2018-10-14 12:07:50 +08:00
parent 712c0216d0
commit 71cf53361f
4 changed files with 21 additions and 10 deletions

View File

@ -158,6 +158,7 @@ public class NamingProxy {
params.put("ip", instance.getIp());
params.put("port", String.valueOf(instance.getPort()));
params.put("weight", String.valueOf(instance.getWeight()));
params.put("enable", String.valueOf(instance.isEnabled()));
params.put("healthy", String.valueOf(instance.isHealthy()));
params.put("metadata", JSON.toJSONString(instance.getMetadata()));
if (instance.getService() == null) {

View File

@ -307,7 +307,7 @@ public class IpAddress extends Instance implements Comparable {
}
public String generateInstanceId() {
return getIp() + "#" + getPort() + "#" + getCluster().getName() + "#" + serviceName();
return getIp() + "#" + getPort() + "#" + getCluster().getName() + "#" + getService().getName();
}
@Override

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.naming.misc;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
@ -210,14 +211,19 @@ public class UtilsAndCommons {
public static Map<String, String> parseMetadata(String metadata) throws NacosException {
Map<String, String> metadataMap = new HashMap<>(16);
String[] datas = metadata.split(",");
if (datas.length > 0) {
for (String data : datas) {
String[] kv = data.split("=");
if (kv.length != 2) {
throw new NacosException(NacosException.INVALID_PARAM, "metadata format incorrect:" + metadata);
try {
metadataMap = JSON.parseObject(metadata, new TypeReference<Map<String, String>>(){});
} catch (Exception e) {
String[] datas = metadata.split(",");
if (datas.length > 0) {
for (String data : datas) {
String[] kv = data.split("=");
if (kv.length != 2) {
throw new NacosException(NacosException.INVALID_PARAM, "metadata format incorrect:" + metadata);
}
metadataMap.put(kv[0], kv[1]);
}
metadataMap.put(kv[0], kv[1]);
}
}

View File

@ -20,6 +20,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.nacos.api.naming.pojo.AbstractHealthChecker;
import com.alibaba.nacos.api.naming.pojo.Service;
import com.alibaba.nacos.common.util.IoUtils;
import com.alibaba.nacos.common.util.Md5Utils;
import com.alibaba.nacos.common.util.SystemUtil;
@ -526,7 +527,10 @@ public class ApiCommands {
VirtualClusterDomain virtualClusterDomain = (VirtualClusterDomain) domainsManager.getDomain(dom);
IpAddress ipAddress = getIPAddress(request);
Service service = new Service(dom);
ipAddress.setApp(app);
ipAddress.setService(service);
ipAddress.setInstanceId(ipAddress.generateInstanceId());
ipAddress.setLastBeat(System.currentTimeMillis());
if (StringUtils.isNotEmpty(metadata)) {
ipAddress.setMetadata(UtilsAndCommons.parseMetadata(metadata));
@ -1046,7 +1050,7 @@ public class ApiCommands {
ipObj.put("valid", ip.isValid());
ipObj.put("weight", ip.getWeight());
ipObj.put("doubleWeight", ip.getWeight());
ipObj.put("instanceId", ip.generateInstanceId());
ipObj.put("instanceId", ip.getInstanceId());
ipObj.put("metadata", ip.getMetadata());
ipArray.add(ipObj);
}
@ -1178,7 +1182,7 @@ public class ApiCommands {
ipObj.put("port", ip.getPort());
ipObj.put("valid", entry.getKey());
ipObj.put("marked", ip.isMarked());
ipObj.put("instanceId", ip.generateInstanceId());
ipObj.put("instanceId", ip.getInstanceId());
ipObj.put("metadata", ip.getMetadata());
ipObj.put("enabled", ip.isEnabled());
ipObj.put("weight", ip.getWeight());