diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java index 11cab85d..30d80717 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java @@ -48,6 +48,7 @@ public class SentinelConfig { public static final String TOTAL_METRIC_FILE_COUNT = "csp.sentinel.metric.file.total.count"; public static final String COLD_FACTOR = "csp.sentinel.flow.cold.factor"; public static final String STATISTIC_MAX_RT = "csp.sentinel.statistic.max.rt"; + public static final String SPI_CLASSLOADER = "csp.sentinel.spi.classloader"; static final String DEFAULT_CHARSET = "UTF-8"; static final long DEFAULT_SINGLE_METRIC_FILE_SIZE = 1024 * 1024 * 50; diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/spi/ServiceLoaderUtil.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/spi/ServiceLoaderUtil.java new file mode 100644 index 00000000..f7fe6fde --- /dev/null +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/spi/ServiceLoaderUtil.java @@ -0,0 +1,45 @@ +/* + * Copyright 1999-2019 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.spi; + +import java.util.ServiceLoader; + +import com.alibaba.csp.sentinel.config.SentinelConfig; + +/** + * @author Eric Zhao + * @since 1.7.0 + */ +public final class ServiceLoaderUtil { + + private static final String CLASSLOADER_DEFAULT = "default"; + private static final String CLASSLOADER_CONTEXT = "context"; + + public static ServiceLoader getServiceLoader(Class clazz) { + if (shouldUseContextClassloader()) { + return ServiceLoader.load(clazz); + } else { + return ServiceLoader.load(clazz, clazz.getClassLoader()); + } + } + + public static boolean shouldUseContextClassloader() { + String classloaderConf = SentinelConfig.getConfig(SentinelConfig.SPI_CLASSLOADER); + return CLASSLOADER_CONTEXT.equalsIgnoreCase(classloaderConf); + } + + private ServiceLoaderUtil() {} +} diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java index 6632b792..e36a6e87 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/SpiLoader.java @@ -23,6 +23,7 @@ import java.util.ServiceLoader; import java.util.concurrent.ConcurrentHashMap; import com.alibaba.csp.sentinel.log.RecordLog; +import com.alibaba.csp.sentinel.spi.ServiceLoaderUtil; import com.alibaba.csp.sentinel.spi.SpiOrder; /** @@ -34,12 +35,13 @@ public final class SpiLoader { private static final Map SERVICE_LOADER_MAP = new ConcurrentHashMap(); public static T loadFirstInstance(Class clazz) { + AssertUtil.notNull(clazz, "SPI class cannot be null"); try { String key = clazz.getName(); // Not thread-safe, as it's expected to be resolved in a thread-safe context. ServiceLoader serviceLoader = SERVICE_LOADER_MAP.get(key); if (serviceLoader == null) { - serviceLoader = ServiceLoader.load(clazz); + serviceLoader = ServiceLoaderUtil.getServiceLoader(clazz); SERVICE_LOADER_MAP.put(key, serviceLoader); } @@ -56,6 +58,41 @@ public final class SpiLoader { } } + /** + * Load the first-found specific SPI instance (excluding provided default SPI class). + * If no other SPI implementation found, then create a default SPI instance. + * + * @param clazz class of the SPI interface + * @param defaultClass class of the default SPI implementation (if no other implementation found) + * @param SPI type + * @return the first specific SPI instance if exists, or else the default SPI instance + * @since 1.7.0 + */ + public static T loadFirstInstanceOrDefault(Class clazz, Class defaultClass) { + AssertUtil.notNull(clazz, "SPI class cannot be null"); + AssertUtil.notNull(defaultClass, "default SPI class cannot be null"); + try { + String key = clazz.getName(); + // Not thread-safe, as it's expected to be resolved in a thread-safe context. + ServiceLoader serviceLoader = SERVICE_LOADER_MAP.get(key); + if (serviceLoader == null) { + serviceLoader = ServiceLoaderUtil.getServiceLoader(clazz); + SERVICE_LOADER_MAP.put(key, serviceLoader); + } + + for (T instance : serviceLoader) { + if (instance.getClass() != defaultClass) { + return instance; + } + } + return defaultClass.newInstance(); + } catch (Throwable t) { + RecordLog.warn("[SpiLoader] ERROR: loadFirstInstanceOrDefault failed", t); + t.printStackTrace(); + return null; + } + } + /** * Load the SPI instance with highest priority. * @@ -70,7 +107,7 @@ public final class SpiLoader { // Not thread-safe, as it's expected to be resolved in a thread-safe context. ServiceLoader serviceLoader = SERVICE_LOADER_MAP.get(key); if (serviceLoader == null) { - serviceLoader = ServiceLoader.load(clazz); + serviceLoader = ServiceLoaderUtil.getServiceLoader(clazz); SERVICE_LOADER_MAP.put(key, serviceLoader); } @@ -105,7 +142,7 @@ public final class SpiLoader { // Not thread-safe, as it's expected to be resolved in a thread-safe context. ServiceLoader serviceLoader = SERVICE_LOADER_MAP.get(key); if (serviceLoader == null) { - serviceLoader = ServiceLoader.load(clazz); + serviceLoader = ServiceLoaderUtil.getServiceLoader(clazz); SERVICE_LOADER_MAP.put(key, serviceLoader); } @@ -135,7 +172,7 @@ public final class SpiLoader { // Not thread-safe, as it's expected to be resolved in a thread-safe context. ServiceLoader serviceLoader = SERVICE_LOADER_MAP.get(key); if (serviceLoader == null) { - serviceLoader = ServiceLoader.load(clazz); + serviceLoader = ServiceLoaderUtil.getServiceLoader(clazz); SERVICE_LOADER_MAP.put(key, serviceLoader); }