Skip to content

Commit

Permalink
Fix test failed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nkorange committed Oct 14, 2018
1 parent 712c021 commit 71cf533
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public void registerService(String serviceName, Instance instance) throws NacosE
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public void setTenant(String tenant) {
}

public String generateInstanceId() {
return getIp() + "#" + getPort() + "#" + getCluster().getName() + "#" + serviceName();
return getIp() + "#" + getPort() + "#" + getCluster().getName() + "#" + getService().getName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -210,14 +211,19 @@ public static String getDomStoreKey(Domain dom) {
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]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand Down Expand Up @@ -526,7 +527,10 @@ public String regService(HttpServletRequest request) throws Exception {
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));
Expand Down Expand Up @@ -1046,7 +1050,7 @@ public JSONObject srvAllIP(HttpServletRequest request) throws Exception {
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);
}
Expand Down Expand Up @@ -1178,7 +1182,7 @@ public JSONObject srvIPXT(HttpServletRequest request) throws Exception {
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());
Expand Down

0 comments on commit 71cf533

Please sign in to comment.