[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; package com.alibaba.nacos.client.constant;
import java.util.concurrent.TimeUnit;
/** /**
* All the constants. * All the constants.
* *
@ -56,4 +58,10 @@ public class Constants {
public static final String HTTPS = "https://"; 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.naming.utils.InitUtils;
import com.alibaba.nacos.client.security.SecurityProxy; import com.alibaba.nacos.client.security.SecurityProxy;
import com.alibaba.nacos.client.utils.ValidatorUtils; import com.alibaba.nacos.client.utils.ValidatorUtils;
import com.alibaba.nacos.common.utils.ThreadUtils;
import java.util.Map; import java.util.Map;
import java.util.Properties; 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. * Nacos naming maintain service.
@ -48,6 +55,12 @@ public class NacosNamingMaintainService implements NamingMaintainService {
private NamingHttpClientProxy serverProxy; private NamingHttpClientProxy serverProxy;
private ServerListManager serverListManager;
private SecurityProxy securityProxy;
private ScheduledExecutorService executorService;
public NacosNamingMaintainService(String serverList) throws NacosException { public NacosNamingMaintainService(String serverList) throws NacosException {
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverList); properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverList);
@ -63,12 +76,26 @@ public class NacosNamingMaintainService implements NamingMaintainService {
namespace = InitUtils.initNamespaceForNaming(properties); namespace = InitUtils.initNamespaceForNaming(properties);
InitUtils.initSerialization(); InitUtils.initSerialization();
InitUtils.initWebRootContext(properties); InitUtils.initWebRootContext(properties);
ServerListManager serverListManager = new ServerListManager(properties, namespace); serverListManager = new ServerListManager(properties, namespace);
SecurityProxy securityProxy = new SecurityProxy(properties, securityProxy = new SecurityProxy(properties,
NamingHttpClientManager.getInstance().getNacosRestTemplate()); NamingHttpClientManager.getInstance().getNacosRestTemplate());
initSecurityProxy();
serverProxy = new NamingHttpClientProxy(namespace, securityProxy, serverListManager, properties, null); 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 @Override
public void updateInstance(String serviceName, Instance instance) throws NacosException { public void updateInstance(String serviceName, Instance instance) throws NacosException {
updateInstance(serviceName, Constants.DEFAULT_GROUP, instance); updateInstance(serviceName, Constants.DEFAULT_GROUP, instance);
@ -167,6 +194,11 @@ public class NacosNamingMaintainService implements NamingMaintainService {
@Override @Override
public void shutDown() throws NacosException { public void shutDown() throws NacosException {
String className = this.getClass().getName();
NAMING_LOGGER.info("{} do shutdown begin", className);
serverListManager.shutdown();
serverProxy.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.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit; 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; 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 { public class NamingClientProxyDelegate implements NamingClientProxy {
private final long securityInfoRefreshIntervalMills = TimeUnit.SECONDS.toMillis(5);
private final ServerListManager serverListManager; private final ServerListManager serverListManager;
private final ServiceInfoUpdateService serviceInfoUpdateService; private final ServiceInfoUpdateService serviceInfoUpdateService;
@ -87,7 +86,7 @@ public class NamingClientProxyDelegate implements NamingClientProxy {
}); });
this.securityProxy.login(serverListManager.getServerList()); this.securityProxy.login(serverListManager.getServerList());
this.executorService.scheduleWithFixedDelay(() -> securityProxy.login(serverListManager.getServerList()), 0, this.executorService.scheduleWithFixedDelay(() -> securityProxy.login(serverListManager.getServerList()), 0,
securityInfoRefreshIntervalMills, TimeUnit.MILLISECONDS); SECURITY_INFO_REFRESH_INTERVAL_MILLS, TimeUnit.MILLISECONDS);
} }
@Override @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.AbstractSelector;
import com.alibaba.nacos.api.selector.ExpressionSelector; import com.alibaba.nacos.api.selector.ExpressionSelector;
import com.alibaba.nacos.api.selector.NoneSelector; 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.naming.remote.http.NamingHttpClientProxy;
import com.alibaba.nacos.client.security.SecurityProxy;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
@ -37,6 +39,7 @@ import java.lang.reflect.Field;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.ScheduledExecutorService;
import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -49,15 +52,35 @@ public class NacosNamingMaintainServiceTest {
private NamingHttpClientProxy serverProxy; private NamingHttpClientProxy serverProxy;
private ServerListManager serverListManager;
private SecurityProxy securityProxy;
private ScheduledExecutorService executorService;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
Properties prop = new Properties(); Properties prop = new Properties();
prop.setProperty(PropertyKeyConst.NAMESPACE, "public"); prop.setProperty(PropertyKeyConst.NAMESPACE, "public");
nacosNamingMaintainService = new NacosNamingMaintainService(prop); nacosNamingMaintainService = new NacosNamingMaintainService(prop);
serverProxy = mock(NamingHttpClientProxy.class); serverProxy = mock(NamingHttpClientProxy.class);
serverListManager = mock(ServerListManager.class);
securityProxy = mock(SecurityProxy.class);
executorService = mock(ScheduledExecutorService.class);
Field serverProxyField = NacosNamingMaintainService.class.getDeclaredField("serverProxy"); Field serverProxyField = NacosNamingMaintainService.class.getDeclaredField("serverProxy");
serverProxyField.setAccessible(true); serverProxyField.setAccessible(true);
serverProxyField.set(nacosNamingMaintainService, serverProxy); 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 @After
@ -275,8 +298,11 @@ public class NacosNamingMaintainServiceTest {
@Test @Test
public void testShutDown() throws NacosException { public void testShutDown() throws NacosException {
//when
nacosNamingMaintainService.shutDown(); nacosNamingMaintainService.shutDown();
//then //then
verify(serverProxy, times(1)).shutdown(); verify(serverProxy, times(1)).shutdown();
verify(serverListManager, times(1)).shutdown();
verify(executorService, times(1)).shutdown();
} }
} }