Search in sources :

Example 6 with Plugin

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

the class PluginManager method destroyPlugins.

/**
 * 销毁已初始化的插件列表
 */
@Override
public void destroyPlugins() {
    // 倒序遍历
    for (int i = pluginTypes.size() - 1; i >= 0; i--) {
        PluginType pluginType = pluginTypes.get(i);
        Map<String, Plugin> plugins = typedPlugins.get(pluginType);
        if (MapUtils.isEmpty(plugins)) {
            continue;
        }
        for (Map.Entry<String, Plugin> plugin : plugins.entrySet()) {
            plugin.getValue().destroy();
        }
    }
}
Also used : PluginType(com.tencent.polaris.api.plugin.PluginType) HashMap(java.util.HashMap) Map(java.util.Map) IdAwarePlugin(com.tencent.polaris.api.plugin.IdAwarePlugin) Plugin(com.tencent.polaris.api.plugin.Plugin)

Example 7 with Plugin

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

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

the class Extensions method loadOutlierDetector.

private void loadOutlierDetector(Configuration config, Supplier plugins) throws PolarisException {
    boolean enable = config.getConsumer().getOutlierDetection().getWhen() != When.never;
    List<String> detectionChain = config.getConsumer().getOutlierDetection().getChain();
    if (enable && CollectionUtils.isNotEmpty(detectionChain)) {
        for (String detectorName : detectionChain) {
            Plugin pluginValue = plugins.getOptionalPlugin(PluginTypes.HEALTH_CHECKER.getBaseType(), detectorName);
            if (null == pluginValue) {
                LOG.warn("outlierDetector plugin {} not found", detectorName);
                continue;
            }
            healthCheckers.add((HealthChecker) pluginValue);
        }
    }
}
Also used : Plugin(com.tencent.polaris.api.plugin.Plugin)

Example 9 with Plugin

use of com.tencent.polaris.api.plugin.Plugin 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 10 with Plugin

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

the class DefaultLimitAPI method reportRateLimit.

private void reportRateLimit(QuotaRequest req, QuotaResponse rsp) {
    if (null != statPlugins) {
        try {
            DefaultRateLimitResult rateLimitGauge = new DefaultRateLimitResult();
            rateLimitGauge.setLabels(formatLabelsToStr(req.getLabels()));
            rateLimitGauge.setMethod(req.getMethod());
            rateLimitGauge.setNamespace(req.getNamespace());
            rateLimitGauge.setService(req.getService());
            rateLimitGauge.setResult(rsp.getCode() == QuotaResultOk ? RateLimitGauge.Result.PASSED : RateLimitGauge.Result.LIMITED);
            StatInfo statInfo = new StatInfo();
            statInfo.setRateLimitGauge(rateLimitGauge);
            for (Plugin statPlugin : statPlugins) {
                if (statPlugin instanceof StatReporter) {
                    ((StatReporter) statPlugin).reportStat(statInfo);
                }
            }
        } catch (Exception ex) {
            LOG.info("rate limit report encountered exception, e: {}", ex.getMessage());
        }
    }
}
Also used : DefaultRateLimitResult(com.tencent.polaris.api.plugin.stat.DefaultRateLimitResult) StatReporter(com.tencent.polaris.api.plugin.stat.StatReporter) StatInfo(com.tencent.polaris.api.plugin.stat.StatInfo) PolarisException(com.tencent.polaris.api.exception.PolarisException) Plugin(com.tencent.polaris.api.plugin.Plugin)

Aggregations

Plugin (com.tencent.polaris.api.plugin.Plugin)10 PolarisException (com.tencent.polaris.api.exception.PolarisException)5 IdAwarePlugin (com.tencent.polaris.api.plugin.IdAwarePlugin)3 PluginType (com.tencent.polaris.api.plugin.PluginType)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Supplier (com.tencent.polaris.api.plugin.Supplier)2 ServiceRouter (com.tencent.polaris.api.plugin.route.ServiceRouter)2 StatInfo (com.tencent.polaris.api.plugin.stat.StatInfo)2 StatReporter (com.tencent.polaris.api.plugin.stat.StatReporter)2 SDKContext (com.tencent.polaris.client.api.SDKContext)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 DefaultRateLimitResult (com.tencent.polaris.api.plugin.stat.DefaultRateLimitResult)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1