Search in sources :

Example 1 with MicroserviceInstanceChangedEvent

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 -> {
    });
}
Also used : ExceptionEvent(org.apache.servicecomb.serviceregistry.event.ExceptionEvent) MicroserviceInstanceChangedEvent(org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent) RecoveryEvent(org.apache.servicecomb.serviceregistry.event.RecoveryEvent)

Example 2 with MicroserviceInstanceChangedEvent

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);
}
Also used : MicroserviceInstanceChangedEvent(org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent) MicroserviceKey(org.apache.servicecomb.registry.api.MicroserviceKey) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Before(org.junit.Before)

Example 3 with MicroserviceInstanceChangedEvent

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());
}
Also used : MicroserviceInstanceChangedEvent(org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent) MicroserviceVersionRule(org.apache.servicecomb.registry.consumer.MicroserviceVersionRule) MicroserviceKey(org.apache.servicecomb.registry.api.MicroserviceKey) Test(org.junit.Test)

Example 4 with MicroserviceInstanceChangedEvent

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());
}
Also used : MicroserviceInstanceChangedEvent(org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent) MicroserviceKey(org.apache.servicecomb.registry.api.MicroserviceKey) Holder(org.apache.servicecomb.foundation.common.Holder) MockUp(mockit.MockUp) Subscribe(com.google.common.eventbus.Subscribe) AsyncResultCallback(org.apache.servicecomb.foundation.vertx.AsyncResultCallback) Test(org.junit.Test)

Example 5 with MicroserviceInstanceChangedEvent

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);
                });
            }
        }
    }
}
Also used : MicroserviceInstanceChangedEvent(org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

MicroserviceInstanceChangedEvent (org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent)5 MicroserviceKey (org.apache.servicecomb.registry.api.MicroserviceKey)3 Test (org.junit.Test)2 Subscribe (com.google.common.eventbus.Subscribe)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 MockUp (mockit.MockUp)1 Holder (org.apache.servicecomb.foundation.common.Holder)1 IpPort (org.apache.servicecomb.foundation.common.net.IpPort)1 AsyncResultCallback (org.apache.servicecomb.foundation.vertx.AsyncResultCallback)1 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)1 MicroserviceVersionRule (org.apache.servicecomb.registry.consumer.MicroserviceVersionRule)1 ClientException (org.apache.servicecomb.serviceregistry.client.ClientException)1 ExceptionEvent (org.apache.servicecomb.serviceregistry.event.ExceptionEvent)1 RecoveryEvent (org.apache.servicecomb.serviceregistry.event.RecoveryEvent)1 Before (org.junit.Before)1