Search in sources :

Example 11 with Event

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

the class SubscribeCluster_ITCase method subscribeDelete.

/**
 * 删除IP,收到通知
 *
 * @throws Exception
 */
@Test(timeout = 10000L)
public void subscribeDelete() throws Exception {
    String serviceName = randomDomainName();
    naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
    TimeUnit.SECONDS.sleep(3);
    naming.subscribe(serviceName, Arrays.asList("c1"), 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();
        }
    });
    TimeUnit.SECONDS.sleep(1);
    naming.deregisterInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
    while (!instances.isEmpty()) {
        Thread.sleep(1000L);
    }
    Assert.assertTrue(instances.isEmpty());
}
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 12 with Event

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

the class Subscribe_ITCase method subscribeAdd.

/**
 * 添加IP,收到通知
 *
 * @throws Exception
 */
@Test(timeout = 4 * TIME_OUT)
public void subscribeAdd() 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, "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 13 with Event

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

the class Subscribe_ITCase method querySubscribers.

@Test
public void querySubscribers() throws Exception {
    String serviceName = randomDomainName();
    naming.registerInstance(serviceName, "1.1.1.1", TEST_PORT, "c1");
    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);
    TimeUnit.SECONDS.sleep(3);
    ResponseEntity<String> response = request(NamingBase.NAMING_CONTROLLER_PATH + "/service/subscribers", Params.newParams().appendParam("serviceName", serviceName).appendParam("pageNo", "1").appendParam("pageSize", "10").done(), String.class, HttpMethod.GET);
    Assert.assertTrue(response.getStatusCode().is2xxSuccessful());
    JsonNode body = JacksonUtils.toObj(response.getBody());
    Assert.assertEquals(1, body.get("subscribers").size());
    Properties properties = new Properties();
    properties.setProperty("namingRequestTimeout", "300000");
    properties.setProperty("serverAddr", "127.0.0.1" + ":" + port);
    NamingService naming2 = NamingFactory.createNamingService(properties);
    naming2.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();
        }
    });
    TimeUnit.SECONDS.sleep(3);
    response = request(NamingBase.NAMING_CONTROLLER_PATH + "/service/subscribers", Params.newParams().appendParam("serviceName", serviceName).appendParam("pageNo", "1").appendParam("pageSize", "10").done(), String.class, HttpMethod.GET);
    Assert.assertTrue(response.getStatusCode().is2xxSuccessful());
    body = JacksonUtils.toObj(response.getBody());
    // server will remove duplicate subscriber by ip port service app and so on
    Assert.assertEquals(1, body.get("subscribers").size());
}
Also used : NamingService(com.alibaba.nacos.api.naming.NamingService) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) Event(com.alibaba.nacos.api.naming.listener.Event) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) JsonNode(com.fasterxml.jackson.databind.JsonNode) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) Properties(java.util.Properties) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 14 with Event

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

the class Subscribe_ITCase method subscribeDelete.

/**
 * 删除IP,收到通知
 *
 * @throws Exception
 */
@Test(timeout = 4 * TIME_OUT)
public void subscribeDelete() throws Exception {
    String serviceName = randomDomainName();
    naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
    TimeUnit.SECONDS.sleep(3);
    naming.subscribe(serviceName, 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();
        }
    });
    TimeUnit.SECONDS.sleep(1);
    naming.deregisterInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
    while (!instances.isEmpty()) {
        Thread.sleep(1000L);
    }
    Assert.assertTrue(instances.isEmpty());
}
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 15 with Event

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

the class Subscribe_ITCase method subscribeUnhealthy.

/**
 * 添加不可用IP,收到通知
 *
 * @throws Exception
 */
@Test(timeout = 4 * TIME_OUT)
public void subscribeUnhealthy() 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)));
}
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)

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