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();
}
}
}
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());
}
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);
}
}
}
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);
}
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());
}
}
}
Aggregations