[ISSUE #7276] init security proxy when construct in NacosNamingMainta… (#7287)

* [ISSUE #7276] init security proxy when construct in NacosNamingMaintainService

* [ISSUE #7276] add something to make ci re-run
This commit is contained in:
MajorHe1 2021-12-21 12:51:21 +08:00 committed by GitHub
parent e5da9be27f
commit c6bae6ba1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 5 deletions

View File

@ -16,6 +16,8 @@
package com.alibaba.nacos.client.constant;
import java.util.concurrent.TimeUnit;
/**
* All the constants.
*
@ -56,4 +58,10 @@ public class Constants {
public static final String HTTPS = "https://";
}
public static class Security {
public static final long SECURITY_INFO_REFRESH_INTERVAL_MILLS = TimeUnit.SECONDS.toMillis(5);
}
}

View File

@ -31,9 +31,16 @@ import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy;
import com.alibaba.nacos.client.naming.utils.InitUtils;
import com.alibaba.nacos.client.security.SecurityProxy;
import com.alibaba.nacos.client.utils.ValidatorUtils;
import com.alibaba.nacos.common.utils.ThreadUtils;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static com.alibaba.nacos.client.constant.Constants.Security.SECURITY_INFO_REFRESH_INTERVAL_MILLS;
import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER;
/**
* Nacos naming maintain service.
@ -48,6 +55,12 @@ public class NacosNamingMaintainService implements NamingMaintainService {
private NamingHttpClientProxy serverProxy;
private ServerListManager serverListManager;
private SecurityProxy securityProxy;
private ScheduledExecutorService executorService;
public NacosNamingMaintainService(String serverList) throws NacosException {
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverList);
@ -63,12 +76,26 @@ public class NacosNamingMaintainService implements NamingMaintainService {
namespace = InitUtils.initNamespaceForNaming(properties);
InitUtils.initSerialization();
InitUtils.initWebRootContext(properties);
ServerListManager serverListManager = new ServerListManager(properties, namespace);
SecurityProxy securityProxy = new SecurityProxy(properties,
serverListManager = new ServerListManager(properties, namespace);
securityProxy = new SecurityProxy(properties,
NamingHttpClientManager.getInstance().getNacosRestTemplate());
initSecurityProxy();
serverProxy = new NamingHttpClientProxy(namespace, securityProxy, serverListManager, properties, null);
}
private void initSecurityProxy() {
this.executorService = new ScheduledThreadPoolExecutor(1, r -> {
Thread t = new Thread(r);
t.setName("com.alibaba.nacos.client.naming.maintainService.security");
t.setDaemon(true);
return t;
});
this.securityProxy.login(serverListManager.getServerList());
this.executorService.scheduleWithFixedDelay(() -> securityProxy.login(serverListManager.getServerList()), 0,
SECURITY_INFO_REFRESH_INTERVAL_MILLS, TimeUnit.MILLISECONDS);
}
@Override
public void updateInstance(String serviceName, Instance instance) throws NacosException {
updateInstance(serviceName, Constants.DEFAULT_GROUP, instance);
@ -167,6 +194,11 @@ public class NacosNamingMaintainService implements NamingMaintainService {
@Override
public void shutDown() throws NacosException {
String className = this.getClass().getName();
NAMING_LOGGER.info("{} do shutdown begin", className);
serverListManager.shutdown();
serverProxy.shutdown();
ThreadUtils.shutdownThreadPool(executorService, NAMING_LOGGER);
NAMING_LOGGER.info("{} do shutdown stop", className);
}
}

View File

@ -39,6 +39,7 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static com.alibaba.nacos.client.constant.Constants.Security.SECURITY_INFO_REFRESH_INTERVAL_MILLS;
import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER;
/**
@ -48,8 +49,6 @@ import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER;
*/
public class NamingClientProxyDelegate implements NamingClientProxy {
private final long securityInfoRefreshIntervalMills = TimeUnit.SECONDS.toMillis(5);
private final ServerListManager serverListManager;
private final ServiceInfoUpdateService serviceInfoUpdateService;
@ -87,7 +86,7 @@ public class NamingClientProxyDelegate implements NamingClientProxy {
});
this.securityProxy.login(serverListManager.getServerList());
this.executorService.scheduleWithFixedDelay(() -> securityProxy.login(serverListManager.getServerList()), 0,
securityInfoRefreshIntervalMills, TimeUnit.MILLISECONDS);
SECURITY_INFO_REFRESH_INTERVAL_MILLS, TimeUnit.MILLISECONDS);
}
@Override

View File

@ -26,7 +26,9 @@ import com.alibaba.nacos.api.naming.pojo.Service;
import com.alibaba.nacos.api.selector.AbstractSelector;
import com.alibaba.nacos.api.selector.ExpressionSelector;
import com.alibaba.nacos.api.selector.NoneSelector;
import com.alibaba.nacos.client.naming.core.ServerListManager;
import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy;
import com.alibaba.nacos.client.security.SecurityProxy;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@ -37,6 +39,7 @@ import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ScheduledExecutorService;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
@ -49,15 +52,35 @@ public class NacosNamingMaintainServiceTest {
private NamingHttpClientProxy serverProxy;
private ServerListManager serverListManager;
private SecurityProxy securityProxy;
private ScheduledExecutorService executorService;
@Before
public void setUp() throws Exception {
Properties prop = new Properties();
prop.setProperty(PropertyKeyConst.NAMESPACE, "public");
nacosNamingMaintainService = new NacosNamingMaintainService(prop);
serverProxy = mock(NamingHttpClientProxy.class);
serverListManager = mock(ServerListManager.class);
securityProxy = mock(SecurityProxy.class);
executorService = mock(ScheduledExecutorService.class);
Field serverProxyField = NacosNamingMaintainService.class.getDeclaredField("serverProxy");
serverProxyField.setAccessible(true);
serverProxyField.set(nacosNamingMaintainService, serverProxy);
Field serverListManagerField = NacosNamingMaintainService.class.getDeclaredField("serverListManager");
serverListManagerField.setAccessible(true);
serverListManagerField.set(nacosNamingMaintainService, serverListManager);
Field securityProxyFiled = NacosNamingMaintainService.class.getDeclaredField("securityProxy");
securityProxyFiled.setAccessible(true);
securityProxyFiled.set(nacosNamingMaintainService, securityProxy);
Field executorServiceField = NacosNamingMaintainService.class.getDeclaredField("executorService");
executorServiceField.setAccessible(true);
executorServiceField.set(nacosNamingMaintainService, executorService);
}
@After
@ -275,8 +298,11 @@ public class NacosNamingMaintainServiceTest {
@Test
public void testShutDown() throws NacosException {
//when
nacosNamingMaintainService.shutDown();
//then
verify(serverProxy, times(1)).shutdown();
verify(serverListManager, times(1)).shutdown();
verify(executorService, times(1)).shutdown();
}
}