diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java index aa2bf35ef..6e1719103 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java @@ -78,7 +78,7 @@ public class ServerHttpAgent implements HttpAgent { newHeaders.addAll(headers); } HttpResult result = HttpSimpleClient.httpGet( - getUrl(currentServerAddr, path, isSSL), newHeaders, paramValues, encoding, + getUrl(currentServerAddr, path), newHeaders, paramValues, encoding, readTimeoutMs, isSSL); if (result.code == HttpURLConnection.HTTP_INTERNAL_ERROR || result.code == HttpURLConnection.HTTP_BAD_GATEWAY @@ -133,7 +133,7 @@ public class ServerHttpAgent implements HttpAgent { } HttpResult result = HttpSimpleClient.httpPost( - getUrl(currentServerAddr, path, isSSL), newHeaders, paramValues, encoding, + getUrl(currentServerAddr, path), newHeaders, paramValues, encoding, readTimeoutMs, isSSL); if (result.code == HttpURLConnection.HTTP_INTERNAL_ERROR || result.code == HttpURLConnection.HTTP_BAD_GATEWAY @@ -187,7 +187,7 @@ public class ServerHttpAgent implements HttpAgent { newHeaders.addAll(headers); } HttpResult result = HttpSimpleClient.httpDelete( - getUrl(currentServerAddr, path, isSSL), newHeaders, paramValues, encoding, + getUrl(currentServerAddr, path), newHeaders, paramValues, encoding, readTimeoutMs, isSSL); if (result.code == HttpURLConnection.HTTP_INTERNAL_ERROR || result.code == HttpURLConnection.HTTP_BAD_GATEWAY @@ -224,12 +224,8 @@ public class ServerHttpAgent implements HttpAgent { throw new ConnectException("no available server"); } - private String getUrl(String serverAddr, String relativePath, boolean isSSL) { - String httpPrefix = "http://"; - if (isSSL) { - httpPrefix = "https://"; - } - return httpPrefix + serverAddr + "/" + serverListMgr.getContentPath() + relativePath; + private String getUrl(String serverAddr, String relativePath) { + return serverAddr + "/" + serverListMgr.getContentPath() + relativePath; } public static String getAppname() { 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 aa3a2fab1..863d28d9b 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 @@ -21,6 +21,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.identify.Constants; import com.alibaba.nacos.client.utils.*; import org.slf4j.Logger; @@ -118,12 +119,16 @@ public class ServerListManager { isFixed = true; List serverAddrs = new ArrayList(); String[] serverAddrsArr = serverAddrsStr.split(","); - for (String serverAddr : serverAddrsArr) { - String[] serverAddrArr = serverAddr.split(":"); - if (serverAddrArr.length == 1) { - serverAddrs.add(serverAddrArr[0] + ":" + ParamUtil.getDefaultServerPort()); - } else { + for (String serverAddr: serverAddrsArr) { + if (serverAddr.startsWith(Constants.HTTPS) || serverAddr.startsWith(Constants.HTTP)) { serverAddrs.add(serverAddr); + } else { + String[] serverAddrArr = serverAddr.split(":"); + if (serverAddrArr.length == 1) { + serverAddrs.add(Constants.HTTP + serverAddrArr[0] + ":" + ParamUtil.getDefaultServerPort()); + } else { + serverAddrs.add(Constants.HTTP + serverAddr); + } } } serverUrls = serverAddrs; diff --git a/client/src/main/java/com/alibaba/nacos/client/identify/Constants.java b/client/src/main/java/com/alibaba/nacos/client/identify/Constants.java index a94110720..7792acad1 100644 --- a/client/src/main/java/com/alibaba/nacos/client/identify/Constants.java +++ b/client/src/main/java/com/alibaba/nacos/client/identify/Constants.java @@ -49,4 +49,8 @@ public class Constants { public static final String NO_APP_NAME = ""; + public static final String HTTP = "http://"; + + public static final String HTTPS = "https://"; + } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java index 285665743..78ad806ea 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java @@ -29,6 +29,7 @@ import com.alibaba.nacos.api.selector.AbstractSelector; import com.alibaba.nacos.api.selector.ExpressionSelector; import com.alibaba.nacos.api.selector.SelectorType; import com.alibaba.nacos.client.config.impl.SpasAdapter; +import com.alibaba.nacos.client.identify.Constants; import com.alibaba.nacos.client.monitor.MetricsMonitor; import com.alibaba.nacos.client.naming.beat.BeatInfo; import com.alibaba.nacos.client.naming.utils.*; @@ -397,13 +398,15 @@ public class NamingProxy { List headers = builderHeaders(); String url; - - if (!curServer.contains(UtilAndComs.SERVER_ADDR_IP_SPLITER)) { - curServer = curServer + UtilAndComs.SERVER_ADDR_IP_SPLITER + serverPort; + if (curServer.startsWith(Constants.HTTPS) || curServer.startsWith(Constants.HTTP)) { + url = curServer + api; + } else { + if (!curServer.contains(UtilAndComs.SERVER_ADDR_IP_SPLITER)) { + curServer = curServer + UtilAndComs.SERVER_ADDR_IP_SPLITER + serverPort; + } + url = HttpClient.getPrefix() + curServer + api; } - url = HttpClient.getPrefix() + curServer + api; - HttpClient.HttpResult result = HttpClient.request(url, headers, params, UtilAndComs.ENCODING, method); end = System.currentTimeMillis(); @@ -418,8 +421,8 @@ public class NamingProxy { return StringUtils.EMPTY; } - throw new NacosException(NacosException.SERVER_ERROR, "failed to req API:" + HttpClient.getPrefix() + curServer - + api + ". code:" + throw new NacosException(NacosException.SERVER_ERROR, "failed to req API:" + + curServer + api + ". code:" + result.code + " msg: " + result.content); } @@ -556,6 +559,6 @@ public class NamingProxy { this.serverPort = Integer.parseInt(sp); } } - + }