Search in sources :

Example 11 with ServiceEventKey

use of com.tencent.polaris.api.pojo.ServiceEventKey in project polaris-java by polarismesh.

the class CircuitBreakUtils method getRuleDestinationSet.

/**
 * 通过配置获取远程规则
 *
 * @param ruleIdentifier 规则标识
 * @param extensions 插件集合
 * @param controlParam 控制参数
 * @return 过滤信息
 */
public static RuleDestinationResult getRuleDestinationSet(RuleIdentifier ruleIdentifier, Extensions extensions, FlowControlParam controlParam) {
    Set<ServiceEventKey> svcEventKeys = new HashSet<>();
    ServiceEventKey dstSvcEventKey = new ServiceEventKey(new ServiceKey(ruleIdentifier.getNamespace(), ruleIdentifier.getService()), EventType.CIRCUIT_BREAKING);
    svcEventKeys.add(dstSvcEventKey);
    ServiceEventKey srcSvcEventKey = null;
    Service callerService = ruleIdentifier.getCallerService();
    if (null != callerService && StringUtils.isNotBlank(callerService.getNamespace()) && StringUtils.isNotBlank(callerService.getService())) {
        srcSvcEventKey = new ServiceEventKey(new ServiceKey(callerService.getNamespace(), callerService.getService()), EventType.CIRCUIT_BREAKING);
        svcEventKeys.add(srcSvcEventKey);
    }
    DefaultServiceEventKeysProvider serviceEventKeysProvider = new DefaultServiceEventKeysProvider();
    serviceEventKeysProvider.setSvcEventKeys(svcEventKeys);
    serviceEventKeysProvider.setUseCache(true);
    ResourcesResponse resourcesResponse = BaseFlow.syncGetResources(extensions, false, serviceEventKeysProvider, controlParam);
    CircuitBreakerProto.CircuitBreaker dstRule = getCircuitBreakerRule(resourcesResponse.getServiceRule(dstSvcEventKey));
    CircuitBreakerProto.CircuitBreaker srcRule = null;
    if (null != srcSvcEventKey) {
        srcRule = getCircuitBreakerRule(resourcesResponse.getServiceRule(srcSvcEventKey));
    }
    List<CbRule> rules = getRules(dstRule, srcRule);
    if (CollectionUtils.isEmpty(rules)) {
        return new RuleDestinationResult(null, false, false);
    }
    for (CbRule rule : rules) {
        MatchSourceResult matchSourceResult = matchSource(rule, ruleIdentifier);
        if (!matchSourceResult.matched) {
            continue;
        }
        MatchDestResult matchDestResult = matchDestination(rule, ruleIdentifier, extensions.getFlowCache());
        if (null == matchDestResult.destinationSet) {
            continue;
        }
        return new RuleDestinationResult(matchDestResult.destinationSet, matchSourceResult.allSourcesMatched, matchDestResult.allMethod);
    }
    return new RuleDestinationResult(null, false, false);
}
Also used : DefaultServiceEventKeysProvider(com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) Service(com.tencent.polaris.api.pojo.Service) ResourcesResponse(com.tencent.polaris.client.flow.ResourcesResponse) CircuitBreakerProto(com.tencent.polaris.client.pb.CircuitBreakerProto) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) CbRule(com.tencent.polaris.client.pb.CircuitBreakerProto.CbRule) HashSet(java.util.HashSet)

Example 12 with ServiceEventKey

use of com.tencent.polaris.api.pojo.ServiceEventKey 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 13 with ServiceEventKey

use of com.tencent.polaris.api.pojo.ServiceEventKey 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 14 with ServiceEventKey

use of com.tencent.polaris.api.pojo.ServiceEventKey in project spring-cloud-tencent by Tencent.

the class ServiceRuleManager method getServiceRouterRule.

public List<RoutingProto.Route> getServiceRouterRule(String namespace, String sourceService, String dstService) {
    Set<ServiceEventKey> routerKeys = new HashSet<>();
    ServiceEventKey dstSvcEventKey = new ServiceEventKey(new ServiceKey(namespace, dstService), ServiceEventKey.EventType.ROUTING);
    routerKeys.add(dstSvcEventKey);
    ServiceEventKey srcSvcEventKey = new ServiceEventKey(new ServiceKey(namespace, sourceService), ServiceEventKey.EventType.ROUTING);
    routerKeys.add(srcSvcEventKey);
    DefaultServiceEventKeysProvider svcKeysProvider = new DefaultServiceEventKeysProvider();
    svcKeysProvider.setSvcEventKeys(routerKeys);
    ResourcesResponse resourcesResponse = BaseFlow.syncGetResources(sdkContext.getExtensions(), true, svcKeysProvider, controlParam);
    List<RoutingProto.Route> rules = new ArrayList<>();
    // get source service outbound rules.
    ServiceRule sourceServiceRule = resourcesResponse.getServiceRule(srcSvcEventKey);
    if (sourceServiceRule != null) {
        Object rule = sourceServiceRule.getRule();
        if (rule instanceof RoutingProto.Routing) {
            rules.addAll(((RoutingProto.Routing) rule).getOutboundsList());
        }
    }
    // get peer service inbound rules.
    ServiceRule dstServiceRule = resourcesResponse.getServiceRule(dstSvcEventKey);
    if (dstServiceRule != null) {
        Object rule = dstServiceRule.getRule();
        if (rule instanceof RoutingProto.Routing) {
            rules.addAll(((RoutingProto.Routing) rule).getInboundsList());
        }
    }
    return rules;
}
Also used : DefaultServiceEventKeysProvider(com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider) ArrayList(java.util.ArrayList) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceRule(com.tencent.polaris.api.pojo.ServiceRule) ResourcesResponse(com.tencent.polaris.client.flow.ResourcesResponse) RoutingProto(com.tencent.polaris.client.pb.RoutingProto) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) HashSet(java.util.HashSet)

Example 15 with ServiceEventKey

use of com.tencent.polaris.api.pojo.ServiceEventKey in project spring-cloud-tencent by Tencent.

the class ServiceRuleManager method getServiceRateLimitRule.

public RateLimitProto.RateLimit getServiceRateLimitRule(String namespace, String service) {
    ServiceEventKey serviceEventKey = new ServiceEventKey(new ServiceKey(namespace, service), ServiceEventKey.EventType.RATE_LIMITING);
    DefaultServiceEventKeysProvider svcKeysProvider = new DefaultServiceEventKeysProvider();
    svcKeysProvider.setSvcEventKey(serviceEventKey);
    ResourcesResponse resourcesResponse = BaseFlow.syncGetResources(sdkContext.getExtensions(), true, svcKeysProvider, controlParam);
    ServiceRule serviceRule = resourcesResponse.getServiceRule(serviceEventKey);
    if (serviceRule != null) {
        Object rule = serviceRule.getRule();
        if (rule instanceof RateLimitProto.RateLimit) {
            return (RateLimitProto.RateLimit) rule;
        }
    }
    return null;
}
Also used : DefaultServiceEventKeysProvider(com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceRule(com.tencent.polaris.api.pojo.ServiceRule) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) ResourcesResponse(com.tencent.polaris.client.flow.ResourcesResponse)

Aggregations

ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)23 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)11 DefaultServiceEventKeysProvider (com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider)5 HashMap (java.util.HashMap)5 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)4 ResourcesResponse (com.tencent.polaris.client.flow.ResourcesResponse)4 ServiceInstancesByProto (com.tencent.polaris.client.pojo.ServiceInstancesByProto)4 PolarisException (com.tencent.polaris.api.exception.PolarisException)3 ResourceFilter (com.tencent.polaris.api.plugin.registry.ResourceFilter)3 RegistryCacheValue (com.tencent.polaris.api.pojo.RegistryCacheValue)3 Message (com.google.protobuf.Message)2 DefaultRouterChainGroup (com.tencent.polaris.api.plugin.compose.DefaultRouterChainGroup)2 CachedStatus (com.tencent.polaris.api.plugin.registry.CacheHandler.CachedStatus)2 RouteInfo (com.tencent.polaris.api.plugin.route.RouteInfo)2 ServiceRouter (com.tencent.polaris.api.plugin.route.ServiceRouter)2 Instance (com.tencent.polaris.api.pojo.Instance)2 EventType (com.tencent.polaris.api.pojo.ServiceEventKey.EventType)2 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)2 ServiceRule (com.tencent.polaris.api.pojo.ServiceRule)2 ServiceProto (com.tencent.polaris.client.pb.ServiceProto)2