Search in sources :

Example 1 with ServicesByProto

use of com.tencent.polaris.client.pojo.ServicesByProto in project spring-cloud-tencent by Tencent.

the class PolarisServiceStatusChangeListener method onResourceUpdated.

@Override
public void onResourceUpdated(ServiceEventKey svcEventKey, RegistryCacheValue oldValue, RegistryCacheValue newValue) {
    if (newValue.getEventType() == ServiceEventKey.EventType.SERVICE) {
        if (oldValue instanceof ServicesByProto && newValue instanceof ServicesByProto) {
            LOG.debug("receive service={} change event", svcEventKey);
            Set<String> oldServiceInfoSet = ((ServicesByProto) oldValue).getServices().stream().map(i -> i.getNamespace() + "::" + i.getService()).collect(Collectors.toSet());
            Set<String> newServiceInfoSet = ((ServicesByProto) newValue).getServices().stream().map(i -> i.getNamespace() + "::" + i.getService()).collect(Collectors.toSet());
            Sets.SetView<String> addServiceInfoSetView = Sets.difference(newServiceInfoSet, oldServiceInfoSet);
            Sets.SetView<String> deleteServiceInfoSetView = Sets.difference(oldServiceInfoSet, newServiceInfoSet);
            if (addServiceInfoSetView.isEmpty() && deleteServiceInfoSetView.isEmpty()) {
                return;
            }
            LOG.info("Service status is update. Add service of {}. Delete service of {}", addServiceInfoSetView, deleteServiceInfoSetView);
            // Trigger reload of gateway route cache.
            this.publisher.publishEvent(new HeartbeatEvent(this, INDEX.getAndIncrement()));
        }
    } else if (newValue.getEventType() == ServiceEventKey.EventType.INSTANCE) {
        if (oldValue instanceof ServiceInstancesByProto && newValue instanceof ServiceInstancesByProto) {
            LOG.debug("receive service instances={} change event", svcEventKey);
            ServiceInstancesByProto oldIns = (ServiceInstancesByProto) oldValue;
            ServiceInstancesByProto newIns = (ServiceInstancesByProto) newValue;
            if ((CollectionUtils.isEmpty(oldIns.getInstances()) && !CollectionUtils.isEmpty(newIns.getInstances())) || (!CollectionUtils.isEmpty(oldIns.getInstances()) && CollectionUtils.isEmpty(newIns.getInstances()))) {
                LOG.info("Service status of {} is update.", newIns.getService());
                // Trigger reload of gateway route cache.
                this.publisher.publishEvent(new HeartbeatEvent(this, INDEX.getAndIncrement()));
            }
        }
    }
}
Also used : ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) AbstractResourceEventListener(com.tencent.polaris.api.plugin.registry.AbstractResourceEventListener) ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto) ServicesByProto(com.tencent.polaris.client.pojo.ServicesByProto) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) HeartbeatEvent(org.springframework.cloud.client.discovery.event.HeartbeatEvent) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) AtomicLong(java.util.concurrent.atomic.AtomicLong) RegistryCacheValue(com.tencent.polaris.api.pojo.RegistryCacheValue) CollectionUtils(org.springframework.util.CollectionUtils) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) HeartbeatEvent(org.springframework.cloud.client.discovery.event.HeartbeatEvent) Sets(com.google.common.collect.Sets) ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto) ServicesByProto(com.tencent.polaris.client.pojo.ServicesByProto)

Example 2 with ServicesByProto

use of com.tencent.polaris.client.pojo.ServicesByProto in project spring-cloud-tencent by Tencent.

the class PolarisServiceStatusChangeListenerTest method testOnResourceUpdated.

@Test
public void testOnResourceUpdated() {
    PolarisServiceStatusChangeListener polarisServiceStatusChangeListener = new PolarisServiceStatusChangeListener();
    polarisServiceStatusChangeListener.setApplicationEventPublisher(publisher);
    // Service update event
    ServiceEventKey serviceUpdateEventKey = new ServiceEventKey(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER), ServiceEventKey.EventType.SERVICE);
    ServiceInfo serviceInfo = new ServiceInfo();
    serviceInfo.setNamespace(NAMESPACE_TEST);
    serviceInfo.setService(SERVICE_PROVIDER);
    // Need update
    ServicesByProto oldServices = new ServicesByProto(Collections.emptyList());
    ServicesByProto newServices = new ServicesByProto(Collections.singletonList(serviceInfo));
    polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldServices, newServices);
    verify(publisher, times(1)).publishEvent(any(ApplicationEvent.class));
    // No need update
    oldServices = new ServicesByProto(Collections.singletonList(serviceInfo));
    newServices = new ServicesByProto(Collections.singletonList(serviceInfo));
    polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldServices, newServices);
    verify(publisher, times(1)).publishEvent(any(ApplicationEvent.class));
    // Instance update event
    ServiceEventKey instanceUpdateEventKey = new ServiceEventKey(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER), ServiceEventKey.EventType.INSTANCE);
    DefaultInstance instance = new DefaultInstance();
    instance.setNamespace(NAMESPACE_TEST);
    instance.setService(SERVICE_PROVIDER);
    instance.setHost(HOST);
    instance.setPort(PORT);
    try {
        Field instances = ServiceInstancesByProto.class.getDeclaredField("instances");
        instances.setAccessible(true);
        // Need update
        ServiceInstancesByProto oldInstances = new ServiceInstancesByProto();
        instances.set(oldInstances, Collections.emptyList());
        ServiceInstancesByProto newInstances = new ServiceInstancesByProto();
        instances.set(newInstances, Collections.singletonList(instance));
        polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldInstances, newInstances);
        verify(publisher, times(2)).publishEvent(any(ApplicationEvent.class));
        // No need update
        oldInstances = new ServiceInstancesByProto();
        instances.set(oldInstances, Collections.singletonList(instance));
        newInstances = new ServiceInstancesByProto();
        instances.set(newInstances, Collections.singletonList(instance));
        polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldInstances, newInstances);
        verify(publisher, times(2)).publishEvent(any(ApplicationEvent.class));
    } catch (NoSuchFieldException | IllegalAccessException e) {
        Assertions.fail("Exception encountered.", e);
    }
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) Field(java.lang.reflect.Field) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) ApplicationEvent(org.springframework.context.ApplicationEvent) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto) ServicesByProto(com.tencent.polaris.client.pojo.ServicesByProto) Test(org.junit.Test)

Example 3 with ServicesByProto

use of com.tencent.polaris.client.pojo.ServicesByProto in project polaris-java by polarismesh.

the class ConsulAPIConnector method syncGetServices.

@Override
public Services syncGetServices(ServiceUpdateTask serviceUpdateTask) {
    Services services = new ServicesByProto(new ArrayList<>());
    try {
        CatalogServicesRequest request = CatalogServicesRequest.newBuilder().setQueryParams(QueryParams.DEFAULT).build();
        ArrayList<String> serviceList = new ArrayList<>(this.consulClient.getCatalogServices(request).getValue().keySet());
        for (String s : serviceList) {
            ServiceInfo serviceInfo = new ServiceInfo();
            serviceInfo.setService(s);
            services.getServices().add(serviceInfo);
        }
    } catch (ConsulException e) {
        throw ServerErrorResponseException.build(ErrorCode.SERVER_USER_ERROR.ordinal(), String.format("Get services of %s instances sync failed.", serviceUpdateTask.getServiceEventKey().getServiceKey()));
    }
    return services;
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) Services(com.tencent.polaris.api.pojo.Services) ConsulException(com.ecwid.consul.ConsulException) CatalogServicesRequest(com.ecwid.consul.v1.catalog.CatalogServicesRequest) ArrayList(java.util.ArrayList) ServicesByProto(com.tencent.polaris.client.pojo.ServicesByProto)

Aggregations

ServicesByProto (com.tencent.polaris.client.pojo.ServicesByProto)3 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)2 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)2 ServiceInstancesByProto (com.tencent.polaris.client.pojo.ServiceInstancesByProto)2 ConsulException (com.ecwid.consul.ConsulException)1 CatalogServicesRequest (com.ecwid.consul.v1.catalog.CatalogServicesRequest)1 Sets (com.google.common.collect.Sets)1 AbstractResourceEventListener (com.tencent.polaris.api.plugin.registry.AbstractResourceEventListener)1 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)1 RegistryCacheValue (com.tencent.polaris.api.pojo.RegistryCacheValue)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 Services (com.tencent.polaris.api.pojo.Services)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Collectors (java.util.stream.Collectors)1 Test (org.junit.Test)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1