Search in sources :

Example 1 with ServiceInstancesByProto

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

the class Utils method checkUpdateInstances.

public static List<ServiceChangeEvent.OneInstanceUpdate> checkUpdateInstances(ServiceInstancesByProto oldVal, ServiceInstancesByProto newVal) {
    Map<String, Instance> oldIns = oldVal.getInstances().stream().collect(Collectors.toMap(Instance::getId, instance -> instance));
    Map<String, Instance> newIns = newVal.getInstances().stream().collect(Collectors.toMap(Instance::getId, instance -> instance));
    List<ServiceChangeEvent.OneInstanceUpdate> ret = new LinkedList<>();
    oldIns.forEach((id, instance) -> {
        Instance ins = newIns.get(id);
        if (ins == null) {
            return;
        }
        if (!Objects.equals(ins.getRevision(), instance.getRevision())) {
            ret.add(new ServiceChangeEvent.OneInstanceUpdate(instance, ins));
        }
    });
    return ret;
}
Also used : ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto) Level(com.tencent.polaris.api.pojo.StatusDimension.Level) Logger(org.slf4j.Logger) ServiceChangeEvent(com.tencent.polaris.api.pojo.ServiceChangeEvent) CircuitBreakerStatus(com.tencent.polaris.api.pojo.CircuitBreakerStatus) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) StatusDimension(com.tencent.polaris.api.pojo.StatusDimension) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) Objects(java.util.Objects) List(java.util.List) Matcher(java.util.regex.Matcher) Map(java.util.Map) StringUtils(com.tencent.polaris.api.utils.StringUtils) Pattern(java.util.regex.Pattern) LinkedList(java.util.LinkedList) Instance(com.tencent.polaris.api.pojo.Instance) ServiceChangeEvent(com.tencent.polaris.api.pojo.ServiceChangeEvent) Instance(com.tencent.polaris.api.pojo.Instance) LinkedList(java.util.LinkedList)

Example 2 with ServiceInstancesByProto

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

the class ChangeStateUtils method getInstance.

/**
 * 获取实例ID
 *
 * @param instanceGauge 实例统计数据
 * @param localRegistry 本地缓存插件
 * @return ID
 */
public static InstanceByProto getInstance(InstanceGauge instanceGauge, LocalRegistry localRegistry) {
    ServiceEventKey serviceEventKey = new ServiceEventKey(new ServiceKey(instanceGauge.getNamespace(), instanceGauge.getService()), EventType.INSTANCE);
    ResourceFilter resourceFilter = new ResourceFilter(serviceEventKey, true, true);
    ServiceInstances instances = localRegistry.getInstances(resourceFilter);
    if (!instances.isInitialized()) {
        return null;
    }
    ServiceInstancesByProto serviceInstancesByProto = (ServiceInstancesByProto) instances;
    Instance instance = instanceGauge.getInstance();
    if (instance instanceof InstanceByProto) {
        return (InstanceByProto) instance;
    }
    InstanceByProto instanceByProto;
    String instanceId = instanceGauge.getInstanceId();
    if (StringUtils.isNotBlank(instanceId)) {
        instanceByProto = serviceInstancesByProto.getInstance(instanceId);
    } else {
        Node node = new Node(instanceGauge.getHost(), instanceGauge.getPort());
        instanceByProto = serviceInstancesByProto.getInstanceByNode(node);
    }
    if (null != instanceByProto) {
        instanceGauge.setInstance(instanceByProto);
    }
    return instanceByProto;
}
Also used : ResourceFilter(com.tencent.polaris.api.plugin.registry.ResourceFilter) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) InstanceByProto(com.tencent.polaris.client.pojo.InstanceByProto) Instance(com.tencent.polaris.api.pojo.Instance) Node(com.tencent.polaris.client.pojo.Node) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto)

Example 3 with ServiceInstancesByProto

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

the class ServiceInstancesCacheHandler method messageToCacheValue.

@Override
public RegistryCacheValue messageToCacheValue(RegistryCacheValue oldValue, Object newValue, boolean isCacheLoaded) {
    DiscoverResponse discoverResponse = (DiscoverResponse) newValue;
    ServiceInstancesByProto oldServiceInstances = null;
    if (null != oldValue) {
        oldServiceInstances = (ServiceInstancesByProto) oldValue;
    }
    return new ServiceInstancesByProto(discoverResponse, oldServiceInstances, isCacheLoaded);
}
Also used : DiscoverResponse(com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse) ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto)

Example 4 with ServiceInstancesByProto

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

the class InstancesCircuitBreakTask method getInstance.

private Instance getInstance() {
    ServiceEventKey serviceEventKey = new ServiceEventKey(serviceKey, EventType.INSTANCE);
    ResourceFilter resourceFilter = new ResourceFilter(serviceEventKey, true, true);
    ServiceInstancesByProto instances = (ServiceInstancesByProto) extensions.getLocalRegistry().getInstances(resourceFilter);
    if (!instances.isInitialized()) {
        return null;
    }
    return instances.getInstance(instId);
}
Also used : ResourceFilter(com.tencent.polaris.api.plugin.registry.ResourceFilter) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto)

Example 5 with ServiceInstancesByProto

use of com.tencent.polaris.client.pojo.ServiceInstancesByProto 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)

Aggregations

ServiceInstancesByProto (com.tencent.polaris.client.pojo.ServiceInstancesByProto)6 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)4 ResourceFilter (com.tencent.polaris.api.plugin.registry.ResourceFilter)2 Instance (com.tencent.polaris.api.pojo.Instance)2 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)2 ServicesByProto (com.tencent.polaris.client.pojo.ServicesByProto)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Sets (com.google.common.collect.Sets)1 AbstractResourceEventListener (com.tencent.polaris.api.plugin.registry.AbstractResourceEventListener)1 CircuitBreakerStatus (com.tencent.polaris.api.pojo.CircuitBreakerStatus)1 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)1 RegistryCacheValue (com.tencent.polaris.api.pojo.RegistryCacheValue)1 ServiceChangeEvent (com.tencent.polaris.api.pojo.ServiceChangeEvent)1 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)1 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)1 StatusDimension (com.tencent.polaris.api.pojo.StatusDimension)1 Level (com.tencent.polaris.api.pojo.StatusDimension.Level)1