From 56977f4f49c0811a5a12afb1e0e6e31122817496 Mon Sep 17 00:00:00 2001 From: wangwei Date: Mon, 20 Jul 2020 10:33:56 +0800 Subject: [PATCH] [#3353]Adjust the value logic of clientBeatInterval --- .../com/alibaba/nacos/api/naming/pojo/Instance.java | 13 +++++++++++++ .../naming/controllers/InstanceController.java | 7 +++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java index 9f96cfb95c7..14f6742240f 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java @@ -231,6 +231,19 @@ public String getInstanceIdGenerator() { Constants.DEFAULT_INSTANCE_ID_GENERATOR); } + /** + * Returns {@code true} if this metadata contains the specified key. + * + * @param key metadata key + * @return {@code true} if this metadata contains the specified key + */ + public boolean containsMetadata(final String key) { + if (getMetadata() == null || getMetadata().isEmpty()) { + return false; + } + return getMetadata().containsKey(key); + } + private long getMetaDataByKeyWithDefault(final String key, final long defaultValue) { if (getMetadata() == null || getMetadata().isEmpty()) { return defaultValue; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java index 0d3aecfb7b0..403ff6ec9fa 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java @@ -20,6 +20,7 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.api.naming.NamingResponseCode; +import com.alibaba.nacos.api.naming.PreservedMetadataKeys; import com.alibaba.nacos.api.naming.utils.NamingUtils; import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.core.auth.ActionTypes; @@ -322,7 +323,7 @@ public ObjectNode detail(HttpServletRequest request) throws Exception { public ObjectNode beat(HttpServletRequest request) throws Exception { ObjectNode result = JacksonUtils.createEmptyJsonNode(); - result.put("clientBeatInterval", switchDomain.getClientBeatInterval()); + result.put(SwitchEntry.CLIENT_BEAT_INTERVAL, switchDomain.getClientBeatInterval()); String beat = WebUtils.optional(request, "beat", StringUtils.EMPTY); RsInfo clientBeat = null; @@ -385,7 +386,9 @@ public ObjectNode beat(HttpServletRequest request) throws Exception { service.processClientBeat(clientBeat); result.put(CommonParams.CODE, NamingResponseCode.OK); - result.put("clientBeatInterval", instance.getInstanceHeartBeatInterval()); + if (instance.containsMetadata(PreservedMetadataKeys.HEART_BEAT_INTERVAL)) { + result.put(SwitchEntry.CLIENT_BEAT_INTERVAL, instance.getInstanceHeartBeatInterval()); + } result.put(SwitchEntry.LIGHT_BEAT_ENABLED, switchDomain.isLightBeatEnabled()); return result; }