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#3712] add apache http client factory #3716

Merged
merged 4 commits into from
Sep 1, 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
@@ -0,0 +1,38 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.common.http;

import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.http.client.request.DefaultHttpClientRequest;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClients;

/**
* apache http client factory implements.
*
* @author mai.jh
*/
public abstract class AbstractApacheHttpClientFactory extends AbstractHttpClientFactory {

@Override
public final NacosRestTemplate createNacosRestTemplate() {
final RequestConfig requestConfig = getRequestConfig();
return new NacosRestTemplate(assignLogger(),
new DefaultHttpClientRequest(HttpClients.custom().setDefaultRequestConfig(requestConfig).build()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
public abstract class AbstractHttpClientFactory implements HttpClientFactory {

@Override
public final NacosRestTemplate createNacosRestTemplate() {
public NacosRestTemplate createNacosRestTemplate() {
HttpClientConfig httpClientConfig = buildHttpClientConfig();
final JdkHttpClientRequest clientRequest = new JdkHttpClientRequest(httpClientConfig);

Expand All @@ -66,13 +66,13 @@ public void onChanged(String filePath) {
}

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

private RequestConfig getRequestConfig() {
protected RequestConfig getRequestConfig() {
HttpClientConfig httpClientConfig = buildHttpClientConfig();
return RequestConfig.custom().setConnectTimeout(httpClientConfig.getConTimeOutMillis())
.setSocketTimeout(httpClientConfig.getReadTimeOutMillis())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package com.alibaba.nacos.common.http;

import com.alibaba.nacos.common.utils.HttpMethod;

import com.alibaba.nacos.common.utils.StringUtils;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPatch;
Expand All @@ -28,6 +28,8 @@
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpTrace;

import java.net.URI;

/**
* Base http method.
*
Expand All @@ -48,7 +50,7 @@ protected HttpRequestBase createRequest(String url) {
GET_LARGE(HttpMethod.GET_LARGE) {
@Override
protected HttpRequestBase createRequest(String url) {
return new BaseHttpClient.HttpGetWithEntity(url);
return new HttpGetWithEntity(url);
}
},

Expand Down Expand Up @@ -151,4 +153,22 @@ public static BaseHttpMethod sourceOf(String name) {
throw new IllegalArgumentException("Unsupported http method : " + name);
}

/**
* get Large implemented.
*/
public static class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {

public static final String METHOD_NAME = "GET";

public HttpGetWithEntity(String url) {
super();
setURI(URI.create(url));
}

@Override
public String getMethod() {
return METHOD_NAME;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import com.alibaba.nacos.common.http.HttpRestResult;
import com.alibaba.nacos.common.http.HttpUtils;
import com.alibaba.nacos.common.http.client.handler.ResponseHandler;
import com.alibaba.nacos.common.http.client.request.DefaultHttpClientRequest;
import com.alibaba.nacos.common.http.client.request.HttpClientRequest;
import com.alibaba.nacos.common.http.client.request.JdkHttpClientRequest;
import com.alibaba.nacos.common.http.client.response.HttpClientResponse;
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.MediaType;
Expand Down Expand Up @@ -95,6 +97,9 @@ public <T> HttpRestResult<T> get(String url, HttpClientConfig config, Header hea
* get request, may be pulling a lot of data URL request params are expanded using the given query {@link Query},
* More request parameters can be set via body.
*
* <p>This method can only be used when HttpClientRequest is implemented by {@link DefaultHttpClientRequest}, note:
* {@link JdkHttpClientRequest} Implementation does not support this method.
*
* <p>{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type.
*
* @param url url
Expand Down