Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE#3311] Add specified Logger when constructing nacos resttemplate #3312

Merged
merged 1 commit into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

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

import com.alibaba.nacos.client.utils.LogUtils;
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;
import org.slf4j.Logger;

/**
* http Manager.
Expand Down Expand Up @@ -58,5 +60,10 @@ protected HttpClientConfig buildHttpClientConfig() {
return HttpClientConfig.builder().setConTimeOutMillis(CON_TIME_OUT_MILLIS)
.setReadTimeOutMillis(READ_TIME_OUT_MILLIS).setMaxRedirects(MAX_REDIRECTS).build();
}

@Override
protected Logger assignLogger() {
return LogUtils.NAMING_LOGGER;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.slf4j.Logger;

/**
* AbstractHttpClientFactory Let the creator only specify the http client config.
Expand All @@ -34,14 +35,14 @@ public abstract class AbstractHttpClientFactory implements HttpClientFactory {
@Override
public final NacosRestTemplate createNacosRestTemplate() {
RequestConfig requestConfig = getRequestConfig();
return new NacosRestTemplate(
return new NacosRestTemplate(assignLogger(),
new DefaultHttpClientRequest(HttpClients.custom().setDefaultRequestConfig(requestConfig).build()));
}

@Override
public final NacosAsyncRestTemplate createNacosAsyncRestTemplate() {
RequestConfig requestConfig = getRequestConfig();
return new NacosAsyncRestTemplate(new DefaultAsyncHttpClientRequest(
return new NacosAsyncRestTemplate(assignLogger(), new DefaultAsyncHttpClientRequest(
HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig).build()));
}

Expand All @@ -58,4 +59,11 @@ private RequestConfig getRequestConfig() {
* @return HttpClientConfig
*/
protected abstract HttpClientConfig buildHttpClientConfig();

/**
* assign Logger.
*
* @return Logger
*/
protected abstract Logger assignLogger();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.alibaba.nacos.common.http;

import org.slf4j.Logger;

/**
* default http client factory.
*
Expand All @@ -25,8 +27,19 @@ public class DefaultHttpClientFactory extends AbstractHttpClientFactory {

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

private final Logger logger;

public DefaultHttpClientFactory(Logger logger) {
this.logger = logger;
}

@Override
protected HttpClientConfig buildHttpClientConfig() {
return HttpClientConfig.builder().setConTimeOutMillis(TIMEOUT).setReadTimeOutMillis(TIMEOUT >> 1).build();
}

@Override
protected Logger assignLogger() {
return logger;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public void run() {
});
}

public static NacosRestTemplate getNacosRestTemplate() {
return getNacosRestTemplate(new DefaultHttpClientFactory());
public static NacosRestTemplate getNacosRestTemplate(Logger logger) {
return getNacosRestTemplate(new DefaultHttpClientFactory(logger));
}

public static NacosRestTemplate getNacosRestTemplate(HttpClientFactory httpClientFactory) {
Expand All @@ -75,8 +75,8 @@ public static NacosRestTemplate getNacosRestTemplate(HttpClientFactory httpClien
return nacosRestTemplate;
}

public static NacosAsyncRestTemplate getNacosAsyncRestTemplate() {
return getNacosAsyncRestTemplate(new DefaultHttpClientFactory());
public static NacosAsyncRestTemplate getNacosAsyncRestTemplate(Logger logger) {
return getNacosAsyncRestTemplate(new DefaultHttpClientFactory(logger));
}

public static NacosAsyncRestTemplate getNacosAsyncRestTemplate(HttpClientFactory httpClientFactory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alibaba.nacos.common.constant.ResponseHandlerType;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.fasterxml.jackson.databind.JavaType;
import org.slf4j.Logger;

import java.lang.reflect.Type;
import java.util.HashMap;
Expand All @@ -29,11 +30,19 @@
*
* @author mai.jh
*/
@SuppressWarnings("all")
public abstract class AbstractNacosRestTemplate {

private final Map<String, ResponseHandler> responseHandlerMap = new HashMap<String, ResponseHandler>();

public AbstractNacosRestTemplate() {
protected final Logger logger;

public AbstractNacosRestTemplate(Logger logger) {
this.logger = logger;
initDefaultResponseHandler();
}

private void initDefaultResponseHandler() {
// init response handler
responseHandlerMap.put(ResponseHandlerType.STRING_TYPE, new StringResponseHandler());
responseHandlerMap.put(ResponseHandlerType.RESTRESULT_TYPE, new RestResultResponseHandler());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URI;
Expand All @@ -41,8 +39,6 @@
@SuppressWarnings({"unchecked", "resource"})
public class DefaultHttpClientRequest implements HttpClientRequest {

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

private final CloseableHttpClient client;

public DefaultHttpClientRequest(CloseableHttpClient client) {
Expand All @@ -54,9 +50,6 @@ public HttpClientResponse execute(URI uri, String httpMethod, RequestHttpEntity
throws Exception {
HttpRequestBase request = build(uri, httpMethod, requestHttpEntity);
CloseableHttpResponse response = client.execute(request);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Request from server: " + request.getURI().toString());
}
return new DefaultClientHttpResponse(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.alibaba.nacos.common.model.RequestHttpEntity;
import com.alibaba.nacos.common.utils.HttpMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Type;
import java.net.URI;
Expand All @@ -39,12 +38,10 @@
*/
public class NacosAsyncRestTemplate extends AbstractNacosRestTemplate {

private static final Logger LOGGER = LoggerFactory.getLogger(NacosAsyncRestTemplate.class);
private final AsyncHttpClientRequest clientRequest;

private AsyncHttpClientRequest clientRequest;

public NacosAsyncRestTemplate(AsyncHttpClientRequest clientRequest) {
super();
public NacosAsyncRestTemplate(Logger logger, AsyncHttpClientRequest clientRequest) {
super(logger);
this.clientRequest = clientRequest;
}

Expand Down Expand Up @@ -335,8 +332,8 @@ public <T> void postForm(String url, Header header, Map<String, String> paramVal
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 " + httpMethod + " " + url);
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.HttpMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Type;
import java.net.URI;
Expand All @@ -43,14 +42,12 @@
*/
public class NacosRestTemplate extends AbstractNacosRestTemplate {

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

private final HttpClientRequest requestClient;

private final List<HttpClientRequestInterceptor> interceptors = new ArrayList<HttpClientRequestInterceptor>();

public NacosRestTemplate(HttpClientRequest requestClient) {
super();
public NacosRestTemplate(Logger logger, HttpClientRequest requestClient) {
super(logger);
this.requestClient = requestClient;
}

Expand Down Expand Up @@ -447,9 +444,10 @@ public List<HttpClientRequestInterceptor> getInterceptors() {
private <T> HttpRestResult<T> execute(String url, String httpMethod, RequestHttpEntity requestEntity,
Type responseType) throws Exception {
URI uri = HttpUtils.buildUri(url, requestEntity.getQuery());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("HTTP " + httpMethod + " " + url);
if (logger.isDebugEnabled()) {
logger.debug("HTTP method: {}, url: {}, body: {}", httpMethod, uri, requestEntity.getBody());
}

ResponseHandler<T> responseHandler = super.selectResponseHandler(responseType);
HttpClientResponse response = null;
try {
Expand All @@ -464,8 +462,8 @@ private <T> HttpRestResult<T> execute(String url, String httpMethod, RequestHttp

private HttpClientRequest requestClient() {
if (CollectionUtils.isNotEmpty(interceptors)) {
if (LOGGER.isDebugEnabled()) {
LOGGER.info("Execute via interceptors :{}", interceptors);
if (logger.isDebugEnabled()) {
logger.debug("Execute via interceptors :{}", interceptors);
}
return new InterceptingHttpClientRequest(requestClient, interceptors.iterator());
}
Expand Down
Loading