Use ServiceLoaderUtil to create SPI ServiceLoader
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
parent
8d5654f727
commit
b0905547ff
|
|
@ -15,13 +15,10 @@
|
||||||
*/
|
*/
|
||||||
package com.alibaba.csp.sentinel.cluster.server;
|
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.TokenService;
|
||||||
import com.alibaba.csp.sentinel.cluster.flow.DefaultTokenService;
|
import com.alibaba.csp.sentinel.cluster.flow.DefaultTokenService;
|
||||||
import com.alibaba.csp.sentinel.log.RecordLog;
|
import com.alibaba.csp.sentinel.log.RecordLog;
|
||||||
|
import com.alibaba.csp.sentinel.util.SpiLoader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Eric Zhao
|
* @author Eric Zhao
|
||||||
|
|
@ -31,8 +28,6 @@ public final class TokenServiceProvider {
|
||||||
|
|
||||||
private static TokenService service = null;
|
private static TokenService service = null;
|
||||||
|
|
||||||
private static final ServiceLoader<TokenService> LOADER = ServiceLoader.load(TokenService.class);
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
resolveTokenServiceSpi();
|
resolveTokenServiceSpi();
|
||||||
}
|
}
|
||||||
|
|
@ -42,24 +37,12 @@ public final class TokenServiceProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void resolveTokenServiceSpi() {
|
private static void resolveTokenServiceSpi() {
|
||||||
boolean hasOther = false;
|
service = SpiLoader.loadFirstInstanceOrDefault(TokenService.class, DefaultTokenService.class);
|
||||||
List<TokenService> list = new ArrayList<TokenService>();
|
if (service != null) {
|
||||||
for (TokenService service : LOADER) {
|
RecordLog.info("[TokenServiceProvider] Global token service resolved: "
|
||||||
if (service.getClass() != DefaultTokenService.class) {
|
+ service.getClass().getCanonicalName());
|
||||||
hasOther = true;
|
|
||||||
list.add(service);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hasOther) {
|
|
||||||
// Pick the first.
|
|
||||||
service = list.get(0);
|
|
||||||
} else {
|
} else {
|
||||||
// No custom token service, using default.
|
RecordLog.warn("[TokenServiceProvider] Unable to resolve TokenService: no SPI found");
|
||||||
service = new DefaultTokenService();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RecordLog.info("[TokenServiceProvider] Global token service resolved: "
|
|
||||||
+ service.getClass().getCanonicalName());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import java.util.ServiceLoader;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.cluster.annotation.RequestType;
|
import com.alibaba.csp.sentinel.cluster.annotation.RequestType;
|
||||||
|
import com.alibaba.csp.sentinel.spi.ServiceLoaderUtil;
|
||||||
import com.alibaba.csp.sentinel.util.AssertUtil;
|
import com.alibaba.csp.sentinel.util.AssertUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -30,7 +31,8 @@ public final class RequestProcessorProvider {
|
||||||
|
|
||||||
private static final Map<Integer, RequestProcessor> PROCESSOR_MAP = new ConcurrentHashMap<>();
|
private static final Map<Integer, RequestProcessor> PROCESSOR_MAP = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private static final ServiceLoader<RequestProcessor> SERVICE_LOADER = ServiceLoader.load(RequestProcessor.class);
|
private static final ServiceLoader<RequestProcessor> SERVICE_LOADER = ServiceLoaderUtil.getServiceLoader(
|
||||||
|
RequestProcessor.class);
|
||||||
|
|
||||||
static {
|
static {
|
||||||
loadAndInit();
|
loadAndInit();
|
||||||
|
|
@ -71,6 +73,5 @@ public final class RequestProcessorProvider {
|
||||||
PROCESSOR_MAP.put(type, processor);
|
PROCESSOR_MAP.put(type, processor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private RequestProcessorProvider() {}
|
private RequestProcessorProvider() {}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ import java.util.ServiceLoader;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.log.RecordLog;
|
import com.alibaba.csp.sentinel.log.RecordLog;
|
||||||
|
import com.alibaba.csp.sentinel.spi.ServiceLoaderUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load registered init functions and execute in order.
|
* Load registered init functions and execute in order.
|
||||||
|
|
@ -42,7 +43,7 @@ public final class InitExecutor {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
ServiceLoader<InitFunc> loader = ServiceLoader.load(InitFunc.class);
|
ServiceLoader<InitFunc> loader = ServiceLoaderUtil.getServiceLoader(InitFunc.class);
|
||||||
List<OrderWrapper> initList = new ArrayList<OrderWrapper>();
|
List<OrderWrapper> initList = new ArrayList<OrderWrapper>();
|
||||||
for (InitFunc initFunc : loader) {
|
for (InitFunc initFunc : loader) {
|
||||||
RecordLog.info("[InitExecutor] Found init func: " + initFunc.getClass().getCanonicalName());
|
RecordLog.info("[InitExecutor] Found init func: " + initFunc.getClass().getCanonicalName());
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ package com.alibaba.csp.sentinel.slotchain;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.log.RecordLog;
|
import com.alibaba.csp.sentinel.log.RecordLog;
|
||||||
import com.alibaba.csp.sentinel.slots.DefaultSlotChainBuilder;
|
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.
|
* 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 volatile SlotChainBuilder slotChainBuilder = null;
|
||||||
|
|
||||||
private static final ServiceLoader<SlotChainBuilder> 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
|
* 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.
|
* via {@code lookProcessChain} in {@link com.alibaba.csp.sentinel.CtSph} under lock.
|
||||||
|
|
@ -42,30 +40,19 @@ public final class SlotChainProvider {
|
||||||
return slotChainBuilder.build();
|
return slotChainBuilder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
resolveSlotChainBuilder();
|
// Resolve the slot chain builder SPI.
|
||||||
|
slotChainBuilder = SpiLoader.loadFirstInstanceOrDefault(SlotChainBuilder.class, DefaultSlotChainBuilder.class);
|
||||||
|
|
||||||
if (slotChainBuilder == null) {
|
if (slotChainBuilder == null) {
|
||||||
|
// Should not go through here.
|
||||||
RecordLog.warn("[SlotChainProvider] Wrong state when resolving slot chain builder, using default");
|
RecordLog.warn("[SlotChainProvider] Wrong state when resolving slot chain builder, using default");
|
||||||
slotChainBuilder = new DefaultSlotChainBuilder();
|
slotChainBuilder = new DefaultSlotChainBuilder();
|
||||||
|
} else {
|
||||||
|
RecordLog.info("[SlotChainProvider] Global slot chain builder resolved: "
|
||||||
|
+ slotChainBuilder.getClass().getCanonicalName());
|
||||||
}
|
}
|
||||||
return slotChainBuilder.build();
|
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() {}
|
private SlotChainProvider() {}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ import java.util.Map;
|
||||||
import java.util.ServiceLoader;
|
import java.util.ServiceLoader;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.command.annotation.CommandMapping;
|
import com.alibaba.csp.sentinel.command.annotation.CommandMapping;
|
||||||
|
import com.alibaba.csp.sentinel.spi.ServiceLoaderUtil;
|
||||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -30,7 +31,8 @@ import com.alibaba.csp.sentinel.util.StringUtil;
|
||||||
*/
|
*/
|
||||||
public class CommandHandlerProvider implements Iterable<CommandHandler> {
|
public class CommandHandlerProvider implements Iterable<CommandHandler> {
|
||||||
|
|
||||||
private final ServiceLoader<CommandHandler> serviceLoader = ServiceLoader.load(CommandHandler.class);
|
private final ServiceLoader<CommandHandler> serviceLoader = ServiceLoaderUtil.getServiceLoader(
|
||||||
|
CommandHandler.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all command handlers annotated with {@link CommandMapping} with command name.
|
* Get all command handlers annotated with {@link CommandMapping} with command name.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue