Search in sources :

Example 26 with Event

use of com.alibaba.nacos.api.naming.listener.Event in project nacos by alibaba.

the class SubscribeCluster_ITCase method subscribeAdd.

/**
 * 添加IP,收到通知
 *
 * @throws Exception
 */
@Test(timeout = 10000L)
public void subscribeAdd() throws Exception {
    String serviceName = randomDomainName();
    naming.subscribe(serviceName, Arrays.asList("c1"), new EventListener() {

        @Override
        public void onEvent(Event event) {
            System.out.println(((NamingEvent) event).getServiceName());
            System.out.println(((NamingEvent) event).getInstances());
            instances = ((NamingEvent) event).getInstances();
        }
    });
    naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
    while (instances.isEmpty()) {
        Thread.sleep(1000L);
    }
    Assert.assertTrue(verifyInstanceList(instances, naming.getAllInstances(serviceName)));
}
Also used : NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) Event(com.alibaba.nacos.api.naming.listener.Event) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 27 with Event

use of com.alibaba.nacos.api.naming.listener.Event in project nacos by alibaba.

the class SubscribeCluster_ITCase method subscribeOtherCluster.

/**
 * 新增其他cluster IP,不会收到通知
 *
 * @throws Exception
 */
@Test
public void subscribeOtherCluster() throws Exception {
    String serviceName = randomDomainName();
    naming.subscribe(serviceName, Arrays.asList("c2"), new EventListener() {

        int index = 0;

        @Override
        public void onEvent(Event event) {
            if (index == 0) {
                index++;
                return;
            }
            System.out.println(((NamingEvent) event).getServiceName());
            System.out.println(((NamingEvent) event).getInstances());
            instances = ((NamingEvent) event).getInstances();
        }
    });
    naming.registerInstance(serviceName, "1.1.1.1", TEST_PORT, "c1");
    int i = 0;
    while (instances.isEmpty()) {
        Thread.sleep(1000L);
        if (i++ > 10) {
            return;
        }
    }
    Assert.fail();
}
Also used : NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) Event(com.alibaba.nacos.api.naming.listener.Event) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 28 with Event

use of com.alibaba.nacos.api.naming.listener.Event in project nacos by alibaba.

the class Subscribe_ITCase method subscribeEmpty.

@Test(timeout = 4 * TIME_OUT)
public void subscribeEmpty() throws Exception {
    String serviceName = randomDomainName();
    naming.subscribe(serviceName, new EventListener() {

        @Override
        public void onEvent(Event event) {
            System.out.println(((NamingEvent) event).getServiceName());
            System.out.println(((NamingEvent) event).getInstances());
            instances = ((NamingEvent) event).getInstances();
        }
    });
    naming.registerInstance(serviceName, "1.1.1.1", TEST_PORT, "c1");
    while (instances.isEmpty()) {
        Thread.sleep(1000L);
    }
    Assert.assertTrue(verifyInstanceList(instances, naming.getAllInstances(serviceName)));
    naming.deregisterInstance(serviceName, "1.1.1.1", TEST_PORT, "c1");
    while (!instances.isEmpty()) {
        Thread.sleep(1000L);
    }
    Assert.assertEquals(0, instances.size());
    Assert.assertEquals(0, naming.getAllInstances(serviceName).size());
}
Also used : NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) Event(com.alibaba.nacos.api.naming.listener.Event) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 29 with Event

use of com.alibaba.nacos.api.naming.listener.Event in project nacos by alibaba.

the class Unsubscribe_ITCase method unsubscribe.

/**
 * 取消订阅,添加IP,不会收到通知
 * @throws Exception
 */
@Test
public void unsubscribe() throws Exception {
    String serviceName = randomDomainName();
    EventListener listener = new EventListener() {

        @Override
        public void onEvent(Event event) {
            System.out.println(((NamingEvent) event).getServiceName());
            System.out.println(((NamingEvent) event).getInstances());
            instances = ((NamingEvent) event).getInstances();
        }
    };
    naming.subscribe(serviceName, listener);
    naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
    while (instances.isEmpty()) {
        Thread.sleep(1000L);
    }
    Assert.assertTrue(verifyInstanceList(instances, naming.getAllInstances(serviceName)));
    naming.unsubscribe(serviceName, listener);
    instances = Collections.emptyList();
    naming.registerInstance(serviceName, "127.0.0.2", TEST_PORT, "c1");
    int i = 0;
    while (instances.isEmpty()) {
        Thread.sleep(1000L);
        if (i++ > 10) {
            return;
        }
    }
    Assert.fail();
}
Also used : Event(com.alibaba.nacos.api.naming.listener.Event) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Event (com.alibaba.nacos.api.naming.listener.Event)29 EventListener (com.alibaba.nacos.api.naming.listener.EventListener)28 Test (org.junit.Test)26 NamingEvent (com.alibaba.nacos.api.naming.listener.NamingEvent)21 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)18 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)8 NamingService (com.alibaba.nacos.api.naming.NamingService)3 Instance (com.alibaba.nacos.api.naming.pojo.Instance)2 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)2 Properties (java.util.Properties)2 NacosDiscoveryProperties (com.alibaba.cloud.nacos.NacosDiscoveryProperties)1 NacosServiceManager (com.alibaba.cloud.nacos.NacosServiceManager)1 NacosException (com.alibaba.nacos.api.exception.NacosException)1 AbstractEventListener (com.alibaba.nacos.api.naming.listener.AbstractEventListener)1 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)1 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)1 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)1 ProviderInfoListener (com.alipay.sofa.rpc.listener.ProviderInfoListener)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Arrays (java.util.Arrays)1