Skip to content

Commit

Permalink
fix: alibaba#3324, Move http client close method to NamingProxy.shutd…
Browse files Browse the repository at this point in the history
…own() (alibaba#3333)
  • Loading branch information
Maijh97 authored Jul 15, 2020
1 parent 52d9cf9 commit 0163d2d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@

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

import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.api.exception.NacosException;
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 com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.utils.ExceptionUtil;
import org.slf4j.Logger;

import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER;

/**
* http Manager.
*
* @author mai.jh
*/
public class NamingHttpClientManager {
public class NamingHttpClientManager implements Closeable {

private static final int READ_TIME_OUT_MILLIS = Integer
.getInteger("com.alibaba.nacos.client.naming.rtimeout", 50000);
Expand All @@ -42,17 +46,38 @@ public class NamingHttpClientManager {

private static final HttpClientFactory HTTP_CLIENT_FACTORY = new NamingHttpClientFactory();

public static String getPrefix() {
private static class NamingHttpClientManagerInstance {

private static final NamingHttpClientManager INSTANCE = new NamingHttpClientManager();
}

public static NamingHttpClientManager getInstance() {
return NamingHttpClientManagerInstance.INSTANCE;
}

public String getPrefix() {
if (ENABLE_HTTPS) {
return "https://";
}
return "http://";
}

public static NacosRestTemplate getNacosRestTemplate() {
public NacosRestTemplate getNacosRestTemplate() {
return HttpClientBeanHolder.getNacosRestTemplate(HTTP_CLIENT_FACTORY);
}

@Override
public void shutdown() throws NacosException {
NAMING_LOGGER.warn("[NamingHttpClientManager] Start destroying NacosRestTemplate");
try {
HttpClientBeanHolder.shutdownNacostSyncRest(HTTP_CLIENT_FACTORY.getClass().getName());
} catch (Exception ex) {
NAMING_LOGGER.error("[NamingHttpClientManager] An exception occurred when the HTTP client was closed : {}",
ExceptionUtil.getStackTrace(ex));
}
NAMING_LOGGER.warn("[NamingHttpClientManager] Destruction of the end");
}

private static class NamingHttpClientFactory extends AbstractHttpClientFactory {

@Override
Expand All @@ -63,7 +88,7 @@ protected HttpClientConfig buildHttpClientConfig() {

@Override
protected Logger assignLogger() {
return LogUtils.NAMING_LOGGER;
return NAMING_LOGGER;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
*/
public class NamingProxy implements Closeable {

private final NacosRestTemplate nacosRestTemplate = NamingHttpClientManager.getNacosRestTemplate();
private final NacosRestTemplate nacosRestTemplate = NamingHttpClientManager.getInstance().getNacosRestTemplate();

private static final int DEFAULT_SERVER_PORT = 8848;

Expand Down Expand Up @@ -591,7 +591,7 @@ public String callServer(String api, Map<String, String> params, Map<String, Str
if (!curServer.contains(UtilAndComs.SERVER_ADDR_IP_SPLITER)) {
curServer = curServer + UtilAndComs.SERVER_ADDR_IP_SPLITER + serverPort;
}
url = NamingHttpClientManager.getPrefix() + curServer + api;
url = NamingHttpClientManager.getInstance().getPrefix() + curServer + api;
}

try {
Expand Down Expand Up @@ -714,6 +714,7 @@ public void shutdown() throws NacosException {
String className = this.getClass().getName();
NAMING_LOGGER.info("{} do shutdown begin", className);
ThreadUtils.shutdownThreadPool(executorService, NAMING_LOGGER);
NamingHttpClientManager.getInstance().shutdown();
NAMING_LOGGER.info("{} do shutdown stop", className);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class SecurityProxy {

private static final String LOGIN_URL = "/v1/auth/users/login";

private final NacosRestTemplate nacosRestTemplate = NamingHttpClientManager.getNacosRestTemplate();
private final NacosRestTemplate nacosRestTemplate = NamingHttpClientManager.getInstance().getNacosRestTemplate();

private String contextPath;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@

import com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.utils.ExceptionUtil;
import com.alibaba.nacos.common.utils.ThreadUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* Create a rest template to ensure that each custom client config and rest template are in one-to-one correspondence.
Expand All @@ -35,22 +30,10 @@
*/
public final class HttpClientBeanHolder {

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

private static final Map<String, NacosRestTemplate> SINGLETON_REST = new HashMap<String, NacosRestTemplate>(10);

private static final Map<String, NacosAsyncRestTemplate> SINGLETON_ASYNC_REST = new HashMap<String, NacosAsyncRestTemplate>(10);

private static final AtomicBoolean ALREADY_SHUTDOWN = new AtomicBoolean(false);

static {
ThreadUtils.addShutdownHook(new Runnable() {
@Override
public void run() {
shutdown();
}
});
}
private static final Map<String, NacosAsyncRestTemplate> SINGLETON_ASYNC_REST = new HashMap<String, NacosAsyncRestTemplate>(
10);

public static NacosRestTemplate getNacosRestTemplate(Logger logger) {
return getNacosRestTemplate(new DefaultHttpClientFactory(logger));
Expand Down Expand Up @@ -99,40 +82,41 @@ public static NacosAsyncRestTemplate getNacosAsyncRestTemplate(HttpClientFactory
}

/**
* Shutdown http client holder and close all template.
* Shutdown http client holder and close remove template.
*
* @param className HttpClientFactory implement class name
* @throws Exception ex
*/
public static void shutdown() {
if (!ALREADY_SHUTDOWN.compareAndSet(false, true)) {
return;
}
LOGGER.warn("[HttpClientBeanFactory] Start destroying NacosRestTemplate");
try {
nacostRestTemplateShutdown();
nacosAsyncRestTemplateShutdown();
} catch (Exception ex) {
LOGGER.error("[HttpClientBeanFactory] An exception occurred when the HTTP client was closed : {}",
ExceptionUtil.getStackTrace(ex));
}
LOGGER.warn("[HttpClientBeanFactory] Destruction of the end");
public static void shutdown(String className) throws Exception {
shutdownNacostSyncRest(className);
shutdownNacosAsyncRest(className);
}

private static void nacostRestTemplateShutdown() throws Exception {
if (!SINGLETON_REST.isEmpty()) {
Collection<NacosRestTemplate> nacosRestTemplates = SINGLETON_REST.values();
for (NacosRestTemplate nacosRestTemplate : nacosRestTemplates) {
nacosRestTemplate.close();
}
SINGLETON_REST.clear();
/**
* Shutdown sync http client holder and remove template.
*
* @param className HttpClientFactory implement class name
* @throws Exception ex
*/
public static void shutdownNacostSyncRest(String className) throws Exception {
final NacosRestTemplate nacosRestTemplate = SINGLETON_REST.get(className);
if (nacosRestTemplate != null) {
nacosRestTemplate.close();
SINGLETON_REST.remove(className);
}
}

private static void nacosAsyncRestTemplateShutdown() throws Exception {
if (!SINGLETON_ASYNC_REST.isEmpty()) {
Collection<NacosAsyncRestTemplate> nacosAsyncRestTemplates = SINGLETON_ASYNC_REST.values();
for (NacosAsyncRestTemplate nacosAsyncRestTemplate : nacosAsyncRestTemplates) {
nacosAsyncRestTemplate.close();
}
SINGLETON_ASYNC_REST.clear();
/**
* Shutdown async http client holder and remove template.
*
* @param className HttpClientFactory implement class name
* @throws Exception ex
*/
public static void shutdownNacosAsyncRest(String className) throws Exception {
final NacosAsyncRestTemplate nacosAsyncRestTemplate = SINGLETON_ASYNC_REST.get(className);
if (nacosAsyncRestTemplate != null) {
nacosAsyncRestTemplate.close();
SINGLETON_ASYNC_REST.remove(className);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
public class NamingBase extends HttpClient4Test {

private static final NacosRestTemplate nacosRestTemplate = NamingHttpClientManager.getNacosRestTemplate();
private static final NacosRestTemplate nacosRestTemplate = NamingHttpClientManager.getInstance().getNacosRestTemplate();

public static final String TEST_DOM_1 = "nacos.test.1";
public static final String TEST_IP_4_DOM_1 = "127.0.0.1";
Expand Down

0 comments on commit 0163d2d

Please sign in to comment.