From dbd3c06ca7c34918981ad70f1b123824f287adcf Mon Sep 17 00:00:00 2001 From: cdfive <31885791+cdfive@users.noreply.github.com> Date: Sun, 28 Apr 2024 09:53:01 +0800 Subject: [PATCH] Fix when duplication spi class found using SpiLoader (#3387) --- .../com/alibaba/csp/sentinel/spi/SpiLoader.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/spi/SpiLoader.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/spi/SpiLoader.java index ffeeaa3eb0..d53f9eb492 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/spi/SpiLoader.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/spi/SpiLoader.java @@ -329,11 +329,11 @@ public void load() { try { urls = classLoader.getResources(fullFileName); } 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()) { - RecordLog.warn("No SPI configuration file, filename=" + fullFileName + ", classloader=" + classLoader); + RecordLog.warn("No SPI configuration file,filename=" + fullFileName + ",classloader=" + classLoader); return; } @@ -371,8 +371,13 @@ public void load() { fail("class " + line + " not found", e); } + if (classMap.containsValue(clazz)) { + RecordLog.warn("duplicate class found,className=" + clazz.getName() + ",SPI configuration file[" + url + "]"); + continue; + } + if (!service.isAssignableFrom(clazz)) { - fail("class " + clazz.getName() + "is not subtype of " + service.getName() + ",SPI configuration file=" + fullFileName); + fail("class " + clazz.getName() + "is not subtype of " + service.getName() + ",SPI configuration file[" + url + "]"); } classList.add(clazz); @@ -381,13 +386,13 @@ public void load() { if (classMap.containsKey(aliasName)) { Class existClass = classMap.get(aliasName); fail("Found repeat alias name for " + clazz.getName() + " and " - + existClass.getName() + ",SPI configuration file=" + fullFileName); + + existClass.getName() + ",SPI configuration file[" + url + "]"); } classMap.put(aliasName, clazz); if (spi != null && spi.isDefault()) { if (defaultClass != null) { - fail("Found more than one default Provider, SPI configuration file=" + fullFileName); + fail("Found more than one default Provider,className=" + clazz.getName() + ",SPI configuration file[" + url + "]"); } defaultClass = clazz; } @@ -400,7 +405,7 @@ public void load() { , spi == null ? 0 : spi.order()); } } catch (IOException e) { - fail("error reading SPI configuration file", e); + fail("error reading SPI configuration file[" + url + "]", e); } finally { closeResources(in, br); }