Skip to content

Commit

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

* core module replace http client

* replace http client
  • Loading branch information
Maijh97 authored Aug 6, 2020
1 parent 7a471c5 commit b1db2be
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
* Base http client.
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
* @deprecated Refer to the new {@link com.alibaba.nacos.common.http.client.request.HttpClientRequest}
*/
@Deprecated
public abstract class BaseHttpClient {

protected <T> RestResult<T> execute(CloseableHttpClient httpClient, final Type type, HttpUriRequest request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
* Use the same HttpClient object in the same space.
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
* @deprecated Refer to the new {@link HttpClientBeanHolder}
*/
@Deprecated
public class HttpClientManager {

private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientManager.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
* Nacos async http client interface.
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
* @deprecated Refer to the new {@link com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate}
*/
@Deprecated
@SuppressWarnings("all")
public interface NAsyncHttpClient extends NHttpClient {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
* Nacos http client interface.
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
* @deprecated Refer to the new {@link com.alibaba.nacos.common.http.client.request.HttpClientRequest}
*/
@Deprecated
@SuppressWarnings("PMD.ClassNamingShouldBeCamelRule")
public interface NHttpClient extends Closeable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
* Nacos sync http client.
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
* @deprecated Refer to the new {@link com.alibaba.nacos.common.http.client.NacosRestTemplate}
*/
@Deprecated
@SuppressWarnings("all")
public interface NSyncHttpClient extends NHttpClient {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
* Nacos async http client.
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
* @deprecated Refer to the new {@link com.alibaba.nacos.common.http.client.request.DefaultAsyncHttpClientRequest}
*/
@Deprecated
class NacosAsyncHttpClient extends BaseHttpClient implements NAsyncHttpClient {

private CloseableHttpAsyncClient asyncClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
* Nacos sync http client.
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
* @deprecated Refer to the new {@link com.alibaba.nacos.common.http.client.request.JdkHttpClientRequest}
*/
@Deprecated
class NacosSyncHttpClient extends BaseHttpClient implements NSyncHttpClient {

private CloseableHttpClient client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.common.JustForTest;
import com.alibaba.nacos.common.http.Callback;
import com.alibaba.nacos.common.http.HttpClientManager;
import com.alibaba.nacos.common.http.HttpClientBeanHolder;
import com.alibaba.nacos.common.http.HttpUtils;
import com.alibaba.nacos.common.http.NAsyncHttpClient;
import com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate;
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.Query;
import com.alibaba.nacos.common.model.RestResult;
Expand Down Expand Up @@ -78,7 +78,7 @@
@Component(value = "serverMemberManager")
public class ServerMemberManager implements ApplicationListener<WebServerInitializedEvent> {

private final NAsyncHttpClient asyncHttpClient = HttpClientManager.getAsyncHttpClient();
private final NacosAsyncRestTemplate asyncRestTemplate = HttpClientBeanHolder.getNacosAsyncRestTemplate(Loggers.CORE);

/**
* Cluster node list.
Expand Down Expand Up @@ -452,7 +452,7 @@ protected void executeBody() {
"/cluster/report");

try {
asyncHttpClient
asyncRestTemplate
.post(url, Header.newInstance().addParam(Constants.NACOS_SERVER_HEADER, VersionUtils.version),
Query.EMPTY, getSelf(), reference.getType(), new Callback<String>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package com.alibaba.nacos.core.cluster.lookup;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.common.http.HttpClientManager;
import com.alibaba.nacos.common.http.NSyncHttpClient;
import com.alibaba.nacos.common.http.HttpClientBeanHolder;
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.model.RestResult;
Expand All @@ -30,7 +30,6 @@
import com.alibaba.nacos.core.utils.GlobalExecutor;
import com.alibaba.nacos.core.utils.Loggers;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;

import java.io.Reader;
import java.io.StringReader;
Expand Down Expand Up @@ -63,7 +62,7 @@ public class AddressServerMemberLookup extends AbstractMemberLookup {

private int maxFailCount = 12;

private NSyncHttpClient syncHttpClient = HttpClientManager.getSyncHttpClient();
private final NacosRestTemplate restTemplate = HttpClientBeanHolder.getNacosRestTemplate(Loggers.CORE);

private volatile boolean shutdown = false;

Expand Down Expand Up @@ -142,9 +141,9 @@ public Map<String, Object> info() {
}

private void syncFromAddressUrl() throws Exception {
RestResult<String> result = syncHttpClient
RestResult<String> result = restTemplate
.get(addressServerUrl, Header.EMPTY, Query.EMPTY, genericType.getType());
if (HttpStatus.OK.value() == result.getCode()) {
if (result.ok()) {
isAddressServerHealth = true;
Reader reader = new StringReader(result.getData());
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public void receiveConfigInfo(String configInfo) {
public void test_e_derby_ops() throws Exception {
String url = "http://127.0.0.1:8848/nacos/v1/cs/ops/derby";
Query query = Query.newInstance().addParam("sql", "select * from users");
RestResult<List<Map<String, Object>>> result = httpClient
RestResult<List<Map<String, Object>>> result = restTemplate
.get(url, Header.EMPTY, query, new GenericType<RestResult<List<Map<String, Object>>>>() {
}.getType());
System.out.println(result.getData());
Expand All @@ -271,7 +271,7 @@ public void test_e_derby_ops() throws Exception {
public void test_g_derby_ops_no_select() throws Exception {
String url = "http://127.0.0.1:8848/nacos/v1/cs/ops/derby";
Query query = Query.newInstance().addParam("sql", "update users set username='nacos'");
RestResult<Object> result = httpClient.get(url, Header.EMPTY, query, new GenericType<RestResult<Object>>() {
RestResult<Object> result = restTemplate.get(url, Header.EMPTY, query, new GenericType<RestResult<Object>>() {
}.getType());
System.out.println(result);
Assert.assertFalse(result.ok());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.common.http.HttpClientManager;
import com.alibaba.nacos.common.http.HttpClientBeanHolder;
import com.alibaba.nacos.common.http.NSyncHttpClient;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.notify.listener.Subscriber;
Expand All @@ -36,6 +37,8 @@

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
Expand All @@ -60,6 +63,8 @@
*/
public class BaseClusterTest extends HttpClient4Test {

private static final Logger logger = LoggerFactory.getLogger(BaseClusterTest.class);

protected static final String CONFIG_INFO_ID = "config-info-id";

protected static ConfigService iconfig7;
Expand All @@ -74,7 +79,7 @@ public class BaseClusterTest extends HttpClient4Test {

protected static NamingService inaming9;

protected static final NSyncHttpClient httpClient = HttpClientManager.getSyncHttpClient();
protected static final NacosRestTemplate restTemplate = HttpClientBeanHolder.getNacosRestTemplate(logger);

protected static final AtomicBoolean[] finished = new AtomicBoolean[] {new AtomicBoolean(false),
new AtomicBoolean(false), new AtomicBoolean(false)};
Expand Down

0 comments on commit b1db2be

Please sign in to comment.