Search in sources :

Example 21 with Event

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

the class NacosNamingServiceTest method testSubscribe1.

@Test
public void testSubscribe1() throws NacosException {
    // given
    String serviceName = "service1";
    EventListener listener = new EventListener() {

        @Override
        public void onEvent(Event event) {
        }
    };
    // when
    client.subscribe(serviceName, listener);
    // then
    verify(changeNotifier, times(1)).registerListener(Constants.DEFAULT_GROUP, serviceName, "", listener);
    verify(proxy, times(1)).subscribe(serviceName, Constants.DEFAULT_GROUP, "");
}
Also used : Event(com.alibaba.nacos.api.naming.listener.Event) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) Test(org.junit.Test)

Example 22 with Event

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

the class NamingExample method main.

public static void main(String[] args) throws NacosException, InterruptedException {
    Properties properties = new Properties();
    properties.setProperty("serverAddr", System.getProperty("serverAddr"));
    properties.setProperty("namespace", System.getProperty("namespace"));
    NamingService naming = NamingFactory.createNamingService(properties);
    naming.registerInstance("nacos.test.3", "11.11.11.11", 8888, "TEST1");
    System.out.println("instances after register: " + naming.getAllInstances("nacos.test.3"));
    Executor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            thread.setName("test-thread");
            return thread;
        }
    });
    naming.subscribe("nacos.test.3", new AbstractEventListener() {

        // EventListener onEvent is sync to handle, If process too low in onEvent, maybe block other onEvent callback.
        // So you can override getExecutor() to async handle event.
        @Override
        public Executor getExecutor() {
            return executor;
        }

        @Override
        public void onEvent(Event event) {
            System.out.println("serviceName: " + ((NamingEvent) event).getServiceName());
            System.out.println("instances from event: " + ((NamingEvent) event).getInstances());
        }
    });
    naming.deregisterInstance("nacos.test.3", "11.11.11.11", 8888, "TEST1");
    Thread.sleep(1000);
    System.out.println("instances after deregister: " + naming.getAllInstances("nacos.test.3"));
    Thread.sleep(1000);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) Executor(java.util.concurrent.Executor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) NamingService(com.alibaba.nacos.api.naming.NamingService) Event(com.alibaba.nacos.api.naming.listener.Event) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) AbstractEventListener(com.alibaba.nacos.api.naming.listener.AbstractEventListener) Properties(java.util.Properties)

Example 23 with Event

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

the class MultiTenant_ITCase method multipleTenant_unSubscribe.

/**
 * @TCDescription : 多租户取消订阅服务
 * @TestStep :
 * @ExpectResult :
 */
@Test
public void multipleTenant_unSubscribe() throws Exception {
    String serviceName = randomDomainName();
    EventListener listener = new EventListener() {

        @Override
        public void onEvent(Event event) {
            System.out.println(((NamingEvent) event).getServiceName());
            instances = ((NamingEvent) event).getInstances();
        }
    };
    naming1.subscribe(serviceName, listener);
    naming1.registerInstance(serviceName, "11.11.11.11", TEST_PORT, "c1");
    naming2.registerInstance(serviceName, "33.33.33.33", TEST_PORT, "c1");
    while (instances.size() == 0) {
        TimeUnit.SECONDS.sleep(1L);
    }
    Assert.assertEquals(serviceName, naming1.getSubscribeServices().get(0).getName());
    Assert.assertEquals(0, naming2.getSubscribeServices().size());
    naming1.unsubscribe(serviceName, listener);
    TimeUnit.SECONDS.sleep(5L);
    Assert.assertEquals(0, naming1.getSubscribeServices().size());
    Assert.assertEquals(0, naming2.getSubscribeServices().size());
}
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)

Example 24 with Event

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

the class ServiceListTest_ITCase method getSubscribeServices_deregisterInstance.

/**
 * @throws NacosException
 * @description 删除注册,获取当前订阅的所有服务
 */
@Test
public void getSubscribeServices_deregisterInstance() throws NacosException, InterruptedException {
    listenseCount = 0;
    EventListener listener = new EventListener() {

        @Override
        public void onEvent(Event event) {
            System.out.println(((NamingEvent) event).getServiceName());
            System.out.println(((NamingEvent) event).getInstances());
            listenseCount++;
        }
    };
    List<ServiceInfo> serviceInfoList = naming.getSubscribeServices();
    int count = serviceInfoList.size();
    String serviceName = randomDomainName();
    naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
    naming.subscribe(serviceName, listener);
    serviceInfoList = naming.getSubscribeServices();
    Assert.assertEquals(count + 1, serviceInfoList.size());
    naming.deregisterInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
    Assert.assertEquals(count + 1, serviceInfoList.size());
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) 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 25 with Event

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

the class ServiceListTest_ITCase method getSubscribeServices.

/**
 * @throws NacosException
 * @description 获取当前订阅的所有服务
 */
@Test
public void getSubscribeServices() throws NacosException, InterruptedException {
    ListView<String> listView = naming.getServicesOfServer(1, 10);
    if (listView != null && listView.getCount() > 0) {
        naming.getAllInstances(listView.getData().get(0));
    }
    List<ServiceInfo> serviceInfoList = naming.getSubscribeServices();
    int count = serviceInfoList.size();
    String serviceName = randomDomainName();
    naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
    naming.subscribe(serviceName, new EventListener() {

        @Override
        public void onEvent(Event event) {
        }
    });
    serviceInfoList = naming.getSubscribeServices();
    Assert.assertEquals(count + 1, serviceInfoList.size());
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) 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