Search in sources :

Example 21 with ServerListManager

use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.

the class NamingHttpClientProxyTest method testUpdateBeatInfo.

@Test
public void testUpdateBeatInfo() throws Exception {
    // given
    BeatReactor mockBeatReactor = mock(BeatReactor.class);
    Field dom2Beat = BeatReactor.class.getDeclaredField("dom2Beat");
    ConcurrentHashMap<String, BeatInfo> beatMap = new ConcurrentHashMap<>();
    String beatServiceName = "service1#127.0.0.1#10000";
    beatMap.put(beatServiceName, new BeatInfo());
    dom2Beat.setAccessible(true);
    dom2Beat.set(mockBeatReactor, beatMap);
    SecurityProxy proxy = mock(SecurityProxy.class);
    ServerListManager mgr = mock(ServerListManager.class);
    when(mgr.getServerList()).thenReturn(Arrays.asList("localhost"));
    Properties props = new Properties();
    ServiceInfoHolder holder = mock(ServiceInfoHolder.class);
    NamingHttpClientProxy clientProxy = new NamingHttpClientProxy("namespaceId", proxy, mgr, props, holder);
    final Field mockBeatReactorField = NamingHttpClientProxy.class.getDeclaredField("beatReactor");
    mockBeatReactorField.setAccessible(true);
    mockBeatReactorField.set(clientProxy, mockBeatReactor);
    Instance instance = new Instance();
    instance.setInstanceId("id1");
    instance.setIp("127.0.0.1");
    instance.setPort(10000);
    instance.setServiceName("service1");
    instance.setClusterName("cluster1");
    Set<Instance> beats = new HashSet<>();
    beats.add(instance);
    when(mockBeatReactor.buildKey("service1", "127.0.0.1", 10000)).thenReturn(beatServiceName);
    // when
    clientProxy.updateBeatInfo(beats);
    // then
    verify(mockBeatReactor, times(1)).addBeatInfo(eq("service1"), any());
}
Also used : ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) Instance(com.alibaba.nacos.api.naming.pojo.Instance) BeatReactor(com.alibaba.nacos.client.naming.beat.BeatReactor) BeatInfo(com.alibaba.nacos.client.naming.beat.BeatInfo) Properties(java.util.Properties) Field(java.lang.reflect.Field) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 22 with ServerListManager

use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.

the class NamingHttpClientProxyTest method testSubscribe.

@Test
public void testSubscribe() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> a = new HttpRestResult<Object>();
    a.setData("");
    a.setCode(200);
    when(nacosRestTemplate.exchangeForm(any(), any(), any(), any(), any(), any())).thenReturn(a);
    SecurityProxy proxy = mock(SecurityProxy.class);
    ServerListManager mgr = mock(ServerListManager.class);
    when(mgr.getServerList()).thenReturn(Arrays.asList("localhost"));
    Properties props = new Properties();
    ServiceInfoHolder holder = mock(ServiceInfoHolder.class);
    NamingHttpClientProxy clientProxy = new NamingHttpClientProxy("namespaceId", proxy, mgr, props, holder);
    final Field nacosRestTemplateField = NamingHttpClientProxy.class.getDeclaredField("nacosRestTemplate");
    nacosRestTemplateField.setAccessible(true);
    nacosRestTemplateField.set(clientProxy, nacosRestTemplate);
    String groupName = "group1";
    String serviceName = "serviceName";
    String clusters = "clusters";
    // when
    ServiceInfo serviceInfo = clientProxy.subscribe(serviceName, groupName, clusters);
    // then
    verify(nacosRestTemplate, times(1)).exchangeForm(endsWith("/instance/list"), any(), any(), any(), eq(HttpMethod.GET), any());
    Assert.assertEquals(groupName + "@@" + serviceName, serviceInfo.getName());
    Assert.assertEquals(clusters, serviceInfo.getClusters());
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Field(java.lang.reflect.Field) ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 23 with ServerListManager

use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.

the class NamingHttpClientProxyTest method testCreateService.

@Test
public void testCreateService() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> a = new HttpRestResult<Object>();
    a.setData("");
    a.setCode(200);
    when(nacosRestTemplate.exchangeForm(any(), any(), any(), any(), any(), any())).thenReturn(a);
    SecurityProxy proxy = mock(SecurityProxy.class);
    ServerListManager mgr = mock(ServerListManager.class);
    when(mgr.getServerList()).thenReturn(Arrays.asList("localhost"));
    Properties props = new Properties();
    ServiceInfoHolder holder = mock(ServiceInfoHolder.class);
    NamingHttpClientProxy clientProxy = new NamingHttpClientProxy("namespaceId", proxy, mgr, props, holder);
    final Field nacosRestTemplateField = NamingHttpClientProxy.class.getDeclaredField("nacosRestTemplate");
    nacosRestTemplateField.setAccessible(true);
    nacosRestTemplateField.set(clientProxy, nacosRestTemplate);
    // when
    clientProxy.createService(new Service(), new NoneSelector());
    // then
    verify(nacosRestTemplate, times(1)).exchangeForm(endsWith(UtilAndComs.nacosUrlService), any(), any(), any(), eq(HttpMethod.POST), any());
}
Also used : Field(java.lang.reflect.Field) ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) Service(com.alibaba.nacos.api.naming.pojo.Service) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) NoneSelector(com.alibaba.nacos.api.selector.NoneSelector) Test(org.junit.Test)

Example 24 with ServerListManager

use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.

the class NamingHttpClientProxyTest method testUnsubscribe.

@Test
public void testUnsubscribe() throws Exception {
    // TODO thrown.expect(UnsupportedOperationException.class);
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> a = new HttpRestResult<Object>();
    a.setData("");
    a.setCode(200);
    when(nacosRestTemplate.exchangeForm(any(), any(), any(), any(), any(), any())).thenReturn(a);
    SecurityProxy proxy = mock(SecurityProxy.class);
    ServerListManager mgr = mock(ServerListManager.class);
    when(mgr.getServerList()).thenReturn(Arrays.asList("localhost"));
    Properties props = new Properties();
    ServiceInfoHolder holder = mock(ServiceInfoHolder.class);
    NamingHttpClientProxy clientProxy = new NamingHttpClientProxy("namespaceId", proxy, mgr, props, holder);
    final Field nacosRestTemplateField = NamingHttpClientProxy.class.getDeclaredField("nacosRestTemplate");
    nacosRestTemplateField.setAccessible(true);
    nacosRestTemplateField.set(clientProxy, nacosRestTemplate);
    String groupName = "group1";
    String serviceName = "serviceName";
    String clusters = "clusters";
    // when
    clientProxy.unsubscribe(serviceName, groupName, clusters);
}
Also used : Field(java.lang.reflect.Field) ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 25 with ServerListManager

use of com.alibaba.nacos.client.naming.core.ServerListManager in project nacos by alibaba.

the class NacosNamingMaintainService method init.

private void init(Properties properties) throws NacosException {
    ValidatorUtils.checkInitParam(properties);
    namespace = InitUtils.initNamespaceForNaming(properties);
    InitUtils.initSerialization();
    InitUtils.initWebRootContext(properties);
    serverListManager = new ServerListManager(properties, namespace);
    securityProxy = new SecurityProxy(serverListManager.getServerList(), NamingHttpClientManager.getInstance().getNacosRestTemplate());
    initSecurityProxy(properties);
    serverProxy = new NamingHttpClientProxy(namespace, securityProxy, serverListManager, properties, null);
}
Also used : NamingHttpClientProxy(com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy)

Aggregations

ServerListManager (com.alibaba.nacos.client.naming.core.ServerListManager)25 SecurityProxy (com.alibaba.nacos.client.security.SecurityProxy)25 Properties (java.util.Properties)24 ServiceInfoHolder (com.alibaba.nacos.client.naming.cache.ServiceInfoHolder)23 Test (org.junit.Test)23 Field (java.lang.reflect.Field)22 HttpRestResult (com.alibaba.nacos.common.http.HttpRestResult)18 NacosRestTemplate (com.alibaba.nacos.common.http.client.NacosRestTemplate)18 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 HashMap (java.util.HashMap)5 Instance (com.alibaba.nacos.api.naming.pojo.Instance)4 Service (com.alibaba.nacos.api.naming.pojo.Service)3 NoneSelector (com.alibaba.nacos.api.selector.NoneSelector)3 BeatReactor (com.alibaba.nacos.client.naming.beat.BeatReactor)3 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)2 BeatInfo (com.alibaba.nacos.client.naming.beat.BeatInfo)2 NamingHttpClientProxy (com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy)2 HashSet (java.util.HashSet)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Before (org.junit.Before)1