Merge pull request #1831 from stackisok/master

fix issue  #1825
This commit is contained in:
Fury Zhu 2019-09-10 17:31:04 +08:00 committed by GitHub
commit 1a2a02a92e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 8 deletions

View File

@ -311,9 +311,7 @@ public class InstanceController {
instance.setMetadata(UtilsAndCommons.parseMetadata(metadata));
}
if (!instance.validate()) {
throw new NacosException(NacosException.INVALID_PARAM, "instance format invalid:" + instance);
}
instance.validate();
return instance;
}

View File

@ -17,6 +17,7 @@ package com.alibaba.nacos.naming.core;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.nacos.naming.exception.NacosException;
import com.alibaba.nacos.naming.healthcheck.HealthCheckStatus;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
@ -197,7 +198,9 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme
ip.setWeight(0.0D);
}
if (!ip.validate()) {
try {
ip.validate();
} catch (NacosException e) {
throw new IllegalArgumentException("malformed ip config: " + json);
}
@ -298,19 +301,19 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme
return getIp() + "#" + getPort() + "#" + getClusterName() + "#" + getServiceName();
}
public boolean validate() {
public void validate() throws NacosException {
if (onlyContainsDigitAndDot()) {
Matcher matcher = IP_PATTERN.matcher(getIp() + ":" + getPort());
if (!matcher.matches()) {
return false;
throw new NacosException(NacosException.INVALID_PARAM, "instance format invalid: Your IP address is spelled incorrectly");
}
}
if (getWeight() > MAX_WEIGHT_VALUE || getWeight() < MIN_WEIGHT_VALUE) {
return false;
throw new NacosException(NacosException.INVALID_PARAM, "instance format invalid: The weights range from " +
MIN_WEIGHT_VALUE + " to " + MAX_WEIGHT_VALUE);
}
return true;
}
private boolean onlyContainsDigitAndDot() {