Search in sources :

Example 1 with ResourcesResponse

use of com.tencent.polaris.client.flow.ResourcesResponse 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 ResourcesResponse

use of com.tencent.polaris.client.flow.ResourcesResponse in project polaris-java by polarismesh.

the class SyncFlow method syncGetServiceInstances.

/**
 * 获取服务实例以及路由数据
 *
 * @param request 请求对象
 * @throws PolarisException 异常
 */
private void syncGetServiceInstances(CommonInstancesRequest request) throws PolarisException {
    ResourcesResponse resourcesResponse = BaseFlow.syncGetResources(extensions, false, request, request);
    request.setDstInstances(resourcesResponse.getServiceInstances(request.getDstInstanceEventKey()));
    if (null != request.getDstRuleEventKey()) {
        request.getRouteInfo().setDestRouteRule(resourcesResponse.getServiceRule(request.getDstRuleEventKey()));
    }
    if (null != request.getSrcRuleEventKey()) {
        request.getRouteInfo().setSourceRouteRule(resourcesResponse.getServiceRule(request.getSrcRuleEventKey()));
    }
}
Also used : ResourcesResponse(com.tencent.polaris.client.flow.ResourcesResponse)

Example 3 with ResourcesResponse

use of com.tencent.polaris.client.flow.ResourcesResponse 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 4 with ResourcesResponse

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

Example 5 with ResourcesResponse

use of com.tencent.polaris.client.flow.ResourcesResponse in project polaris-java by polarismesh.

the class QuotaFlow method lookupRateLimitWindow.

private RateLimitWindow lookupRateLimitWindow(CommonQuotaRequest request) throws PolarisException {
    // 1.获取限流规则
    ResourcesResponse resourcesResponse = BaseFlow.syncGetResources(rateLimitExtension.getExtensions(), false, request, request.getFlowControlParam());
    ServiceRule serviceRule = resourcesResponse.getServiceRule(request.getSvcEventKey());
    // 2.进行规则匹配
    Rule rule = lookupRule(serviceRule, request.getLabels());
    if (null == rule) {
        return null;
    }
    request.setTargetRule(rule);
    // 3.获取已有的限流窗口
    ServiceKey serviceKey = request.getSvcEventKey().getServiceKey();
    String labelsStr = formatLabelsToStr(request);
    RateLimitWindowSet rateLimitWindowSet = getRateLimitWindowSet(serviceKey);
    RateLimitWindow rateLimitWindow = rateLimitWindowSet.getRateLimitWindow(rule, labelsStr);
    if (null != rateLimitWindow) {
        return rateLimitWindow;
    }
    // 3.创建限流窗口
    return rateLimitWindowSet.addRateLimitWindow(request, labelsStr);
}
Also used : ServiceRule(com.tencent.polaris.api.pojo.ServiceRule) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) Rule(com.tencent.polaris.client.pb.RateLimitProto.Rule) ServiceRule(com.tencent.polaris.api.pojo.ServiceRule) MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) ResourcesResponse(com.tencent.polaris.client.flow.ResourcesResponse)

Aggregations

ResourcesResponse (com.tencent.polaris.client.flow.ResourcesResponse)6 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)5 DefaultServiceEventKeysProvider (com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider)4 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)4 ServiceRule (com.tencent.polaris.api.pojo.ServiceRule)3 HashSet (java.util.HashSet)3 DefaultRouterChainGroup (com.tencent.polaris.api.plugin.compose.DefaultRouterChainGroup)1 RouteInfo (com.tencent.polaris.api.plugin.route.RouteInfo)1 ServiceRouter (com.tencent.polaris.api.plugin.route.ServiceRouter)1 Service (com.tencent.polaris.api.pojo.Service)1 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)1 DefaultFlowControlParam (com.tencent.polaris.client.flow.DefaultFlowControlParam)1 CircuitBreakerProto (com.tencent.polaris.client.pb.CircuitBreakerProto)1 CbRule (com.tencent.polaris.client.pb.CircuitBreakerProto.CbRule)1 MatchString (com.tencent.polaris.client.pb.ModelProto.MatchString)1 Rule (com.tencent.polaris.client.pb.RateLimitProto.Rule)1 RoutingProto (com.tencent.polaris.client.pb.RoutingProto)1 RouterNamesGroup (com.tencent.polaris.router.api.rpc.ProcessRoutersRequest.RouterNamesGroup)1 ProcessRoutersResponse (com.tencent.polaris.router.api.rpc.ProcessRoutersResponse)1 ArrayList (java.util.ArrayList)1