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);
}
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);
}
Aggregations