Search in sources :

Example 1 with MicroserviceInstanceChangedEvent

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 -> {
    });
}
Also used : MicroserviceInstanceChangedEvent(io.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent)

Example 2 with MicroserviceInstanceChangedEvent

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);
}
Also used : MicroserviceInstanceChangedEvent(io.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent) MicroserviceKey(io.servicecomb.serviceregistry.api.MicroserviceKey) MicroserviceInstance(io.servicecomb.serviceregistry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 3 with MicroserviceInstanceChangedEvent

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());
}
Also used : MicroserviceInstanceChangedEvent(io.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent) Microservice(io.servicecomb.serviceregistry.api.registry.Microservice) MicroserviceKey(io.servicecomb.serviceregistry.api.MicroserviceKey) Test(org.junit.Test)

Example 4 with MicroserviceInstanceChangedEvent

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);
                });
            }
        }
    }
}
Also used : MicroserviceInstanceChangedEvent(io.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent) IpPort(io.servicecomb.foundation.common.net.IpPort) ClientException(io.servicecomb.serviceregistry.client.ClientException) ClientException(io.servicecomb.serviceregistry.client.ClientException)

Example 5 with MicroserviceInstanceChangedEvent

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);
}
Also used : MicroserviceInstanceChangedEvent(io.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent) MicroserviceKey(io.servicecomb.serviceregistry.api.MicroserviceKey) MicroserviceInstance(io.servicecomb.serviceregistry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Aggregations

MicroserviceInstanceChangedEvent (io.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent)6 MicroserviceKey (io.servicecomb.serviceregistry.api.MicroserviceKey)4 Test (org.junit.Test)4 MicroserviceInstance (io.servicecomb.serviceregistry.api.registry.MicroserviceInstance)3 IpPort (io.servicecomb.foundation.common.net.IpPort)1 Microservice (io.servicecomb.serviceregistry.api.registry.Microservice)1 ClientException (io.servicecomb.serviceregistry.client.ClientException)1