Search in sources :

Example 1 with Plugin

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

the class InMemoryRegistry method reportCircuitStat.

private void reportCircuitStat(Entry<StatusDimension, CircuitBreakerStatus> dimensionEntry, Instance instance) {
    if (null != statPlugins) {
        try {
            for (Plugin statPlugin : statPlugins) {
                if (statPlugin instanceof StatReporter) {
                    StatInfo info = new StatInfo();
                    info.setCircuitBreakGauge(convertToCircuitBreakGauge(dimensionEntry, instance));
                    ((StatReporter) statPlugin).reportStat(info);
                }
            }
        } catch (Exception ex) {
            LOG.info("circuit breaker report encountered exception, e: {}", ex.getMessage());
        }
    }
}
Also used : StatReporter(com.tencent.polaris.api.plugin.stat.StatReporter) StatInfo(com.tencent.polaris.api.plugin.stat.StatInfo) IOException(java.io.IOException) PolarisException(com.tencent.polaris.api.exception.PolarisException) Plugin(com.tencent.polaris.api.plugin.Plugin)

Example 2 with Plugin

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

the class APIFactoryTest method testInitContextByConfig.

@Test
public void testInitContextByConfig() {
    SDKContext sdkContext = null;
    try {
        sdkContext = APIFactory.initContextByConfig(TestUtils.createSimpleConfiguration(8888));
        Supplier plugins = sdkContext.getPlugins();
        Plugin plugin = plugins.getPlugin(PluginTypes.LOAD_BALANCER.getBaseType(), LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_RANDOM);
        Assert.assertNotNull(plugin);
    } catch (PolarisException e) {
        Assert.fail(e.getMessage());
    } finally {
        if (null != sdkContext) {
            sdkContext.destroy();
        }
    }
}
Also used : SDKContext(com.tencent.polaris.client.api.SDKContext) PolarisException(com.tencent.polaris.api.exception.PolarisException) Supplier(com.tencent.polaris.api.plugin.Supplier) Plugin(com.tencent.polaris.api.plugin.Plugin) Test(org.junit.Test)

Example 3 with Plugin

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

the class APIFactoryTest method testInitContextByFile.

@Test
public void testInitContextByFile() {
    InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("conf/default.yaml");
    SDKContext sdkContext = null;
    try {
        sdkContext = APIFactory.initContextByFile(resourceAsStream);
        Supplier plugins = sdkContext.getPlugins();
        Plugin plugin = plugins.getPlugin(PluginTypes.CIRCUIT_BREAKER.getBaseType(), DefaultPlugins.CIRCUIT_BREAKER_ERROR_COUNT);
        Assert.assertNotNull(plugin);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        if (null != sdkContext) {
            sdkContext.destroy();
        }
    }
}
Also used : SDKContext(com.tencent.polaris.client.api.SDKContext) InputStream(java.io.InputStream) Supplier(com.tencent.polaris.api.plugin.Supplier) PolarisException(com.tencent.polaris.api.exception.PolarisException) Plugin(com.tencent.polaris.api.plugin.Plugin) Test(org.junit.Test)

Example 4 with Plugin

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

the class PluginManager method initPlugins.

@Override
public void initPlugins(InitContext context) throws PolarisException {
    // 先实例化所有插件
    int baseId = 0;
    for (PluginType pluginType : pluginTypes) {
        Map<String, Plugin> plugins = new HashMap<>();
        typedPlugins.put(pluginType, plugins);
        ServiceLoader<? extends Plugin> loader = ServiceLoader.load(pluginType.getClazz());
        for (Plugin plugin : loader) {
            baseId++;
            String name = plugin.getName();
            if (StringUtils.isBlank(name) || plugins.containsKey(name)) {
                throw new PolarisException(ErrorCode.PLUGIN_ERROR, String.format("duplicated name for plugin(name=%s, type=%s)", name, pluginType));
            }
            if (plugin instanceof IdAwarePlugin) {
                ((IdAwarePlugin) plugin).setId(baseId);
            }
            plugins.put(name, plugin);
        }
    }
    // 再进行初始化
    for (PluginType pluginType : pluginTypes) {
        Map<String, Plugin> plugins = typedPlugins.get(pluginType);
        for (Map.Entry<String, Plugin> pluginEntry : plugins.entrySet()) {
            pluginEntry.getValue().init(context);
        }
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) HashMap(java.util.HashMap) IdAwarePlugin(com.tencent.polaris.api.plugin.IdAwarePlugin) 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 5 with Plugin

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

the class PluginManager method postContextInitPlugins.

/**
 * 在应用上下文初始化完毕后进行调用
 *
 * @param extensions 插件实例
 * @throws PolarisException 运行异常
 */
public void postContextInitPlugins(Extensions extensions) throws PolarisException {
    for (PluginType pluginType : pluginTypes) {
        Map<String, Plugin> plugins = typedPlugins.get(pluginType);
        if (MapUtils.isEmpty(plugins)) {
            continue;
        }
        for (Map.Entry<String, Plugin> pluginEntry : plugins.entrySet()) {
            Plugin plugin = pluginEntry.getValue();
            plugin.postContextInit(extensions);
        }
    }
}
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)

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