fix bug
This commit is contained in:
parent
4f56452999
commit
6b4a9b72a5
@ -44,12 +44,6 @@ public class Constants {
|
|||||||
|
|
||||||
public static final String GROUP = "group";
|
public static final String GROUP = "group";
|
||||||
|
|
||||||
public static final String HEART_BEAT_TIMEOUT = "heartBeatTimeout";
|
|
||||||
|
|
||||||
public static final String IP_DELETE_TIMEOUT = "ipDeleteTimeout";
|
|
||||||
|
|
||||||
public static final String HEART_BEAT_INTERVAL = "heartBeatInterval";
|
|
||||||
|
|
||||||
public static final String LAST_MODIFIED = "Last-Modified";
|
public static final String LAST_MODIFIED = "Last-Modified";
|
||||||
|
|
||||||
public static final String ACCEPT_ENCODING = "Accept-Encoding";
|
public static final String ACCEPT_ENCODING = "Accept-Encoding";
|
||||||
@ -166,4 +160,6 @@ public class Constants {
|
|||||||
public static final String SERVICE_INFO_SPLITER = "@@";
|
public static final String SERVICE_INFO_SPLITER = "@@";
|
||||||
|
|
||||||
public static final String NULL_STRING = "null";
|
public static final String NULL_STRING = "null";
|
||||||
|
|
||||||
|
public static final String NUMBER_PATTERN = "^\\d+$";
|
||||||
}
|
}
|
||||||
|
@ -27,4 +27,10 @@ public class PreservedMetadataKeys {
|
|||||||
* The key to indicate the registry source of service instance, such as Dubbo, SpringCloud, etc.
|
* The key to indicate the registry source of service instance, such as Dubbo, SpringCloud, etc.
|
||||||
*/
|
*/
|
||||||
public static final String REGISTER_SOURCE = "preserved.register.source";
|
public static final String REGISTER_SOURCE = "preserved.register.source";
|
||||||
|
|
||||||
|
public static final String HEART_BEAT_TIMEOUT = "preserved.heart.beat.timeout";
|
||||||
|
|
||||||
|
public static final String IP_DELETE_TIMEOUT = "preserved.ip.delete.timeout";
|
||||||
|
|
||||||
|
public static final String HEART_BEAT_INTERVAL = "preserved.heart.beat.interval";
|
||||||
}
|
}
|
||||||
|
@ -17,10 +17,15 @@ package com.alibaba.nacos.api.naming.pojo;
|
|||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.annotation.JSONField;
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
|
import com.alibaba.nacos.api.common.Constants;
|
||||||
|
import com.alibaba.nacos.api.naming.PreservedMetadataKeys;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static com.alibaba.nacos.api.common.Constants.NUMBER_PATTERN;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instance
|
* Instance
|
||||||
*
|
*
|
||||||
@ -193,4 +198,27 @@ public class Instance {
|
|||||||
return str1 == null ? str2 == null : str1.equals(str2);
|
return str1 == null ? str2 == null : str1.equals(str2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long getInstanceHeartBeatInterval() {
|
||||||
|
return getMetaDataByKeyWithDefault(PreservedMetadataKeys.HEART_BEAT_INTERVAL, Constants.DEFAULT_HEART_BEAT_INTERVAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getInstanceHeartBeatTimeOut() {
|
||||||
|
return getMetaDataByKeyWithDefault(PreservedMetadataKeys.HEART_BEAT_TIMEOUT, Constants.DEFAULT_HEART_BEAT_TIMEOUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getIpDeleteTimeout() {
|
||||||
|
return getMetaDataByKeyWithDefault(PreservedMetadataKeys.IP_DELETE_TIMEOUT, Constants.DEFAULT_IP_DELETE_TIMEOUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private long getMetaDataByKeyWithDefault( String key, long defaultValue) {
|
||||||
|
if (getMetadata() == null || getMetadata().isEmpty()) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
String value = getMetadata().get(key);
|
||||||
|
if (!StringUtils.isEmpty(value) && value.matches(NUMBER_PATTERN)) {
|
||||||
|
return Long.valueOf(value);
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ import com.alibaba.nacos.api.PropertyKeyConst;
|
|||||||
import com.alibaba.nacos.api.common.Constants;
|
import com.alibaba.nacos.api.common.Constants;
|
||||||
import com.alibaba.nacos.api.exception.NacosException;
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
import com.alibaba.nacos.api.naming.NamingService;
|
import com.alibaba.nacos.api.naming.NamingService;
|
||||||
|
import com.alibaba.nacos.api.naming.PreservedMetadataKeys;
|
||||||
import com.alibaba.nacos.api.naming.listener.EventListener;
|
import com.alibaba.nacos.api.naming.listener.EventListener;
|
||||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||||
import com.alibaba.nacos.api.naming.pojo.ListView;
|
import com.alibaba.nacos.api.naming.pojo.ListView;
|
||||||
@ -47,7 +48,6 @@ import java.util.concurrent.TimeUnit;
|
|||||||
@SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule")
|
@SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule")
|
||||||
public class NacosNamingService implements NamingService {
|
public class NacosNamingService implements NamingService {
|
||||||
private static final String DEFAULT_PORT = "8080";
|
private static final String DEFAULT_PORT = "8080";
|
||||||
private static final String NUMBER_PATTERN = "^\\d+$";
|
|
||||||
private static final long DEFAULT_HEART_BEAT_INTERVAL = TimeUnit.SECONDS.toMillis(5);
|
private static final long DEFAULT_HEART_BEAT_INTERVAL = TimeUnit.SECONDS.toMillis(5);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -197,7 +197,7 @@ public class NacosNamingService implements NamingService {
|
|||||||
beatInfo.setMetadata(instance.getMetadata());
|
beatInfo.setMetadata(instance.getMetadata());
|
||||||
beatInfo.setScheduled(false);
|
beatInfo.setScheduled(false);
|
||||||
beatInfo.setTime(System.currentTimeMillis());
|
beatInfo.setTime(System.currentTimeMillis());
|
||||||
long instanceInterval = getInstanceHeartBeatInterval(instance);
|
long instanceInterval = instance.getInstanceHeartBeatInterval();
|
||||||
beatInfo.setPeriod(instanceInterval == 0 ? DEFAULT_HEART_BEAT_INTERVAL : instanceInterval);
|
beatInfo.setPeriod(instanceInterval == 0 ? DEFAULT_HEART_BEAT_INTERVAL : instanceInterval);
|
||||||
|
|
||||||
beatReactor.addBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), beatInfo);
|
beatReactor.addBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), beatInfo);
|
||||||
@ -206,17 +206,6 @@ public class NacosNamingService implements NamingService {
|
|||||||
serverProxy.registerService(NamingUtils.getGroupedName(serviceName, groupName), groupName, instance);
|
serverProxy.registerService(NamingUtils.getGroupedName(serviceName, groupName), groupName, instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
private long getInstanceHeartBeatInterval(Instance instance) {
|
|
||||||
Map<String, String> metaData = instance.getMetadata();
|
|
||||||
if (metaData == null || metaData.isEmpty()) {
|
|
||||||
return Constants.DEFAULT_HEART_BEAT_INTERVAL;
|
|
||||||
}
|
|
||||||
String interval = metaData.get(Constants.HEART_BEAT_INTERVAL);
|
|
||||||
if (!StringUtils.isEmpty(interval) && interval.matches(NUMBER_PATTERN)) {
|
|
||||||
return Long.valueOf(interval);
|
|
||||||
}
|
|
||||||
return Constants.DEFAULT_HEART_BEAT_TIMEOUT;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deregisterInstance(String serviceName, String ip, int port) throws NacosException {
|
public void deregisterInstance(String serviceName, String ip, int port) throws NacosException {
|
||||||
|
@ -18,6 +18,7 @@ package com.alibaba.nacos.naming.healthcheck;
|
|||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.annotation.JSONField;
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
import com.alibaba.nacos.api.common.Constants;
|
import com.alibaba.nacos.api.common.Constants;
|
||||||
|
import com.alibaba.nacos.api.naming.PreservedMetadataKeys;
|
||||||
import com.alibaba.nacos.naming.boot.RunningConfig;
|
import com.alibaba.nacos.naming.boot.RunningConfig;
|
||||||
import com.alibaba.nacos.naming.boot.SpringContext;
|
import com.alibaba.nacos.naming.boot.SpringContext;
|
||||||
import com.alibaba.nacos.naming.core.DistroMapper;
|
import com.alibaba.nacos.naming.core.DistroMapper;
|
||||||
@ -79,13 +80,13 @@ public class ClientBeatCheckTask implements Runnable {
|
|||||||
|
|
||||||
// first set health status of instances:
|
// first set health status of instances:
|
||||||
for (Instance instance : instances) {
|
for (Instance instance : instances) {
|
||||||
if (System.currentTimeMillis() - instance.getLastBeat() > getInstanceHeartBeatTimeOut(instance)) {
|
if (System.currentTimeMillis() - instance.getLastBeat() > instance.getInstanceHeartBeatTimeOut()) {
|
||||||
if (!instance.isMarked()) {
|
if (!instance.isMarked()) {
|
||||||
if (instance.isHealthy()) {
|
if (instance.isHealthy()) {
|
||||||
instance.setHealthy(false);
|
instance.setHealthy(false);
|
||||||
Loggers.EVT_LOG.info("{POS} {IP-DISABLED} valid: {}:{}@{}@{}, region: {}, msg: client timeout after {}, last beat: {}",
|
Loggers.EVT_LOG.info("{POS} {IP-DISABLED} valid: {}:{}@{}@{}, region: {}, msg: client timeout after {}, last beat: {}",
|
||||||
instance.getIp(), instance.getPort(), instance.getClusterName(), service.getName(),
|
instance.getIp(), instance.getPort(), instance.getClusterName(), service.getName(),
|
||||||
UtilsAndCommons.LOCALHOST_SITE, getInstanceHeartBeatTimeOut(instance), instance.getLastBeat());
|
UtilsAndCommons.LOCALHOST_SITE, instance.getInstanceHeartBeatTimeOut(), instance.getLastBeat());
|
||||||
getPushService().serviceChanged(service.getNamespaceId(), service.getName());
|
getPushService().serviceChanged(service.getNamespaceId(), service.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -98,7 +99,7 @@ public class ClientBeatCheckTask implements Runnable {
|
|||||||
|
|
||||||
// then remove obsolete instances:
|
// then remove obsolete instances:
|
||||||
for (Instance instance : instances) {
|
for (Instance instance : instances) {
|
||||||
if (System.currentTimeMillis() - instance.getLastBeat() > getIpDeleteTimeout(instance)) {
|
if (System.currentTimeMillis() - instance.getLastBeat() > instance.getIpDeleteTimeout()) {
|
||||||
// delete instance
|
// delete instance
|
||||||
Loggers.SRV_LOG.info("[AUTO-DELETE-IP] service: {}, ip: {}", service.getName(), JSON.toJSONString(instance));
|
Loggers.SRV_LOG.info("[AUTO-DELETE-IP] service: {}, ip: {}", service.getName(), JSON.toJSONString(instance));
|
||||||
deleteIP(instance);
|
deleteIP(instance);
|
||||||
@ -111,29 +112,6 @@ public class ClientBeatCheckTask implements Runnable {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private long getInstanceHeartBeatTimeOut(Instance instance) {
|
|
||||||
Map<String, String> metaData = instance.getMetadata();
|
|
||||||
if(metaData == null || metaData.isEmpty()) {
|
|
||||||
return Constants.DEFAULT_HEART_BEAT_TIMEOUT;
|
|
||||||
}
|
|
||||||
String timeout = metaData.get(Constants.HEART_BEAT_TIMEOUT);
|
|
||||||
if(!StringUtils.isEmpty(timeout) && timeout.matches(NUMBER_PATTERN)){
|
|
||||||
return Long.valueOf(timeout);
|
|
||||||
}
|
|
||||||
return Constants.DEFAULT_HEART_BEAT_TIMEOUT;
|
|
||||||
}
|
|
||||||
|
|
||||||
private long getIpDeleteTimeout(Instance instance) {
|
|
||||||
Map<String, String> metaData = instance.getMetadata();
|
|
||||||
if(metaData == null || metaData.isEmpty()) {
|
|
||||||
return Constants.DEFAULT_IP_DELETE_TIMEOUT;
|
|
||||||
}
|
|
||||||
String timeout = metaData.get(Constants.IP_DELETE_TIMEOUT);
|
|
||||||
if(!StringUtils.isEmpty(timeout) && timeout.matches(NUMBER_PATTERN)){
|
|
||||||
return Long.valueOf(timeout);
|
|
||||||
}
|
|
||||||
return Constants.DEFAULT_IP_DELETE_TIMEOUT;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void deleteIP(Instance instance) {
|
private void deleteIP(Instance instance) {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user