diff --git a/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/server/TokenServiceProvider.java b/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/server/TokenServiceProvider.java index 969e5400..1f9cd1c7 100644 --- a/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/server/TokenServiceProvider.java +++ b/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/server/TokenServiceProvider.java @@ -15,13 +15,10 @@ */ package com.alibaba.csp.sentinel.cluster.server; -import java.util.ArrayList; -import java.util.List; -import java.util.ServiceLoader; - import com.alibaba.csp.sentinel.cluster.TokenService; import com.alibaba.csp.sentinel.cluster.flow.DefaultTokenService; import com.alibaba.csp.sentinel.log.RecordLog; +import com.alibaba.csp.sentinel.util.SpiLoader; /** * @author Eric Zhao @@ -31,8 +28,6 @@ public final class TokenServiceProvider { private static TokenService service = null; - private static final ServiceLoader LOADER = ServiceLoader.load(TokenService.class); - static { resolveTokenServiceSpi(); } @@ -42,24 +37,12 @@ public final class TokenServiceProvider { } private static void resolveTokenServiceSpi() { - boolean hasOther = false; - List list = new ArrayList(); - for (TokenService service : LOADER) { - if (service.getClass() != DefaultTokenService.class) { - hasOther = true; - list.add(service); - } - } - - if (hasOther) { - // Pick the first. - service = list.get(0); + service = SpiLoader.loadFirstInstanceOrDefault(TokenService.class, DefaultTokenService.class); + if (service != null) { + RecordLog.info("[TokenServiceProvider] Global token service resolved: " + + service.getClass().getCanonicalName()); } else { - // No custom token service, using default. - service = new DefaultTokenService(); + RecordLog.warn("[TokenServiceProvider] Unable to resolve TokenService: no SPI found"); } - - RecordLog.info("[TokenServiceProvider] Global token service resolved: " - + service.getClass().getCanonicalName()); } } diff --git a/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/server/processor/RequestProcessorProvider.java b/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/server/processor/RequestProcessorProvider.java index bd572f27..ca1bd87d 100644 --- a/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/server/processor/RequestProcessorProvider.java +++ b/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/server/processor/RequestProcessorProvider.java @@ -20,6 +20,7 @@ import java.util.ServiceLoader; import java.util.concurrent.ConcurrentHashMap; import com.alibaba.csp.sentinel.cluster.annotation.RequestType; +import com.alibaba.csp.sentinel.spi.ServiceLoaderUtil; import com.alibaba.csp.sentinel.util.AssertUtil; /** @@ -30,7 +31,8 @@ public final class RequestProcessorProvider { private static final Map PROCESSOR_MAP = new ConcurrentHashMap<>(); - private static final ServiceLoader SERVICE_LOADER = ServiceLoader.load(RequestProcessor.class); + private static final ServiceLoader SERVICE_LOADER = ServiceLoaderUtil.getServiceLoader( + RequestProcessor.class); static { loadAndInit(); @@ -71,6 +73,5 @@ public final class RequestProcessorProvider { PROCESSOR_MAP.put(type, processor); } - private RequestProcessorProvider() {} } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/init/InitExecutor.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/init/InitExecutor.java index 0d84ad78..e68fd599 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/init/InitExecutor.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/init/InitExecutor.java @@ -21,6 +21,7 @@ import java.util.ServiceLoader; import java.util.concurrent.atomic.AtomicBoolean; import com.alibaba.csp.sentinel.log.RecordLog; +import com.alibaba.csp.sentinel.spi.ServiceLoaderUtil; /** * Load registered init functions and execute in order. @@ -42,7 +43,7 @@ public final class InitExecutor { return; } try { - ServiceLoader loader = ServiceLoader.load(InitFunc.class); + ServiceLoader loader = ServiceLoaderUtil.getServiceLoader(InitFunc.class); List initList = new ArrayList(); for (InitFunc initFunc : loader) { RecordLog.info("[InitExecutor] Found init func: " + initFunc.getClass().getCanonicalName()); diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/SlotChainProvider.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/SlotChainProvider.java index 2d801486..9aabbb33 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/SlotChainProvider.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slotchain/SlotChainProvider.java @@ -17,7 +17,7 @@ package com.alibaba.csp.sentinel.slotchain; import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.slots.DefaultSlotChainBuilder; -import java.util.ServiceLoader; +import com.alibaba.csp.sentinel.util.SpiLoader; /** * A provider for creating slot chains via resolved slot chain builder SPI. @@ -29,8 +29,6 @@ public final class SlotChainProvider { private static volatile SlotChainBuilder slotChainBuilder = null; - private static final ServiceLoader LOADER = ServiceLoader.load(SlotChainBuilder.class); - /** * The load and pick process is not thread-safe, but it's okay since the method should be only invoked * via {@code lookProcessChain} in {@link com.alibaba.csp.sentinel.CtSph} under lock. @@ -42,30 +40,19 @@ public final class SlotChainProvider { return slotChainBuilder.build(); } - resolveSlotChainBuilder(); + // Resolve the slot chain builder SPI. + slotChainBuilder = SpiLoader.loadFirstInstanceOrDefault(SlotChainBuilder.class, DefaultSlotChainBuilder.class); if (slotChainBuilder == null) { + // Should not go through here. RecordLog.warn("[SlotChainProvider] Wrong state when resolving slot chain builder, using default"); slotChainBuilder = new DefaultSlotChainBuilder(); + } else { + RecordLog.info("[SlotChainProvider] Global slot chain builder resolved: " + + slotChainBuilder.getClass().getCanonicalName()); } return slotChainBuilder.build(); } - private static void resolveSlotChainBuilder() { - for (SlotChainBuilder builder : LOADER) { - if (builder.getClass() != DefaultSlotChainBuilder.class) { - slotChainBuilder = builder; - break; - } - } - if (slotChainBuilder == null){ - // No custom builder, using default. - slotChainBuilder = new DefaultSlotChainBuilder(); - } - - RecordLog.info("[SlotChainProvider] Global slot chain builder resolved: " - + slotChainBuilder.getClass().getCanonicalName()); - } - private SlotChainProvider() {} } diff --git a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/CommandHandlerProvider.java b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/CommandHandlerProvider.java index a39c24ba..00b8e5de 100755 --- a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/CommandHandlerProvider.java +++ b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/CommandHandlerProvider.java @@ -21,6 +21,7 @@ import java.util.Map; import java.util.ServiceLoader; import com.alibaba.csp.sentinel.command.annotation.CommandMapping; +import com.alibaba.csp.sentinel.spi.ServiceLoaderUtil; import com.alibaba.csp.sentinel.util.StringUtil; /** @@ -30,7 +31,8 @@ import com.alibaba.csp.sentinel.util.StringUtil; */ public class CommandHandlerProvider implements Iterable { - private final ServiceLoader serviceLoader = ServiceLoader.load(CommandHandler.class); + private final ServiceLoader serviceLoader = ServiceLoaderUtil.getServiceLoader( + CommandHandler.class); /** * Get all command handlers annotated with {@link CommandMapping} with command name.