Browse Source

Polish SpiLoader and SentinelConfig

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 4 years ago
parent
commit
33570dad40
2 changed files with 19 additions and 15 deletions
  1. +1
    -0
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java
  2. +18
    -15
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/spi/SpiLoader.java

+ 1
- 0
sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java View File

@@ -167,6 +167,7 @@ public final class SentinelConfig {
/** /**
* Get the metric log flush interval in second * Get the metric log flush interval in second
* @return the metric log flush interval in second * @return the metric log flush interval in second
* @since 1.8.1
*/ */
public static long metricLogFlushIntervalSec() { public static long metricLogFlushIntervalSec() {
String flushIntervalStr = SentinelConfig.getConfig(METRIC_FLUSH_INTERVAL); String flushIntervalStr = SentinelConfig.getConfig(METRIC_FLUSH_INTERVAL);


+ 18
- 15
sentinel-core/src/main/java/com/alibaba/csp/sentinel/spi/SpiLoader.java View File

@@ -23,12 +23,13 @@ import com.alibaba.csp.sentinel.util.StringUtil;
import java.io.*; import java.io.*;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;


/** /**
* A simple SPI loading facility.
* A simple SPI loading facility (refactored since 1.8.1).
* *
* <p>SPI is short for Service Provider Interface.</p> * <p>SPI is short for Service Provider Interface.</p>
* *
@@ -54,13 +55,14 @@ import java.util.concurrent.atomic.AtomicBoolean;
* 3. The comment line character is #, all characters following it are ignored. * 3. The comment line character is #, all characters following it are ignored.
* </p> * </p>
* *
* <p>
* Provide common functions, such as:
* Load all Provider instance unsorted/sorted list.
* Load highest/lowest order priority instance.
* Load first-found or default instance.
* Load instance by aliasname or provider class.
* </p>
*
* <p>{@code SpiLoader} provide common functions, such as:</p>
* <ul>
* <li>Load all Provider instance unsorted/sorted list.</li>
* <li>Load highest/lowest order priority instance.</li>
* <li>Load first-found or default instance.</li>
* <li>Load instance by alias name or provider class.</li>
* </ul>
* *
* @author Eric Zhao * @author Eric Zhao
* @author cdfive * @author cdfive
@@ -92,7 +94,7 @@ public final class SpiLoader<S> {
// Cache the singleton instance of Provider, key: classname of Provider, value: Provider instance // Cache the singleton instance of Provider, key: classname of Provider, value: Provider instance
private final ConcurrentHashMap<String, S> singletonMap = new ConcurrentHashMap<>(); private final ConcurrentHashMap<String, S> singletonMap = new ConcurrentHashMap<>();


// Whether this SpiLoader has beend loaded, that is, loaded the Provider configuration file
// Whether this SpiLoader has been loaded, that is, loaded the Provider configuration file
private final AtomicBoolean loaded = new AtomicBoolean(false); private final AtomicBoolean loaded = new AtomicBoolean(false);


// Default provider class // Default provider class
@@ -327,11 +329,11 @@ public final class SpiLoader<S> {
try { try {
urls = classLoader.getResources(fullFileName); urls = classLoader.getResources(fullFileName);
} catch (IOException e) { } catch (IOException e) {
fail("Error locating SPI configuration file,filename=" + fullFileName + ",classloader=" + classLoader, e);
fail("Error locating SPI configuration file, filename=" + fullFileName + ", classloader=" + classLoader, e);
} }


if (urls == null || !urls.hasMoreElements()) { if (urls == null || !urls.hasMoreElements()) {
RecordLog.warn("No SPI configuration file,filename=" + fullFileName + ",classloader=" + classLoader);
RecordLog.warn("No SPI configuration file, filename=" + fullFileName + ", classloader=" + classLoader);
return; return;
} }


@@ -342,7 +344,7 @@ public final class SpiLoader<S> {
BufferedReader br = null; BufferedReader br = null;
try { try {
in = url.openStream(); in = url.openStream();
br = new BufferedReader(new InputStreamReader(in, "utf-8"));
br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
if (StringUtil.isBlank(line)) { if (StringUtil.isBlank(line)) {
@@ -378,19 +380,20 @@ public final class SpiLoader<S> {
String aliasName = spi == null || "".equals(spi.value()) ? clazz.getName() : spi.value(); String aliasName = spi == null || "".equals(spi.value()) ? clazz.getName() : spi.value();
if (classMap.containsKey(aliasName)) { if (classMap.containsKey(aliasName)) {
Class<? extends S> existClass = classMap.get(aliasName); Class<? extends S> existClass = classMap.get(aliasName);
fail("Found repeat aliasname for " + clazz.getName() + " and "
fail("Found repeat alias name for " + clazz.getName() + " and "
+ existClass.getName() + ",SPI configuration file=" + fullFileName); + existClass.getName() + ",SPI configuration file=" + fullFileName);
} }
classMap.put(aliasName, clazz); classMap.put(aliasName, clazz);


if (spi != null && spi.isDefault()) { if (spi != null && spi.isDefault()) {
if (defaultClass != null) { if (defaultClass != null) {
fail("Found more than one default Provider,SPI configuration file=" + fullFileName);
fail("Found more than one default Provider, SPI configuration file=" + fullFileName);
} }
defaultClass = clazz; defaultClass = clazz;
} }


RecordLog.info("[SpiLoader]Found SPI,Service={},Provider={},aliasname={},isSingleton={},isDefault={},order={}",
RecordLog.info("[SpiLoader] Found SPI implementation for SPI {}, provider={}, aliasName={}"
+ ", isSingleton={}, isDefault={}, order={}",
service.getName(), line, aliasName service.getName(), line, aliasName
, spi == null ? true : spi.isSingleton() , spi == null ? true : spi.isSingleton()
, spi == null ? false : spi.isDefault() , spi == null ? false : spi.isDefault()


Loading…
Cancel
Save