Skip to content

Commit

Permalink
[ISSUE #3224]nacos-client module http client replace (#3348)
Browse files Browse the repository at this point in the history
* nacos-client module http client replace

* fix code style problem

* add HashMap initialCapacity

* fix code style problem

* Modify the header object, keep the original response header to avoid modifying the original logic code

* fix code style problem

* naming http client request exception messages output change

* Merge code
  • Loading branch information
Maijh97 authored Jul 18, 2020
1 parent 9f12f76 commit 63a4e30
Show file tree
Hide file tree
Showing 16 changed files with 730 additions and 511 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,20 @@
import com.alibaba.nacos.client.config.http.MetricsHttpAgent;
import com.alibaba.nacos.client.config.http.ServerHttpAgent;
import com.alibaba.nacos.client.config.impl.ClientWorker;
import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult;
import com.alibaba.nacos.client.config.impl.LocalConfigInfoProcessor;
import com.alibaba.nacos.client.config.utils.ContentUtils;
import com.alibaba.nacos.client.config.utils.ParamUtils;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.ValidatorUtils;
import com.alibaba.nacos.common.http.HttpRestResult;
import com.alibaba.nacos.common.utils.StringUtils;
import org.slf4j.Logger;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
Expand Down Expand Up @@ -179,37 +178,34 @@ private boolean removeConfigInner(String tenant, String dataId, String group, St
group = null2defaultGroup(group);
ParamUtils.checkKeyParam(dataId, group);
String url = Constants.CONFIG_CONTROLLER_PATH;
List<String> params = new ArrayList<String>();
params.add("dataId");
params.add(dataId);
params.add("group");
params.add(group);
Map<String, String> params = new HashMap<String, String>(4);
params.put("dataId", dataId);
params.put("group", group);

if (StringUtils.isNotEmpty(tenant)) {
params.add("tenant");
params.add(tenant);
params.put("tenant", tenant);
}
if (StringUtils.isNotEmpty(tag)) {
params.add("tag");
params.add(tag);
params.put("tag", tag);
}
HttpResult result = null;
HttpRestResult<String> result = null;
try {
result = agent.httpDelete(url, null, params, encode, POST_TIMEOUT);
} catch (IOException ioe) {
LOGGER.warn("[remove] error, " + dataId + ", " + group + ", " + tenant + ", msg: " + ioe.toString());
} catch (Exception ex) {
LOGGER.warn("[remove] error, " + dataId + ", " + group + ", " + tenant + ", msg: " + ex.toString());
return false;
}

if (HttpURLConnection.HTTP_OK == result.code) {
if (result.ok()) {
LOGGER.info("[{}] [remove] ok, dataId={}, group={}, tenant={}", agent.getName(), dataId, group, tenant);
return true;
} else if (HttpURLConnection.HTTP_FORBIDDEN == result.code) {
} else if (HttpURLConnection.HTTP_FORBIDDEN == result.getCode()) {
LOGGER.warn("[{}] [remove] error, dataId={}, group={}, tenant={}, code={}, msg={}", agent.getName(), dataId,
group, tenant, result.code, result.content);
throw new NacosException(result.code, result.content);
group, tenant, result.getCode(), result.getMessage());
throw new NacosException(result.getCode(), result.getMessage());
} else {
LOGGER.warn("[{}] [remove] error, dataId={}, group={}, tenant={}, code={}, msg={}", agent.getName(), dataId,
group, tenant, result.code, result.content);
group, tenant, result.getCode(), result.getMessage());
return false;
}
}
Expand All @@ -228,52 +224,44 @@ private boolean publishConfigInner(String tenant, String dataId, String group, S
content = cr.getContent();

String url = Constants.CONFIG_CONTROLLER_PATH;
List<String> params = new ArrayList<String>();
params.add("dataId");
params.add(dataId);
params.add("group");
params.add(group);
params.add("content");
params.add(content);
Map<String, String> params = new HashMap<String, String>(6);
params.put("dataId", dataId);
params.put("group", group);
params.put("content", content);
if (StringUtils.isNotEmpty(tenant)) {
params.add("tenant");
params.add(tenant);
params.put("tenant", tenant);
}
if (StringUtils.isNotEmpty(appName)) {
params.add("appName");
params.add(appName);
params.put("appName", appName);
}
if (StringUtils.isNotEmpty(tag)) {
params.add("tag");
params.add(tag);
params.put("tag", tag);
}

List<String> headers = new ArrayList<String>();
Map<String, String> headers = new HashMap<String, String>(1);
if (StringUtils.isNotEmpty(betaIps)) {
headers.add("betaIps");
headers.add(betaIps);
headers.put("betaIps", betaIps);
}

HttpResult result = null;
HttpRestResult<String> result = null;
try {
result = agent.httpPost(url, headers, params, encode, POST_TIMEOUT);
} catch (IOException ioe) {
} catch (Exception ex) {
LOGGER.warn("[{}] [publish-single] exception, dataId={}, group={}, msg={}", agent.getName(), dataId, group,
ioe.toString());
ex.toString());
return false;
}

if (HttpURLConnection.HTTP_OK == result.code) {
if (result.ok()) {
LOGGER.info("[{}] [publish-single] ok, dataId={}, group={}, tenant={}, config={}", agent.getName(), dataId,
group, tenant, ContentUtils.truncateContent(content));
return true;
} else if (HttpURLConnection.HTTP_FORBIDDEN == result.code) {
} else if (HttpURLConnection.HTTP_FORBIDDEN == result.getCode()) {
LOGGER.warn("[{}] [publish-single] error, dataId={}, group={}, tenant={}, code={}, msg={}", agent.getName(),
dataId, group, tenant, result.code, result.content);
throw new NacosException(result.code, result.content);
dataId, group, tenant, result.getCode(), result.getMessage());
throw new NacosException(result.getCode(), result.getMessage());
} else {
LOGGER.warn("[{}] [publish-single] error, dataId={}, group={}, tenant={}, code={}, msg={}", agent.getName(),
dataId, group, tenant, result.code, result.content);
dataId, group, tenant, result.getCode(), result.getMessage());
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
package com.alibaba.nacos.client.config.http;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult;
import com.alibaba.nacos.common.http.HttpRestResult;
import com.alibaba.nacos.common.lifecycle.Closeable;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* HttpAgent.
Expand All @@ -46,11 +45,11 @@ public interface HttpAgent extends Closeable {
* @param encoding http encode
* @param readTimeoutMs http timeout
* @return HttpResult http response
* @throws IOException If an input or output exception occurred
* @throws Exception If an input or output exception occurred
*/

HttpResult httpGet(String path, List<String> headers, List<String> paramValues, String encoding, long readTimeoutMs)
throws IOException;
HttpRestResult<String> httpGet(String path, Map<String, String> headers, Map<String, String> paramValues,
String encoding, long readTimeoutMs) throws Exception;

/**
* invoke http post method.
Expand All @@ -61,10 +60,10 @@ HttpResult httpGet(String path, List<String> headers, List<String> paramValues,
* @param encoding http encode
* @param readTimeoutMs http timeout
* @return HttpResult http response
* @throws IOException If an input or output exception occurred
* @throws Exception If an input or output exception occurred
*/
HttpResult httpPost(String path, List<String> headers, List<String> paramValues, String encoding,
long readTimeoutMs) throws IOException;
HttpRestResult<String> httpPost(String path, Map<String, String> headers, Map<String, String> paramValues,
String encoding, long readTimeoutMs) throws Exception;

/**
* invoke http delete method.
Expand All @@ -75,10 +74,10 @@ HttpResult httpPost(String path, List<String> headers, List<String> paramValues,
* @param encoding http encode
* @param readTimeoutMs http timeout
* @return HttpResult http response
* @throws IOException If an input or output exception occurred
* @throws Exception If an input or output exception occurred
*/
HttpResult httpDelete(String path, List<String> headers, List<String> paramValues, String encoding,
long readTimeoutMs) throws IOException;
HttpRestResult<String> httpDelete(String path, Map<String, String> headers, Map<String, String> paramValues,
String encoding, long readTimeoutMs) throws Exception;

/**
* get name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
package com.alibaba.nacos.client.config.http;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult;
import com.alibaba.nacos.client.monitor.MetricsMonitor;
import com.alibaba.nacos.common.http.HttpRestResult;
import io.prometheus.client.Histogram;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* MetricsHttpAgent.
Expand All @@ -43,12 +43,12 @@ public void start() throws NacosException {
}

@Override
public HttpResult httpGet(String path, List<String> headers, List<String> paramValues, String encoding,
long readTimeoutMs) throws IOException {
public HttpRestResult<String> httpGet(String path, Map<String, String> headers, Map<String, String> paramValues,
String encode, long readTimeoutMs) throws Exception {
Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("GET", path, "NA");
HttpResult result;
HttpRestResult<String> result;
try {
result = httpAgent.httpGet(path, headers, paramValues, encoding, readTimeoutMs);
result = httpAgent.httpGet(path, headers, paramValues, encode, readTimeoutMs);
} catch (IOException e) {
throw e;
} finally {
Expand All @@ -60,12 +60,12 @@ public HttpResult httpGet(String path, List<String> headers, List<String> paramV
}

@Override
public HttpResult httpPost(String path, List<String> headers, List<String> paramValues, String encoding,
long readTimeoutMs) throws IOException {
public HttpRestResult<String> httpPost(String path, Map<String, String> headers, Map<String, String> paramValues,
String encode, long readTimeoutMs) throws Exception {
Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("POST", path, "NA");
HttpResult result;
HttpRestResult<String> result;
try {
result = httpAgent.httpPost(path, headers, paramValues, encoding, readTimeoutMs);
result = httpAgent.httpPost(path, headers, paramValues, encode, readTimeoutMs);
} catch (IOException e) {
throw e;
} finally {
Expand All @@ -77,12 +77,12 @@ public HttpResult httpPost(String path, List<String> headers, List<String> param
}

@Override
public HttpResult httpDelete(String path, List<String> headers, List<String> paramValues, String encoding,
long readTimeoutMs) throws IOException {
public HttpRestResult<String> httpDelete(String path, Map<String, String> headers, Map<String, String> paramValues,
String encode, long readTimeoutMs) throws Exception {
Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("DELETE", path, "NA");
HttpResult result;
HttpRestResult<String> result;
try {
result = httpAgent.httpDelete(path, headers, paramValues, encoding, readTimeoutMs);
result = httpAgent.httpDelete(path, headers, paramValues, encode, readTimeoutMs);
} catch (IOException e) {

throw e;
Expand Down
Loading

0 comments on commit 63a4e30

Please sign in to comment.