Search in sources :

Example 1 with Service

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

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

the class CircuitBreakUtils method matchSource.

private static MatchSourceResult matchSource(CbRule rule, RuleIdentifier ruleIdentifier) {
    if (rule.getSourcesCount() == 0) {
        return new MatchSourceResult(true, true);
    }
    Service callerService = ruleIdentifier.getCallerService();
    for (SourceMatcher sourceMatcher : rule.getSourcesList()) {
        boolean matchAllNamespace = sourceMatcher.getNamespace().getValue().equals(matchAll);
        boolean matchAllService = sourceMatcher.getService().getValue().equals(matchAll);
        if (matchAllNamespace && matchAllService) {
            return new MatchSourceResult(true, true);
        }
        if (null == callerService) {
            continue;
        }
        boolean namespaceMatch = matchAllNamespace;
        boolean serviceMatch = matchAllService;
        if (!namespaceMatch) {
            namespaceMatch = sourceMatcher.getNamespace().getValue().equals(callerService.getNamespace());
        }
        if (!serviceMatch) {
            serviceMatch = sourceMatcher.getService().getValue().equals(callerService.getService());
        }
        if (namespaceMatch && serviceMatch) {
            return new MatchSourceResult(true, false);
        }
    }
    return new MatchSourceResult(false, false);
}
Also used : SourceMatcher(com.tencent.polaris.client.pb.CircuitBreakerProto.SourceMatcher) Service(com.tencent.polaris.api.pojo.Service)

Aggregations

Service (com.tencent.polaris.api.pojo.Service)2 DefaultServiceEventKeysProvider (com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider)1 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 ResourcesResponse (com.tencent.polaris.client.flow.ResourcesResponse)1 CircuitBreakerProto (com.tencent.polaris.client.pb.CircuitBreakerProto)1 CbRule (com.tencent.polaris.client.pb.CircuitBreakerProto.CbRule)1 SourceMatcher (com.tencent.polaris.client.pb.CircuitBreakerProto.SourceMatcher)1 HashSet (java.util.HashSet)1