use of org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent in project java-chassis by ServiceComb.
the class MicroserviceWatchTask method doRun.
@Override
public void doRun() {
// will always run watch when it is ready
if (!needToWatch()) {
return;
}
srClient.watch(microservice.getServiceId(), (event) -> {
if (event.failed()) {
eventBus.post(new ExceptionEvent(event.cause()));
return;
}
MicroserviceInstanceChangedEvent changedEvent = event.result();
if (isProviderInstancesChanged(changedEvent) && !serviceRegistryConfig.isWatch()) {
return;
}
if (!isProviderInstancesChanged(changedEvent) && !serviceRegistryConfig.isRegistryAutoDiscovery()) {
return;
}
onMicroserviceInstanceChanged(changedEvent);
}, open -> eventBus.post(new RecoveryEvent()), close -> {
});
}
use of org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent in project java-chassis by ServiceComb.
the class TestMicroserviceInstanceChangedEvent method setUp.
@Before
public void setUp() throws Exception {
oMicroserviceInstanceChangedEvent = new MicroserviceInstanceChangedEvent();
oMockMicroserviceKey = Mockito.mock(MicroserviceKey.class);
oMockMicroserviceInstance = Mockito.mock(MicroserviceInstance.class);
}
use of org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent in project java-chassis by ServiceComb.
the class TestConsumers method watchDeleteEvent.
@Test
public void watchDeleteEvent() {
MicroserviceVersionRule microserviceVersionRule = appManager.getOrCreateMicroserviceVersionRule(appId, serviceName, versionRule);
Assert.assertEquals("0.0.0.0+", microserviceVersionRule.getVersionRule().getVersionRule());
Assert.assertEquals(1, microserviceManager.getVersionsByName().size());
mockNotExist();
MicroserviceKey key = new MicroserviceKey();
MicroserviceInstanceChangedEvent event = new MicroserviceInstanceChangedEvent();
event.setKey(key);
key.setAppId(appId);
key.setServiceName(serviceName);
eventBus.post(event);
Assert.assertEquals(0, microserviceManager.getVersionsByName().size());
}
use of org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent in project java-chassis by ServiceComb.
the class TestMicroserviceWatchTask method testWatchInstanceChanged.
@Test
public void testWatchInstanceChanged(@Mocked ServiceRegistryConfig serviceRegistryConfig, @Mocked ServiceRegistryClient srClient, @Mocked Microservice microservice) {
initWatch(serviceRegistryConfig, srClient, microservice);
MicroserviceInstanceChangedEvent changedEvent = new MicroserviceInstanceChangedEvent();
MicroserviceKey key = new MicroserviceKey();
key.setAppId(microservice.getAppId());
key.setVersion(microservice.getVersion());
key.setServiceName(microservice.getServiceName());
changedEvent.setKey(key);
changedEvent.setInstance(microservice.getInstance());
new MockUp<ServiceRegistryClient>(srClient) {
@Mock
void watch(String selfMicroserviceId, AsyncResultCallback<MicroserviceInstanceChangedEvent> callback, AsyncResultCallback<Void> onOpen, AsyncResultCallback<Void> onClose) {
callback.success(changedEvent);
}
};
Holder<MicroserviceInstanceChangedEvent> holder = new Holder<>();
eventBus.register(new Object() {
@Subscribe
public void onException(MicroserviceInstanceChangedEvent event) {
holder.value = event;
}
});
changedEvent.setAction(WatchAction.CREATE);
microserviceWatchTask.run();
Assert.assertEquals(WatchAction.CREATE, holder.value.getAction());
changedEvent.setAction(WatchAction.DELETE);
microserviceWatchTask.run();
Assert.assertEquals(WatchAction.DELETE, holder.value.getAction());
changedEvent.setAction(WatchAction.UPDATE);
microserviceWatchTask.run();
Assert.assertEquals(WatchAction.UPDATE, holder.value.getAction());
}
use of org.apache.servicecomb.registry.api.event.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 = String.format(Const.REGISTRY_API.MICROSERVICE_WATCH, selfMicroserviceId);
IpPort ipPort = ipPortManager.getAvailableAddress();
websocketClientUtil.open(ipPort, url, o -> {
onOpen.success(o);
LOGGER.info("watching microservice {} successfully, " + "the chosen service center address is {}:{}", selfMicroserviceId, ipPort.getHostOrIp(), ipPort.getPort());
}, c -> {
watchErrorHandler(new ClientException("connection is closed accidentally"), selfMicroserviceId, callback);
onClose.success(null);
}, bodyBuffer -> {
MicroserviceInstanceChangedEvent response;
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 -> {
watchErrorHandler(e, selfMicroserviceId, callback);
onClose.success(null);
}, f -> {
watchErrorHandler(f, selfMicroserviceId, callback);
});
}
}
}
}
Aggregations