Search in sources :

Example 1 with ServiceRouter

use of com.tencent.polaris.api.plugin.route.ServiceRouter in project polaris-java by polarismesh.

the class BaseFlow method processRouterChain.

private static boolean processRouterChain(List<ServiceRouter> routers, RouteInfo routeInfo, ServiceInstancesWrap serviceInstances) throws PolarisException {
    if (CollectionUtils.isEmpty(routers)) {
        return false;
    }
    boolean processed = false;
    for (ServiceRouter router : routers) {
        if (CollectionUtils.isEmpty(serviceInstances.getInstances())) {
            // 实例为空,则退出路由
            break;
        }
        if (!router.enable(routeInfo, serviceInstances)) {
            continue;
        }
        processed = true;
        do {
            RouteResult filteredInstances = router.getFilteredInstances(routeInfo, serviceInstances);
            RouteResult.NextRouterInfo nextRouterInfo = filteredInstances.getNextRouterInfo();
            if (nextRouterInfo.getState() == RouteResult.State.Next) {
                serviceInstances.setInstances(filteredInstances.getInstances());
                break;
            }
            // 重试获取
            routeInfo.setNextRouterInfo(nextRouterInfo);
        } while (true);
    }
    return processed;
}
Also used : ServiceRouter(com.tencent.polaris.api.plugin.route.ServiceRouter) RouteResult(com.tencent.polaris.api.plugin.route.RouteResult)

Example 2 with ServiceRouter

use of com.tencent.polaris.api.plugin.route.ServiceRouter in project polaris-java by polarismesh.

the class BaseFlow method commonGetOneInstance.

/**
 * 通用获取单个服务实例的方法,用于SDK内部调用
 *
 * @param extensions 插件上下文
 * @param serviceKey 服务信息
 * @param coreRouterNames 核心路由插件链
 * @param lbPolicy 负载均衡策略
 * @param protocol 协议信息
 * @param hashKey 一致性hash的key
 * @return 过滤后的实例
 */
public static Instance commonGetOneInstance(Extensions extensions, ServiceKey serviceKey, List<String> coreRouterNames, String lbPolicy, String protocol, String hashKey) {
    ServiceEventKey svcEventKey = new ServiceEventKey(serviceKey, EventType.INSTANCE);
    LOG.info("[ConnectionManager]start to discover service {}", svcEventKey);
    DefaultServiceEventKeysProvider provider = new DefaultServiceEventKeysProvider();
    provider.setSvcEventKey(svcEventKey);
    // 为性能考虑,优先使用本地缓存
    provider.setUseCache(true);
    FlowControlParam flowControlParam = new DefaultFlowControlParam();
    APIConfig apiConfig = extensions.getConfiguration().getGlobal().getAPI();
    flowControlParam.setTimeoutMs(apiConfig.getTimeout());
    flowControlParam.setMaxRetry(apiConfig.getMaxRetryTimes());
    flowControlParam.setRetryIntervalMs(apiConfig.getRetryInterval());
    // 执行服务路由
    ServiceInfo dstSvcInfo = new ServiceInfo();
    Map<String, String> metadata = new HashMap<>();
    metadata.put("protocol", protocol);
    dstSvcInfo.setMetadata(metadata);
    RouteInfo routeInfo = new RouteInfo(null, null, dstSvcInfo, null, "");
    ResourcesResponse resourcesResponse = BaseFlow.syncGetResources(extensions, false, provider, flowControlParam);
    LOG.info("[ConnectionManager]success to discover service {}", svcEventKey);
    ServiceInstances serviceInstances = resourcesResponse.getServiceInstances(svcEventKey);
    RouterChainGroup sysRouterChainGroup = extensions.getSysRouterChainGroup();
    List<ServiceRouter> coreRouters = Extensions.loadServiceRouters(coreRouterNames, extensions.getPlugins(), false);
    RouterChainGroup routerChainGroup = new DefaultRouterChainGroup(sysRouterChainGroup.getBeforeRouters(), coreRouters, sysRouterChainGroup.getAfterRouters());
    ServiceInstances instancesAfterRoute = BaseFlow.processServiceRouters(routeInfo, serviceInstances, routerChainGroup);
    // 执行负载均衡
    LoadBalancer loadBalancer = (LoadBalancer) extensions.getPlugins().getPlugin(PluginTypes.LOAD_BALANCER.getBaseType(), lbPolicy);
    Criteria criteria = new Criteria();
    criteria.setHashKey(hashKey);
    return BaseFlow.processLoadBalance(loadBalancer, criteria, instancesAfterRoute);
}
Also used : ServiceRouter(com.tencent.polaris.api.plugin.route.ServiceRouter) DefaultServiceEventKeysProvider(com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider) HashMap(java.util.HashMap) LoadBalancer(com.tencent.polaris.api.plugin.loadbalance.LoadBalancer) Criteria(com.tencent.polaris.api.rpc.Criteria) ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) APIConfig(com.tencent.polaris.api.config.global.APIConfig) DefaultRouterChainGroup(com.tencent.polaris.api.plugin.compose.DefaultRouterChainGroup) RouterChainGroup(com.tencent.polaris.api.plugin.compose.RouterChainGroup) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) RouteInfo(com.tencent.polaris.api.plugin.route.RouteInfo) DefaultRouterChainGroup(com.tencent.polaris.api.plugin.compose.DefaultRouterChainGroup)

Example 3 with ServiceRouter

use of com.tencent.polaris.api.plugin.route.ServiceRouter in project polaris-java by polarismesh.

the class Extensions method init.

/**
 * 初始化
 *
 * @param config 配置
 * @param plugins 插件工厂
 * @param valueContext 全局变量
 * @throws PolarisException 异常
 */
public void init(Configuration config, Supplier plugins, ValueContext valueContext) throws PolarisException {
    this.configuration = config;
    this.plugins = plugins;
    this.valueContext = valueContext;
    String localCacheType = config.getConsumer().getLocalCache().getType();
    localRegistry = (LocalRegistry) plugins.getPlugin(PluginTypes.LOCAL_REGISTRY.getBaseType(), localCacheType);
    String flowCacheName = config.getGlobal().getSystem().getFlowCache().getName();
    flowCache = (FlowCache) plugins.getPlugin(PluginTypes.FLOW_CACHE.getBaseType(), flowCacheName);
    String loadBalanceType = config.getConsumer().getLoadbalancer().getType();
    loadBalancer = (LoadBalancer) plugins.getPlugin(PluginTypes.LOAD_BALANCER.getBaseType(), loadBalanceType);
    List<ServiceRouter> beforeRouters = loadServiceRouters(config.getConsumer().getServiceRouter().getBeforeChain(), plugins, true);
    List<ServiceRouter> coreRouters = loadServiceRouters(config.getConsumer().getServiceRouter().getChain(), plugins, false);
    List<ServiceRouter> afterRouters = loadServiceRouters(config.getConsumer().getServiceRouter().getAfterChain(), plugins, true);
    configRouterChainGroup = new DefaultRouterChainGroup(beforeRouters, coreRouters, afterRouters);
    // 加载系统路由链
    List<String> sysBefore = new ArrayList<>();
    sysBefore.add(ServiceRouterConfig.DEFAULT_ROUTER_ISOLATED);
    List<String> sysAfter = new ArrayList<>();
    sysAfter.add(ServiceRouterConfig.DEFAULT_ROUTER_RECOVER);
    List<ServiceRouter> sysBeforeRouters = loadServiceRouters(sysBefore, plugins, true);
    List<ServiceRouter> sysAfterRouters = loadServiceRouters(sysAfter, plugins, true);
    sysRouterChainGroup = new DefaultRouterChainGroup(sysBeforeRouters, Collections.emptyList(), sysAfterRouters);
    // 加载熔断器
    boolean enable = config.getConsumer().getCircuitBreaker().isEnable();
    List<String> cbChain = config.getConsumer().getCircuitBreaker().getChain();
    if (enable && CollectionUtils.isNotEmpty(cbChain)) {
        for (String cbName : cbChain) {
            Plugin pluginValue = plugins.getOptionalPlugin(PluginTypes.CIRCUIT_BREAKER.getBaseType(), cbName);
            if (null == pluginValue) {
                LOG.warn("circuitBreaker plugin {} not found", cbName);
                continue;
            }
            circuitBreakers.add((CircuitBreaker) pluginValue);
        }
    }
    // 加载探测器
    loadOutlierDetector(config, plugins);
    serverConnector = (ServerConnector) plugins.getPlugin(PluginTypes.SERVER_CONNECTOR.getBaseType(), valueContext.getServerConnectorProtocol());
}
Also used : ServiceRouter(com.tencent.polaris.api.plugin.route.ServiceRouter) ArrayList(java.util.ArrayList) Plugin(com.tencent.polaris.api.plugin.Plugin)

Example 4 with ServiceRouter

use of com.tencent.polaris.api.plugin.route.ServiceRouter in project polaris-java by polarismesh.

the class Extensions method loadServiceRouters.

public static List<ServiceRouter> loadServiceRouters(List<String> routerChain, Supplier plugins, boolean force) {
    List<ServiceRouter> routers = new ArrayList<>();
    if (CollectionUtils.isNotEmpty(routerChain)) {
        for (String routerName : routerChain) {
            Plugin routerPlugin;
            if (force) {
                routerPlugin = plugins.getPlugin(PluginTypes.SERVICE_ROUTER.getBaseType(), routerName);
            } else {
                routerPlugin = plugins.getOptionalPlugin(PluginTypes.SERVICE_ROUTER.getBaseType(), routerName);
            }
            if (null == routerPlugin) {
                LOG.warn("router {} not found", routerName);
                continue;
            }
            routers.add((ServiceRouter) routerPlugin);
        }
    }
    return Collections.unmodifiableList(routers);
}
Also used : ServiceRouter(com.tencent.polaris.api.plugin.route.ServiceRouter) ArrayList(java.util.ArrayList) Plugin(com.tencent.polaris.api.plugin.Plugin)

Example 5 with ServiceRouter

use of com.tencent.polaris.api.plugin.route.ServiceRouter 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

ServiceRouter (com.tencent.polaris.api.plugin.route.ServiceRouter)5 Plugin (com.tencent.polaris.api.plugin.Plugin)2 DefaultRouterChainGroup (com.tencent.polaris.api.plugin.compose.DefaultRouterChainGroup)2 RouteInfo (com.tencent.polaris.api.plugin.route.RouteInfo)2 DefaultServiceEventKeysProvider (com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider)2 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)2 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)2 ArrayList (java.util.ArrayList)2 APIConfig (com.tencent.polaris.api.config.global.APIConfig)1 RouterChainGroup (com.tencent.polaris.api.plugin.compose.RouterChainGroup)1 LoadBalancer (com.tencent.polaris.api.plugin.loadbalance.LoadBalancer)1 RouteResult (com.tencent.polaris.api.plugin.route.RouteResult)1 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 Criteria (com.tencent.polaris.api.rpc.Criteria)1 DefaultFlowControlParam (com.tencent.polaris.client.flow.DefaultFlowControlParam)1 ResourcesResponse (com.tencent.polaris.client.flow.ResourcesResponse)1 RouterNamesGroup (com.tencent.polaris.router.api.rpc.ProcessRoutersRequest.RouterNamesGroup)1 ProcessRoutersResponse (com.tencent.polaris.router.api.rpc.ProcessRoutersResponse)1 HashMap (java.util.HashMap)1