Skip to content

Commit

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

* nacos-config module replace http client

* replace http client

* Remove redundant package import

* add license

* Delete JOSNUtils and use JacksonUtils instead; modify NacosAsyncRestTemplate exception handling.
  • Loading branch information
Maijh97 authored Aug 6, 2020
1 parent 95c8bf2 commit a64a356
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ public NacosAsyncRestTemplate(Logger logger, AsyncHttpClientRequest clientReques
* @param header http header param
* @param query http query param
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
* @throws Exception ex
*/
public <T> void get(String url, Header header, Query query, Type responseType, Callback<T> callback)
throws Exception {
public <T> void get(String url, Header header, Query query, Type responseType, Callback<T> callback) {
execute(url, HttpMethod.GET, new RequestHttpEntity(header, query), responseType, callback);
}

Expand All @@ -81,10 +79,9 @@ public <T> void get(String url, Header header, Query query, Type responseType, C
* @param paramValues paramValues
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
* @throws Exception ex
*/
public <T> void get(String url, Header header, Map<String, String> paramValues, Type responseType,
Callback<T> callback) throws Exception {
Callback<T> callback) {
execute(url, HttpMethod.GET, new RequestHttpEntity(header, Query.newInstance().initParams(paramValues)),
responseType, callback);
}
Expand All @@ -104,10 +101,9 @@ public <T> void get(String url, Header header, Map<String, String> paramValues,
* @param body get with body
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
* @throws Exception ex
*/
public <T> void getLarge(String url, Header header, Query query, Object body, Type responseType,
Callback<T> callback) throws Exception {
Callback<T> callback) {
execute(url, HttpMethod.GET_LARGE, new RequestHttpEntity(header, query, body), responseType, callback);
}

Expand All @@ -124,10 +120,8 @@ public <T> void getLarge(String url, Header header, Query query, Object body, Ty
* @param query http query param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
* @throws Exception ex
*/
public <T> void delete(String url, Header header, Query query, Type responseType, Callback<T> callback)
throws Exception {
public <T> void delete(String url, Header header, Query query, Type responseType, Callback<T> callback) {
execute(url, HttpMethod.DELETE, new RequestHttpEntity(header, query), responseType, callback);
}

Expand All @@ -147,10 +141,8 @@ public <T> void delete(String url, Header header, Query query, Type responseType
* @param body http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
* @throws Exception ex
*/
public <T> void put(String url, Header header, Query query, Object body, Type responseType, Callback<T> callback)
throws Exception {
public <T> void put(String url, Header header, Query query, Object body, Type responseType, Callback<T> callback) {
execute(url, HttpMethod.PUT, new RequestHttpEntity(header, query, body), responseType, callback);
}

Expand All @@ -171,10 +163,9 @@ public <T> void put(String url, Header header, Query query, Object body, Type re
* @param body http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
* @throws Exception ex
*/
public <T> void putJson(String url, Header header, Map<String, String> paramValues, String body, Type responseType,
Callback<T> callback) throws Exception {
Callback<T> callback) {
execute(url, HttpMethod.PUT, new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON),
Query.newInstance().initParams(paramValues), body), responseType, callback);
}
Expand All @@ -196,10 +187,9 @@ public <T> void putJson(String url, Header header, Map<String, String> paramValu
* @param bodyValues http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
* @throws Exception ex
*/
public <T> void putForm(String url, Header header, Query query, Map<String, String> bodyValues, Type responseType,
Callback<T> callback) throws Exception {
Callback<T> callback) {
execute(url, HttpMethod.PUT,
new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues),
responseType, callback);
Expand All @@ -222,10 +212,9 @@ public <T> void putForm(String url, Header header, Query query, Map<String, Stri
* @param bodyValues http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
* @throws Exception ex
*/
public <T> void putForm(String url, Header header, Map<String, String> paramValues, Map<String, String> bodyValues,
Type responseType, Callback<T> callback) throws Exception {
Type responseType, Callback<T> callback) {
execute(url, HttpMethod.PUT, new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_FORM_URLENCODED),
Query.newInstance().initParams(paramValues), bodyValues), responseType, callback);
}
Expand All @@ -246,10 +235,8 @@ public <T> void putForm(String url, Header header, Map<String, String> paramValu
* @param body http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
* @throws Exception ex
*/
public <T> void post(String url, Header header, Query query, Object body, Type responseType, Callback<T> callback)
throws Exception {
public <T> void post(String url, Header header, Query query, Object body, Type responseType, Callback<T> callback) {
execute(url, HttpMethod.POST, new RequestHttpEntity(header, query, body), responseType, callback);
}

Expand All @@ -270,10 +257,9 @@ public <T> void post(String url, Header header, Query query, Object body, Type r
* @param body http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
* @throws Exception ex
*/
public <T> void postJson(String url, Header header, Map<String, String> paramValues, String body, Type responseType,
Callback<T> callback) throws Exception {
Callback<T> callback) {
execute(url, HttpMethod.POST, new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON),
Query.newInstance().initParams(paramValues), body), responseType, callback);
}
Expand All @@ -295,10 +281,9 @@ public <T> void postJson(String url, Header header, Map<String, String> paramVal
* @param bodyValues http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
* @throws Exception ex
*/
public <T> void postForm(String url, Header header, Query query, Map<String, String> bodyValues, Type responseType,
Callback<T> callback) throws Exception {
Callback<T> callback) {
execute(url, HttpMethod.POST,
new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues),
responseType, callback);
Expand All @@ -321,10 +306,9 @@ public <T> void postForm(String url, Header header, Query query, Map<String, Str
* @param bodyValues http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
* @throws Exception ex
*/
public <T> void postForm(String url, Header header, Map<String, String> paramValues, Map<String, String> bodyValues,
Type responseType, Callback<T> callback) throws Exception {
Type responseType, Callback<T> callback) {
execute(url, HttpMethod.POST,
new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_FORM_URLENCODED),
Query.newInstance().initParams(paramValues), bodyValues), responseType, callback);
Expand All @@ -333,13 +317,18 @@ public <T> void postForm(String url, Header header, Map<String, String> paramVal

@SuppressWarnings("unchecked")
private <T> void execute(String url, String httpMethod, RequestHttpEntity requestEntity, Type type,
Callback<T> callback) throws Exception {
URI uri = HttpUtils.buildUri(url, requestEntity.getQuery());
if (logger.isDebugEnabled()) {
logger.debug("HTTP method: {}, url: {}, body: {}", httpMethod, uri, requestEntity.getBody());
Callback<T> callback) {
try {
URI uri = HttpUtils.buildUri(url, requestEntity.getQuery());
if (logger.isDebugEnabled()) {
logger.debug("HTTP method: {}, url: {}, body: {}", httpMethod, uri, requestEntity.getBody());
}
ResponseHandler<T> responseHandler = super.selectResponseHandler(type);
clientRequest.execute(uri, httpMethod, requestEntity, responseHandler, callback);
} catch (Exception e) {
// When an exception occurs, use Callback to pass it instead of throw it directly.
callback.onError(e);
}
ResponseHandler<T> responseHandler = super.selectResponseHandler(type);
clientRequest.execute(uri, httpMethod, requestEntity, responseHandler, callback);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.alibaba.nacos.config.server.service;

import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.config.server.model.AclInfo;
import com.alibaba.nacos.config.server.utils.JSONUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -76,7 +76,7 @@ public static void load(String content) {
}
DEFAULT_LOG.warn("[clientIpWhiteList] {}", content);
try {
AclInfo acl = (AclInfo) JSONUtils.deserializeObject(content, AclInfo.class);
AclInfo acl = JacksonUtils.toObj(content, AclInfo.class);
isOpen = acl.getIsOpen();
CLIENT_IP_WHITELIST.set(acl.getIps());
} catch (Exception ioe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@

package com.alibaba.nacos.config.server.service;

import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.model.SampleResult;
import com.alibaba.nacos.config.server.service.notify.NotifyService;
import com.alibaba.nacos.config.server.utils.ConfigExecutor;
import com.alibaba.nacos.config.server.utils.JSONUtils;
import com.alibaba.nacos.config.server.utils.LogUtil;
import com.alibaba.nacos.core.cluster.Member;
import com.alibaba.nacos.core.cluster.ServerMemberManager;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -178,19 +177,15 @@ public SampleResult call() throws Exception {
}

String urlAll = getUrl(ip, url) + "?" + paramUrl;
com.alibaba.nacos.config.server.service.notify.NotifyService.HttpResult result = NotifyService
RestResult<String> result = NotifyService
.invokeURL(urlAll, null, Constants.ENCODE);

// Http code 200
if (result.code == HttpURLConnection.HTTP_OK) {
String json = result.content;
SampleResult resultObj = JSONUtils.deserializeObject(json, new TypeReference<SampleResult>() {
});
return resultObj;

if (result.ok()) {
return JacksonUtils.toObj(result.getData(), SampleResult.class);
} else {

LogUtil.DEFAULT_LOG.info("Can not get clientInfo from {} with {}", ip, result.code);
LogUtil.DEFAULT_LOG.info("Can not get clientInfo from {} with {}", ip, result.getData());
return null;
}
} catch (Exception e) {
Expand Down
Loading

0 comments on commit a64a356

Please sign in to comment.