From 61fede38276f65db6105dc282f857d533babf6db Mon Sep 17 00:00:00 2001 From: cdfive <31885791+cdfive@users.noreply.github.com> Date: Mon, 4 Mar 2019 17:34:41 +0800 Subject: [PATCH] Add a CommandCenterProvider to resolve and cache the CommandCenter instance (#409) --- .../command/CommandCenterProvider.java | 37 +++++++++++++++++++ .../transport/init/CommandCenterInitFunc.java | 26 ++++++------- 2 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/CommandCenterProvider.java diff --git a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/CommandCenterProvider.java b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/CommandCenterProvider.java new file mode 100644 index 00000000..13d3f46c --- /dev/null +++ b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/CommandCenterProvider.java @@ -0,0 +1,37 @@ +package com.alibaba.csp.sentinel.command; + +import com.alibaba.csp.sentinel.log.RecordLog; +import com.alibaba.csp.sentinel.transport.CommandCenter; +import com.alibaba.csp.sentinel.util.SpiLoader; + +/** + * Provider for a universal {@link CommandCenter} instance. + * + * @author cdfive + * @date 2019-01-11 + */ +public class CommandCenterProvider { + + private static CommandCenter commandCenter = null; + + static { + resolveInstance(); + } + + private static void resolveInstance() { + CommandCenter resolveCommandCenter = SpiLoader.loadFirstInstance(CommandCenter.class); + + if (resolveCommandCenter == null) { + RecordLog.warn("[CommandCenterProvider] No existing CommandCenter, resolve failed"); + } else { + commandCenter = resolveCommandCenter; + RecordLog.info("[CommandCenterProvider] CommandCenter resolved: " + resolveCommandCenter.getClass().getCanonicalName()); + } + } + + public static CommandCenter getCommandCenter() { + return commandCenter; + } + + private CommandCenterProvider() {} +} diff --git a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/init/CommandCenterInitFunc.java b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/init/CommandCenterInitFunc.java index 78aea54e..ebf1b997 100755 --- a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/init/CommandCenterInitFunc.java +++ b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/init/CommandCenterInitFunc.java @@ -15,9 +15,7 @@ */ package com.alibaba.csp.sentinel.transport.init; -import java.util.Iterator; -import java.util.ServiceLoader; - +import com.alibaba.csp.sentinel.command.CommandCenterProvider; import com.alibaba.csp.sentinel.init.InitFunc; import com.alibaba.csp.sentinel.init.InitOrder; import com.alibaba.csp.sentinel.log.RecordLog; @@ -31,18 +29,16 @@ public class CommandCenterInitFunc implements InitFunc { @Override public void init() throws Exception { - ServiceLoader loader = ServiceLoader.load(CommandCenter.class); - Iterator iterator = loader.iterator(); - if (iterator.hasNext()) { - CommandCenter commandCenter = iterator.next(); - if (iterator.hasNext()) { - throw new IllegalStateException("Only single command center can be started"); - } else { - commandCenter.beforeStart(); - commandCenter.start(); - RecordLog.info("[CommandCenterInit] Starting command center: " - + commandCenter.getClass().getCanonicalName()); - } + CommandCenter commandCenter = CommandCenterProvider.getCommandCenter(); + + if (commandCenter == null) { + RecordLog.warn("[CommandCenterInitFunc] Cannot resolve CommandCenter"); + return; } + + commandCenter.beforeStart(); + commandCenter.start(); + RecordLog.info("[CommandCenterInit] Starting command center: " + + commandCenter.getClass().getCanonicalName()); } }