#1344 server-addr 支持 http 字符串

This commit is contained in:
rushsky518 2019-06-24 10:41:37 +08:00
parent 0f72a349c5
commit 6d5191f11a
4 changed files with 30 additions and 22 deletions

View File

@ -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() {

View File

@ -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<String> serverAddrs = new ArrayList<String>();
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;

View File

@ -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://";
}

View File

@ -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<String> 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);
}