Skip to content

Commit

Permalink
[ISSUE alibaba#3140]nacos-client module replace http client (alibaba#…
Browse files Browse the repository at this point in the history
…3142)

* nacos-client http client replace

* Remove some explain

* Remove some explain

* Adjust some code styles and fix some misspelled method names

* Fix code style issues

* fix some misspelled method names

* Fix code style issues
  • Loading branch information
Maijh97 authored Jun 22, 2020
1 parent 1dc29f2 commit 6ce8d8c
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.alibaba.nacos.client.naming.net;

import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.utils.HttpMethod;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.StringUtils;
Expand All @@ -35,7 +36,9 @@

/**
* @author nkorange
* @deprecated Use NacosRestTemplate{@link NacosRestTemplate} unified http client
*/
@Deprecated
public class HttpClient {

public static final int READ_TIME_OUT_MILLIS = Integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,42 @@

package com.alibaba.nacos.client.naming.net;

import com.alibaba.nacos.common.http.*;
import com.alibaba.nacos.common.http.AbstractHttpClientFactory;
import com.alibaba.nacos.common.http.HttpClientBeanHolder;
import com.alibaba.nacos.common.http.HttpClientConfig;
import com.alibaba.nacos.common.http.HttpClientFactory;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;

/**
* http Manager
*
* @author mai.jh
* @date 2020/6/14
*/
public class NamingHttpClientManager {

private static final int READ_TIME_OUT_MILLIS = Integer
.getInteger("com.alibaba.nacos.client.naming.rtimeout", 50000);
private static final int CON_TIME_OUT_MILLIS = Integer
.getInteger("com.alibaba.nacos.client.naming.ctimeout", 3000);
private static final boolean ENABLE_HTTPS = Boolean
.getBoolean("com.alibaba.nacos.client.naming.tls.enable");
private static final int MAX_REDIRECTS = 5;

private static final HttpClientFactory HTTP_CLIENT_FACTORY = new NamingHttpClientFactory();

public static String getPrefix() {
if (ENABLE_HTTPS) {
return "https://";
}
return "http://";
}

public static NacosRestTemplate getNacosRestTemplate() {
return HttpClientBeanHolder.getNacosRestTemplate(HTTP_CLIENT_FACTORY);
}

private static class NamingHttpClientFactory extends AbstractHttpClientFactory {

@Override
protected HttpClientConfig buildHttpClientConfig() {
return HttpClientConfig.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,27 @@
import com.alibaba.nacos.client.utils.AppNameUtils;
import com.alibaba.nacos.client.utils.TemplateUtils;
import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.http.HttpRestResult;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.Query;
import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.utils.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import org.apache.http.HttpStatus;

import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Map;
import java.util.HashMap;
import java.util.Collections;
import java.util.Random;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
Expand All @@ -67,6 +72,8 @@
*/
public class NamingProxy implements Closeable {

private NacosRestTemplate nacosRestTemplate = NamingHttpClientManager.getNacosRestTemplate();

private static final int DEFAULT_SERVER_PORT = 8848;

private int serverPort = DEFAULT_SERVER_PORT;
Expand Down Expand Up @@ -144,15 +151,14 @@ public List<String> getServerListFromEndpoint() {

try {
String urlString = "http://" + endpoint + "/nacos/serverlist";
List<String> headers = builderHeaders();

HttpClient.HttpResult result = HttpClient.httpGet(urlString, headers, null, UtilAndComs.ENCODING);
if (HttpURLConnection.HTTP_OK != result.code) {
Header header = builderHeader();
HttpRestResult<String> restResult = nacosRestTemplate.get(urlString, header, Query.EMPTY, String.class);
if (!restResult.ok()) {
throw new IOException("Error while requesting: " + urlString + "'. Server returned: "
+ result.code);
+ restResult.getCode());
}

String content = result.content;
String content = restResult.getData();
List<String> list = new ArrayList<String>();
for (String line : IoUtils.readLines(new StringReader(content))) {
if (!line.trim().isEmpty()) {
Expand Down Expand Up @@ -333,10 +339,10 @@ public JsonNode sendBeat(BeatInfo beatInfo, boolean lightBeatEnabled) throws Nac
NAMING_LOGGER.debug("[BEAT] {} sending beat to server: {}", namespaceId, beatInfo.toString());
}
Map<String, String> params = new HashMap<String, String>(8);
String body = StringUtils.EMPTY;
Map<String, String> bodyMap = new HashMap<String, String>(2);
if (!lightBeatEnabled) {
try {
body = "beat=" + URLEncoder.encode(JacksonUtils.toJson(beatInfo), "UTF-8");
bodyMap.put("beat", URLEncoder.encode(JacksonUtils.toJson(beatInfo), "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new NacosException(NacosException.SERVER_ERROR, "encode beatInfo error", e);
}
Expand All @@ -346,7 +352,7 @@ public JsonNode sendBeat(BeatInfo beatInfo, boolean lightBeatEnabled) throws Nac
params.put(CommonParams.CLUSTER_NAME, beatInfo.getCluster());
params.put("ip", beatInfo.getIp());
params.put("port", String.valueOf(beatInfo.getPort()));
String result = reqAPI(UtilAndComs.NACOS_URL_BASE + "/instance/beat", params, body, HttpMethod.PUT);
String result = reqAPI(UtilAndComs.NACOS_URL_BASE + "/instance/beat", params, bodyMap, HttpMethod.PUT);
return JacksonUtils.toObj(result);
}

Expand Down Expand Up @@ -399,10 +405,10 @@ public ListView<String> getServiceList(int pageNo, int pageSize, String groupNam
}

public String reqAPI(String api, Map<String, String> params, String method) throws NacosException {
return reqAPI(api, params, StringUtils.EMPTY, method);
return reqAPI(api, params, Collections.EMPTY_MAP, method);
}

public String reqAPI(String api, Map<String, String> params, String body, String method) throws NacosException {
public String reqAPI(String api, Map<String, String> params, Map<String, String> body, String method) throws NacosException {
return reqAPI(api, params, body, getServerList(), method);
}

Expand All @@ -414,16 +420,16 @@ private List<String> getServerList() {
return snapshot;
}

public String callServer(String api, Map<String, String> params, String body, String curServer) throws NacosException {
public String callServer(String api, Map<String, String> params, Map<String, String> body, String curServer) throws NacosException {
return callServer(api, params, body, curServer, HttpMethod.GET);
}

public String callServer(String api, Map<String, String> params, String body, String curServer, String method)
public String callServer(String api, Map<String, String> params, Map<String, String> body, String curServer, String method)
throws NacosException {
long start = System.currentTimeMillis();
long end = 0;
injectSecurityInfo(params);
List<String> headers = builderHeaders();
Header header = builderHeader();

String url;
if (curServer.startsWith(UtilAndComs.HTTPS) || curServer.startsWith(UtilAndComs.HTTP)) {
Expand All @@ -432,27 +438,30 @@ public String callServer(String api, Map<String, String> params, String body, St
if (!curServer.contains(UtilAndComs.SERVER_ADDR_IP_SPLITER)) {
curServer = curServer + UtilAndComs.SERVER_ADDR_IP_SPLITER + serverPort;
}
url = HttpClient.getPrefix() + curServer + api;
url = NamingHttpClientManager.getPrefix() + curServer + api;
}

HttpClient.HttpResult result = HttpClient.request(url, headers, params, body, UtilAndComs.ENCODING, method);
end = System.currentTimeMillis();

MetricsMonitor.getNamingRequestMonitor(method, url, String.valueOf(result.code))
.observe(end - start);
try {
HttpRestResult<String> restResult = nacosRestTemplate.exchangeForm(url, header, params, body, method, String.class);
end = System.currentTimeMillis();

if (HttpURLConnection.HTTP_OK == result.code) {
return result.content;
}
MetricsMonitor.getNamingRequestMonitor(method, url, String.valueOf(restResult.getCode()))
.observe(end - start);

if (HttpURLConnection.HTTP_NOT_MODIFIED == result.code) {
return StringUtils.EMPTY;
if (restResult.ok()) {
return restResult.getData();
}
if (HttpStatus.SC_NOT_MODIFIED == restResult.getCode()) {
return StringUtils.EMPTY;
}
throw new NacosException(restResult.getCode(), restResult.getData());
} catch (Exception e) {
NAMING_LOGGER.error("[NA] failed to request", e);
throw new NacosException(NacosException.SERVER_ERROR, e);
}

throw new NacosException(result.code, result.content);
}

public String reqAPI(String api, Map<String, String> params, String body, List<String> servers, String method) throws NacosException {
public String reqAPI(String api, Map<String, String> params, Map<String, String> body, List<String> servers, String method) throws NacosException {

params.put(CommonParams.NAMESPACE_ID, getNamespaceId());

Expand Down Expand Up @@ -526,14 +535,15 @@ private void injectSecurityInfo(Map<String, String> params) {
}
}

public List<String> builderHeaders() {
List<String> headers = Arrays.asList(
HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.version,
HttpHeaderConsts.USER_AGENT_HEADER, UtilAndComs.VERSION,
"Accept-Encoding", "gzip,deflate,sdch",
"Connection", "Keep-Alive",
"RequestId", UuidUtils.generateUuid(), "Request-Module", "Naming");
return headers;
public Header builderHeader() {
Header header = Header.newInstance();
header.addParam(HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.version);
header.addParam(HttpHeaderConsts.USER_AGENT_HEADER, UtilAndComs.VERSION);
header.addParam(HttpHeaderConsts.ACCEPT_ENCODING, "gzip,deflate,sdch");
header.addParam(HttpHeaderConsts.CONNECTION, "Keep-Alive");
header.addParam(HttpHeaderConsts.REQUEST_ID, UuidUtils.generateUuid());
header.addParam(HttpHeaderConsts.REQUEST_MODULE, "Naming");
return header;
}

private static String getSignData(String serviceName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.client.naming.net.HttpClient;
import com.alibaba.nacos.common.utils.HttpMethod;
import com.alibaba.nacos.client.naming.net.NamingHttpClientManager;
import com.alibaba.nacos.common.http.HttpRestResult;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.fasterxml.jackson.databind.JsonNode;

import org.apache.commons.codec.Charsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.HttpURLConnection;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -43,6 +46,8 @@ public class SecurityProxy {

private static final String LOGIN_URL = "/v1/auth/users/login";

private NacosRestTemplate nacosRestTemplate = NamingHttpClientManager.getNacosRestTemplate();

private String contextPath;

/**
Expand Down Expand Up @@ -110,28 +115,31 @@ public boolean login(String server) {

if (StringUtils.isNotBlank(username)) {
Map<String, String> params = new HashMap<String, String>(2);
Map<String, String> bodyMap = new HashMap<>(2);
params.put("username", username);
String body = "password=" + password;
bodyMap.put("password", password);
String url = "http://" + server + contextPath + LOGIN_URL;

if (server.contains(Constants.HTTP_PREFIX)) {
url = server + contextPath + LOGIN_URL;
}

HttpClient.HttpResult result = HttpClient.request(url, new ArrayList<String>(2),
params, body, Charsets.UTF_8.name(), HttpMethod.POST);

if (result.code != HttpURLConnection.HTTP_OK) {
SECURITY_LOGGER.error("login failed: {}", JacksonUtils.toJson(result));
try {
HttpRestResult<String> restResult = nacosRestTemplate.postForm(url, Header.EMPTY, params, bodyMap, String.class);
if (!restResult.ok()) {
SECURITY_LOGGER.error("login failed: {}", JacksonUtils.toJson(restResult));
return false;
}
JsonNode obj = JacksonUtils.toObj(restResult.getData());
if (obj.has(Constants.ACCESS_TOKEN)) {
accessToken = obj.get(Constants.ACCESS_TOKEN).asText();
tokenTtl = obj.get(Constants.TOKEN_TTL).asInt();
tokenRefreshWindow = tokenTtl / 10;
}
} catch (Exception e) {
SECURITY_LOGGER.error("[SecurityProxy] login http request failed" +
" url: {}, params: {}, bodyMap: {}, errorMsg: {}", url, params, bodyMap, e.getMessage());
return false;
}

JsonNode obj = JacksonUtils.toObj(result.content);
if (obj.has(Constants.ACCESS_TOKEN)) {
accessToken = obj.get(Constants.ACCESS_TOKEN).asText();
tokenTtl = obj.get(Constants.TOKEN_TTL).asInt();
tokenRefreshWindow = tokenTtl / 10;
}
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public void initEntity(Object body, String mediaType) throws Exception {
* @throws Exception exception
*/
public void initFromEntity(Map<String, String> body, String charset) throws Exception {
if (body.isEmpty()) {
if (body == null || body.isEmpty()) {
return;
}
List<NameValuePair> params = new ArrayList<NameValuePair>(body.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,9 @@ public final class HttpClientBeanHolder {

private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientManager.class);

private static final int TIMEOUT = Integer.getInteger("nacos.http.timeout", 5000);

private static final HttpClientConfig HTTP_CLIENT_CONFIG = HttpClientConfig.builder().setConTimeOutMillis(TIMEOUT)
.setReadTimeOutMillis(TIMEOUT >> 1).build();

private static final Map<String, NacosRestTemplate> SINGLETON_REST = new HashMap<String, NacosRestTemplate>(10);

private static final Map<String, NacosAsyncRestTemplate> SINGLETON_ASYNC_REST = new HashMap<String, NacosAsyncRestTemplate>(
10);
private static final Map<String, NacosAsyncRestTemplate> SINGLETON_ASYNC_REST = new HashMap<String, NacosAsyncRestTemplate>(10);

private static final AtomicBoolean ALREADY_SHUTDOWN = new AtomicBoolean(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
* Represents a client-side HTTP response.
*
* @author mai.jh
* @date 2020/5/23
*/
public interface HttpClientResponse extends Closeable {

Expand Down
Loading

0 comments on commit 6ce8d8c

Please sign in to comment.