#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); newHeaders.addAll(headers);
} }
HttpResult result = HttpSimpleClient.httpGet( HttpResult result = HttpSimpleClient.httpGet(
getUrl(currentServerAddr, path, isSSL), newHeaders, paramValues, encoding, getUrl(currentServerAddr, path), newHeaders, paramValues, encoding,
readTimeoutMs, isSSL); readTimeoutMs, isSSL);
if (result.code == HttpURLConnection.HTTP_INTERNAL_ERROR if (result.code == HttpURLConnection.HTTP_INTERNAL_ERROR
|| result.code == HttpURLConnection.HTTP_BAD_GATEWAY || result.code == HttpURLConnection.HTTP_BAD_GATEWAY
@ -133,7 +133,7 @@ public class ServerHttpAgent implements HttpAgent {
} }
HttpResult result = HttpSimpleClient.httpPost( HttpResult result = HttpSimpleClient.httpPost(
getUrl(currentServerAddr, path, isSSL), newHeaders, paramValues, encoding, getUrl(currentServerAddr, path), newHeaders, paramValues, encoding,
readTimeoutMs, isSSL); readTimeoutMs, isSSL);
if (result.code == HttpURLConnection.HTTP_INTERNAL_ERROR if (result.code == HttpURLConnection.HTTP_INTERNAL_ERROR
|| result.code == HttpURLConnection.HTTP_BAD_GATEWAY || result.code == HttpURLConnection.HTTP_BAD_GATEWAY
@ -187,7 +187,7 @@ public class ServerHttpAgent implements HttpAgent {
newHeaders.addAll(headers); newHeaders.addAll(headers);
} }
HttpResult result = HttpSimpleClient.httpDelete( HttpResult result = HttpSimpleClient.httpDelete(
getUrl(currentServerAddr, path, isSSL), newHeaders, paramValues, encoding, getUrl(currentServerAddr, path), newHeaders, paramValues, encoding,
readTimeoutMs, isSSL); readTimeoutMs, isSSL);
if (result.code == HttpURLConnection.HTTP_INTERNAL_ERROR if (result.code == HttpURLConnection.HTTP_INTERNAL_ERROR
|| result.code == HttpURLConnection.HTTP_BAD_GATEWAY || result.code == HttpURLConnection.HTTP_BAD_GATEWAY
@ -224,12 +224,8 @@ public class ServerHttpAgent implements HttpAgent {
throw new ConnectException("no available server"); throw new ConnectException("no available server");
} }
private String getUrl(String serverAddr, String relativePath, boolean isSSL) { private String getUrl(String serverAddr, String relativePath) {
String httpPrefix = "http://"; return serverAddr + "/" + serverListMgr.getContentPath() + relativePath;
if (isSSL) {
httpPrefix = "https://";
}
return httpPrefix + serverAddr + "/" + serverListMgr.getContentPath() + relativePath;
} }
public static String getAppname() { 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.EventDispatcher.ServerlistChangeEvent;
import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult;
import com.alibaba.nacos.client.config.utils.IOUtils; import com.alibaba.nacos.client.config.utils.IOUtils;
import com.alibaba.nacos.client.identify.Constants;
import com.alibaba.nacos.client.utils.*; import com.alibaba.nacos.client.utils.*;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -118,12 +119,16 @@ public class ServerListManager {
isFixed = true; isFixed = true;
List<String> serverAddrs = new ArrayList<String>(); List<String> serverAddrs = new ArrayList<String>();
String[] serverAddrsArr = serverAddrsStr.split(","); String[] serverAddrsArr = serverAddrsStr.split(",");
for (String serverAddr : serverAddrsArr) { for (String serverAddr: serverAddrsArr) {
String[] serverAddrArr = serverAddr.split(":"); if (serverAddr.startsWith(Constants.HTTPS) || serverAddr.startsWith(Constants.HTTP)) {
if (serverAddrArr.length == 1) {
serverAddrs.add(serverAddrArr[0] + ":" + ParamUtil.getDefaultServerPort());
} else {
serverAddrs.add(serverAddr); 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; serverUrls = serverAddrs;

View File

@ -49,4 +49,8 @@ public class Constants {
public static final String NO_APP_NAME = ""; 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.ExpressionSelector;
import com.alibaba.nacos.api.selector.SelectorType; import com.alibaba.nacos.api.selector.SelectorType;
import com.alibaba.nacos.client.config.impl.SpasAdapter; 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.monitor.MetricsMonitor;
import com.alibaba.nacos.client.naming.beat.BeatInfo; import com.alibaba.nacos.client.naming.beat.BeatInfo;
import com.alibaba.nacos.client.naming.utils.*; import com.alibaba.nacos.client.naming.utils.*;
@ -397,13 +398,15 @@ public class NamingProxy {
List<String> headers = builderHeaders(); List<String> headers = builderHeaders();
String url; String url;
if (curServer.startsWith(Constants.HTTPS) || curServer.startsWith(Constants.HTTP)) {
if (!curServer.contains(UtilAndComs.SERVER_ADDR_IP_SPLITER)) { url = curServer + api;
curServer = curServer + UtilAndComs.SERVER_ADDR_IP_SPLITER + serverPort; } 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); HttpClient.HttpResult result = HttpClient.request(url, headers, params, UtilAndComs.ENCODING, method);
end = System.currentTimeMillis(); end = System.currentTimeMillis();
@ -418,8 +421,8 @@ public class NamingProxy {
return StringUtils.EMPTY; return StringUtils.EMPTY;
} }
throw new NacosException(NacosException.SERVER_ERROR, "failed to req API:" + HttpClient.getPrefix() + curServer throw new NacosException(NacosException.SERVER_ERROR, "failed to req API:"
+ api + ". code:" + curServer + api + ". code:"
+ result.code + " msg: " + result.content); + result.code + " msg: " + result.content);
} }
@ -556,6 +559,6 @@ public class NamingProxy {
this.serverPort = Integer.parseInt(sp); this.serverPort = Integer.parseInt(sp);
} }
} }
} }