use of io.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent in project java-chassis by ServiceComb.
the class RegistryUtils method watch.
public static void watch() {
if (!needToWatch()) {
return;
}
srClient.watch(getMicroservice().getServiceId(), (event) -> {
if (event.failed()) {
exception(event.cause());
return;
}
MicroserviceInstanceChangedEvent changedEvent = event.result();
if (isProviderInstancesChanged(changedEvent) && !serviceRegistryConfig.isWatch()) {
return;
}
if (!isProviderInstancesChanged(changedEvent) && !serviceRegistryConfig.isRegistryAutoDiscovery()) {
return;
}
InstanceCacheManager.INSTANCE.onInstanceUpdate(changedEvent);
}, open -> {
recover();
}, close -> {
});
}
use of io.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent in project java-chassis by ServiceComb.
the class TestCacheRegistryListener method testonMicroserviceInstanceChangedUPDATE.
@Test
public void testonMicroserviceInstanceChangedUPDATE() {
MicroserviceInstanceChangedEvent changedEvent = new MicroserviceInstanceChangedEvent();
changedEvent.setAction(WatchAction.UPDATE);
MicroserviceKey key = Mockito.mock(MicroserviceKey.class);
changedEvent.setKey(key);
MicroserviceInstance mInstance = Mockito.mock(MicroserviceInstance.class);
changedEvent.setInstance(mInstance);
instance.onMicroserviceInstanceChanged(changedEvent);
}
use of io.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent in project java-chassis by ServiceComb.
the class TestInstanceCacheManager method testInstanceUpdate.
@Test
public void testInstanceUpdate() {
Microservice oInstance = RegistryUtils.getMicroservice();
InstanceCacheManager oInstanceCacheManager = new InstanceCacheManager();
MicroserviceInstanceChangedEvent oChangedEnvent = new MicroserviceInstanceChangedEvent();
oChangedEnvent.setAction(WatchAction.UPDATE);
MicroserviceKey oKey = new MicroserviceKey();
oKey.setAppId(oInstance.getAppId());
oKey.setVersion(oInstance.getVersion());
oKey.setServiceName(oInstance.getServiceName());
oChangedEnvent.setKey(oKey);
oChangedEnvent.setInstance(RegistryUtils.getMicroserviceInstance());
oInstanceCacheManager.onInstanceUpdate(oChangedEnvent);
oChangedEnvent.setAction(WatchAction.DELETE);
oInstanceCacheManager.onInstanceUpdate(oChangedEnvent);
oChangedEnvent.setAction(WatchAction.CREATE);
oInstanceCacheManager.onInstanceUpdate(oChangedEnvent);
Assert.assertEquals("UP", RegistryUtils.getMicroserviceInstance().getStatus().toString());
}
use of io.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent in project java-chassis by ServiceComb.
the class ServiceRegistryClientImpl method watch.
public void watch(String selfMicroserviceId, AsyncResultCallback<MicroserviceInstanceChangedEvent> callback, AsyncResultCallback<Void> onOpen, AsyncResultCallback<Void> onClose) {
Boolean alreadyWatch = watchServices.get(selfMicroserviceId);
if (alreadyWatch == null) {
synchronized (ServiceRegistryClientImpl.class) {
alreadyWatch = watchServices.get(selfMicroserviceId);
if (alreadyWatch == null) {
watchServices.put(selfMicroserviceId, true);
String url = Const.MS_API_PATH + Const.MICROSERVICE_PATH + "/" + selfMicroserviceId + Const.WATCHER_PATH;
IpPort ipPort = IpPortManager.INSTANCE.get();
if (ipPort == null) {
LOGGER.error("request address is null, watch microservice {}", selfMicroserviceId);
watchErrorHandler(new Exception("request address is null"), selfMicroserviceId, callback);
return;
}
WebsocketUtils.open(ipPort, url, o -> {
onOpen.success(o);
LOGGER.info("watching microservice {} successfully, " + "the chosen service center address is {}:{}", selfMicroserviceId, ipPort.getHostOrIp(), ipPort.getPort());
}, c -> {
LOGGER.warn("watching microservice {} connection is closed accidentally", selfMicroserviceId);
watchErrorHandler(new ClientException("connection is closed accidentally"), selfMicroserviceId, callback);
onClose.success(null);
}, bodyBuffer -> {
MicroserviceInstanceChangedEvent response = null;
try {
response = JsonUtils.readValue(bodyBuffer.getBytes(), MicroserviceInstanceChangedEvent.class);
} catch (Exception e) {
LOGGER.error("watcher handle microservice {} response failed, {}", selfMicroserviceId, bodyBuffer.toString());
return;
}
try {
callback.success(response);
} catch (Exception e) {
LOGGER.error("notify watcher failed, microservice {}", selfMicroserviceId, e);
}
}, e -> {
LOGGER.error("watcher read microservice {} message from service center failed," + " {}", selfMicroserviceId, e.getMessage());
}, f -> {
if (!watchServices.containsKey(selfMicroserviceId)) {
return;
}
LOGGER.error("watcher connect to service center server failed, microservice {}, {}", selfMicroserviceId, f.getMessage());
watchErrorHandler(f, selfMicroserviceId, callback);
});
}
}
}
}
use of io.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent in project java-chassis by ServiceComb.
the class TestCacheRegistryListener method testonMicroserviceInstanceChangedCREATE.
@Test
public void testonMicroserviceInstanceChangedCREATE() {
MicroserviceInstanceChangedEvent changedEvent = new MicroserviceInstanceChangedEvent();
changedEvent.setAction(WatchAction.CREATE);
MicroserviceKey key = Mockito.mock(MicroserviceKey.class);
changedEvent.setKey(key);
MicroserviceInstance mInstance = Mockito.mock(MicroserviceInstance.class);
changedEvent.setInstance(mInstance);
instance.onMicroserviceInstanceChanged(changedEvent);
}
Aggregations