Search in sources :

Example 6 with ResourcesResponse

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

the class DefaultRouterAPI method processRouters.

@Override
public ProcessRoutersResponse processRouters(ProcessRoutersRequest request) throws PolarisException {
    checkAvailable("RouterAPI");
    RouterValidator.validateProcessRouterRequest(request);
    RouterNamesGroup requestRouterGroup = request.getRouters();
    List<ServiceRouter> beforeRouters;
    if (null != requestRouterGroup && CollectionUtils.isNotEmpty(requestRouterGroup.getBeforeRouters())) {
        beforeRouters = Extensions.loadServiceRouters(requestRouterGroup.getBeforeRouters(), extensions.getPlugins(), true);
    } else {
        beforeRouters = extensions.getConfigRouterChainGroup().getBeforeRouters();
    }
    List<ServiceRouter> afterRouters;
    if (null != requestRouterGroup && CollectionUtils.isNotEmpty(requestRouterGroup.getAfterRouters())) {
        afterRouters = Extensions.loadServiceRouters(requestRouterGroup.getAfterRouters(), extensions.getPlugins(), true);
    } else {
        afterRouters = extensions.getConfigRouterChainGroup().getAfterRouters();
    }
    List<ServiceRouter> coreRouters;
    if (null != requestRouterGroup && CollectionUtils.isNotEmpty(requestRouterGroup.getCoreRouters())) {
        coreRouters = Extensions.loadServiceRouters(requestRouterGroup.getCoreRouters(), extensions.getPlugins(), true);
    } else {
        coreRouters = extensions.getConfigRouterChainGroup().getCoreRouters();
    }
    ServiceInstances dstInstances = request.getDstInstances();
    RouteInfo routeInfo = new RouteInfo(request.getSourceService(), dstInstances, request.getMethod());
    // 获取路由规则
    DefaultFlowControlParam engineFlowControlParam = new DefaultFlowControlParam();
    BaseFlow.buildFlowControlParam(request, config, engineFlowControlParam);
    Set<ServiceEventKey> routerKeys = new HashSet<>();
    ServiceEventKey dstSvcEventKey = new ServiceEventKey(new ServiceKey(dstInstances.getNamespace(), dstInstances.getService()), EventType.ROUTING);
    routerKeys.add(dstSvcEventKey);
    ServiceEventKey srcSvcEventKey = null;
    if (null != routeInfo.getSourceService() && StringUtils.isNotBlank(routeInfo.getSourceService().getNamespace()) && StringUtils.isNotBlank(routeInfo.getSourceService().getService())) {
        srcSvcEventKey = new ServiceEventKey(new ServiceKey(routeInfo.getSourceService().getNamespace(), routeInfo.getSourceService().getService()), EventType.ROUTING);
        routerKeys.add(srcSvcEventKey);
    }
    DefaultServiceEventKeysProvider svcKeysProvider = new DefaultServiceEventKeysProvider();
    svcKeysProvider.setSvcEventKeys(routerKeys);
    ResourcesResponse resourcesResponse = BaseFlow.syncGetResources(extensions, false, svcKeysProvider, engineFlowControlParam);
    routeInfo.setDestRouteRule(resourcesResponse.getServiceRule(dstSvcEventKey));
    if (null != srcSvcEventKey) {
        routeInfo.setSourceRouteRule(resourcesResponse.getServiceRule(srcSvcEventKey));
    }
    // 执行路由
    DefaultRouterChainGroup routerChainGroup = new DefaultRouterChainGroup(beforeRouters, coreRouters, afterRouters);
    ServiceInstances svcInstances = BaseFlow.processServiceRouters(routeInfo, dstInstances, routerChainGroup);
    return new ProcessRoutersResponse(svcInstances);
}
Also used : ServiceRouter(com.tencent.polaris.api.plugin.route.ServiceRouter) DefaultServiceEventKeysProvider(com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider) RouterNamesGroup(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest.RouterNamesGroup) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ResourcesResponse(com.tencent.polaris.client.flow.ResourcesResponse) ProcessRoutersResponse(com.tencent.polaris.router.api.rpc.ProcessRoutersResponse) DefaultFlowControlParam(com.tencent.polaris.client.flow.DefaultFlowControlParam) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) RouteInfo(com.tencent.polaris.api.plugin.route.RouteInfo) DefaultRouterChainGroup(com.tencent.polaris.api.plugin.compose.DefaultRouterChainGroup) HashSet(java.util.HashSet)

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