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)));
}
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();
}
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());
}
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();
}
Aggregations