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