Search in sources :

Example 6 with ServiceRule

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

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

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

ServiceRule (com.tencent.polaris.api.pojo.ServiceRule)8 ResourceFilter (com.tencent.polaris.api.plugin.registry.ResourceFilter)3 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)3 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)3 Services (com.tencent.polaris.api.pojo.Services)3 ResourcesResponse (com.tencent.polaris.client.flow.ResourcesResponse)3 MatchString (com.tencent.polaris.client.pb.ModelProto.MatchString)3 Rule (com.tencent.polaris.client.pb.RateLimitProto.Rule)3 DefaultServiceEventKeysProvider (com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider)2 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)2 RateLimit (com.tencent.polaris.client.pb.RateLimitProto.RateLimit)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 LocalRegistry (com.tencent.polaris.api.plugin.registry.LocalRegistry)1 RoutingProto (com.tencent.polaris.client.pb.RoutingProto)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1