diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java index eefdd3368..838af5183 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java @@ -20,11 +20,7 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.config.impl.EventDispatcher.ServerlistChangeEvent; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.config.utils.IOUtils; -import com.alibaba.nacos.client.utils.TemplateUtils; -import com.alibaba.nacos.client.utils.EnvUtil; -import com.alibaba.nacos.client.utils.LogUtils; -import com.alibaba.nacos.client.utils.ParamUtil; -import com.alibaba.nacos.client.utils.StringUtils; +import com.alibaba.nacos.client.utils.*; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import org.slf4j.Logger; @@ -171,7 +167,7 @@ public class ServerListManager { private String initEndpoint(String endpointTmp) { String endpointPortTmp = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT); - if (StringUtils.isNotEmpty(endpointPortTmp)) { + if (StringUtils.isNotBlank(endpointPortTmp)) { endpointPort = Integer.parseInt(endpointPortTmp); } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index 3ebda5f2f..0b5266efa 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -97,11 +97,11 @@ public class NacosNamingService implements NamingService { eventDispatcher = new EventDispatcher(); serverProxy = new NamingProxy(namespace, endpoint, serverList); serverProxy.setProperties(properties); - beatReactor = new BeatReactor(serverProxy, clientBeatThreadCount(properties)); - hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir, isLoadCacheAtStart(properties), pollingThreadCount(properties)); + beatReactor = new BeatReactor(serverProxy, initClientBeatThreadCount(properties)); + hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir, isLoadCacheAtStart(properties), initPollingThreadCount(properties)); } - private int clientBeatThreadCount(Properties properties) { + private int initClientBeatThreadCount(Properties properties) { if (properties == null) { return UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT; @@ -113,7 +113,7 @@ public class NacosNamingService implements NamingService { return clientBeatThreadCount; } - private int pollingThreadCount(Properties properties) { + private int initPollingThreadCount(Properties properties) { if (properties == null) { return UtilAndComs.DEFAULT_POLLING_THREAD_COUNT; @@ -234,19 +234,17 @@ public class NacosNamingService implements NamingService { private void initWebRootContext() { // support the web context with ali-yun if the app deploy by EDAS - String webContext = System.getProperty(SystemPropertyKeyConst.NAMING_WEB_CONTEXT); - boolean isSetting = false; - if (StringUtils.isNotEmpty(webContext)) { - UtilAndComs.WEB_CONTEXT = webContext.indexOf("/") > -1 ? webContext - : "/" + webContext; + final String webContext = System.getProperty(SystemPropertyKeyConst.NAMING_WEB_CONTEXT); + TemplateUtils.stringNotEmptyAndThenExecute(webContext, new Runnable() { + @Override + public void run() { + UtilAndComs.WEB_CONTEXT = webContext.indexOf("/") > -1 ? webContext + : "/" + webContext; - isSetting = true; - } - - if (isSetting) { - UtilAndComs.NACOS_URL_BASE = UtilAndComs.WEB_CONTEXT + "/v1/ns"; - UtilAndComs.NACOS_URL_INSTANCE = UtilAndComs.NACOS_URL_BASE + "/instance"; - } + UtilAndComs.NACOS_URL_BASE = UtilAndComs.WEB_CONTEXT + "/v1/ns"; + UtilAndComs.NACOS_URL_INSTANCE = UtilAndComs.NACOS_URL_BASE + "/instance"; + } + }); } @Override diff --git a/client/src/main/java/com/alibaba/nacos/client/utils/TemplateUtils.java b/client/src/main/java/com/alibaba/nacos/client/utils/TemplateUtils.java index 890fd5d66..7e2384473 100644 --- a/client/src/main/java/com/alibaba/nacos/client/utils/TemplateUtils.java +++ b/client/src/main/java/com/alibaba/nacos/client/utils/TemplateUtils.java @@ -23,6 +23,18 @@ import java.util.concurrent.Callable; */ public class TemplateUtils { + public static final void stringNotEmptyAndThenExecute(String source, Runnable runnable) { + + if (StringUtils.isNotEmpty(source)) { + + try { + runnable.run(); + } catch (Exception e) { + LogUtils.NAMING_LOGGER.error("string empty and then execute cause an exception.", e); + } + } + } + public static final String stringEmptyAndThenExecute(String source, Callable callable) { if (StringUtils.isEmpty(source)) {