diff --git a/microsphere-alibaba-sentinel-commons/pom.xml b/microsphere-alibaba-sentinel-commons/pom.xml index 5fd3371..1948978 100644 --- a/microsphere-alibaba-sentinel-commons/pom.xml +++ b/microsphere-alibaba-sentinel-commons/pom.xml @@ -26,6 +26,13 @@ microsphere-java-core + + + io.github.microsphere-projects + microsphere-annotation-processor + true + + com.alibaba.csp diff --git a/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/SentinelPlugin.java b/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/SentinelPlugin.java index 7fee5bb..8d54764 100644 --- a/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/SentinelPlugin.java +++ b/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/SentinelPlugin.java @@ -19,9 +19,9 @@ import com.alibaba.csp.sentinel.EntryType; import com.alibaba.csp.sentinel.ResourceTypeConstants; -import io.microsphere.annotation.Nonnull; import io.microsphere.alibaba.sentinel.common.constants.SentinelConstants; import io.microsphere.alibaba.sentinel.common.util.SentinelUtils; +import io.microsphere.annotation.Nonnull; import static io.microsphere.alibaba.sentinel.common.SentinelPluginRepository.INSTANCE; import static io.microsphere.alibaba.sentinel.common.util.SentinelUtils.getDefaultContextName; diff --git a/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/SentinelTemplate.java b/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/SentinelTemplate.java index fa43826..2de3009 100644 --- a/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/SentinelTemplate.java +++ b/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/SentinelTemplate.java @@ -27,9 +27,9 @@ import static com.alibaba.csp.sentinel.Tracer.traceEntry; import static com.alibaba.csp.sentinel.context.ContextUtil.enter; import static com.alibaba.csp.sentinel.context.ContextUtil.exit; -import static io.microsphere.logging.LoggerFactory.getLogger; import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.DEFAULT_CONTEXT_NAME; import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.DEFAULT_ORIGIN; +import static io.microsphere.logging.LoggerFactory.getLogger; import static io.microsphere.util.StringUtils.isBlank; /** diff --git a/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/SimpleSentinelPlugin.java b/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/SimpleSentinelPlugin.java index 82576de..b6374fa 100644 --- a/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/SimpleSentinelPlugin.java +++ b/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/SimpleSentinelPlugin.java @@ -48,7 +48,8 @@ public SimpleSentinelPlugin(String name, String contextName, String origin, int super(name, contextName, origin, resourceType, trafficType); } - public SimpleSentinelPlugin(String name, String contextName, String origin, int resourceType, EntryType trafficType, boolean autoRegisterMBean) { - super(name, contextName, origin, resourceType, trafficType, autoRegisterMBean); + public SimpleSentinelPlugin(String name, String contextName, String origin, int resourceType, EntryType trafficType, + boolean autoInstalled) { + super(name, contextName, origin, resourceType, trafficType, autoInstalled); } } \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/constants/SentinelConstants.java b/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/constants/SentinelConstants.java index da4cefc..0162810 100644 --- a/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/constants/SentinelConstants.java +++ b/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/constants/SentinelConstants.java @@ -18,6 +18,11 @@ package io.microsphere.alibaba.sentinel.common.constants; import io.microsphere.alibaba.sentinel.common.SentinelContext; +import io.microsphere.annotation.ConfigurationProperty; +import io.microsphere.constants.PropertyConstants; + +import static io.microsphere.annotation.ConfigurationProperty.APPLICATION_SOURCE; +import static io.microsphere.annotation.ConfigurationProperty.SYSTEM_PROPERTIES_SOURCE; /** * The constants of Sentinel @@ -52,6 +57,24 @@ public interface SentinelConstants { */ String PROPERTY_NAME_PREFIX = "microsphere.sentinel."; + /** + * The default value of enabled : "true" + */ + String ENABLED_PROPERTY_VALUE = "true"; + + /** + * The Property Name of enabled : "microsphere.sentinel.enabled" + */ + @ConfigurationProperty( + type = boolean.class, + defaultValue = ENABLED_PROPERTY_VALUE, + source = { + SYSTEM_PROPERTIES_SOURCE, + APPLICATION_SOURCE + } + ) + String ENABLED_PROPERTY_NAME = PROPERTY_NAME_PREFIX + PropertyConstants.ENABLED_PROPERTY_NAME; + /** * The attribute name of {@link SentinelContext} * diff --git a/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/util/SentinelUtils.java b/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/util/SentinelUtils.java index cf05559..8f02fe9 100644 --- a/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/util/SentinelUtils.java +++ b/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/common/util/SentinelUtils.java @@ -2,9 +2,11 @@ import com.alibaba.csp.sentinel.ResourceTypeConstants; import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory; +import com.alibaba.csp.sentinel.context.ContextUtil; import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; import io.microsphere.annotation.Nonnull; +import java.lang.invoke.MethodHandle; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Map; @@ -13,16 +15,20 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadFactory; +import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.DEFAULT_CONTEXT_NAME_PATTERN; +import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.FLOW_DATA_ID_PATTERN; +import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.PROPERTY_NAME_PREFIX; import static io.microsphere.collection.MapUtils.newFixedHashMap; +import static io.microsphere.concurrent.ExecutorUtils.shutdownOnExit; import static io.microsphere.constants.PropertyConstants.ENABLED_PROPERTY_NAME; import static io.microsphere.constants.SymbolConstants.DOT; +import static io.microsphere.invoke.MethodHandleUtils.findStatic; +import static io.microsphere.lang.function.ThrowableAction.execute; import static io.microsphere.reflect.FieldUtils.getFieldValue; import static io.microsphere.reflect.FieldUtils.getStaticFieldValue; -import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.DEFAULT_CONTEXT_NAME_PATTERN; -import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.FLOW_DATA_ID_PATTERN; -import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.PROPERTY_NAME_PREFIX; import static io.microsphere.text.FormatUtils.format; import static io.microsphere.util.ClassUtils.getSimpleName; +import static io.microsphere.util.ShutdownHookUtils.addShutdownHookCallback; import static io.microsphere.util.SystemUtils.getSystemProperty; import static java.lang.reflect.Modifier.isStatic; import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; @@ -37,8 +43,20 @@ public abstract class SentinelUtils { private static final Map resourceTypeToLabelMapping = initResourceTypeToLabelMapping(); + /** + * The {@link MethodHandle} of {@link ContextUtil#resetContextMap()} + * + * @see ContextUtil#resetContextMap() + */ + private static final MethodHandle resetContextMapMethodHandle = findStatic(ContextUtil.class, "resetContextMap"); + private static volatile ScheduledExecutorService sentinelMetricsTaskExecutor; + static { + shutdownOnExit(sentinelMetricsTaskExecutor); + addShutdownHookCallback(() -> execute(SentinelUtils::resetContextMap)); + } + private static Map initResourceTypeToLabelMapping() { Field[] fields = ResourceTypeConstants.class.getFields(); Map resourceTypeToLabelMapping = newFixedHashMap(fields.length); @@ -153,6 +171,16 @@ public static ScheduledExecutorService getSentinelMetricsTaskExecutor() { return scheduledExecutorService; } + /** + * Reset the {@link ContextUtil#resetContextMap() context map} of Sentinel + * + * @throws Throwable if any exception occurred + * @see ContextUtil#resetContextMap() + */ + public static void resetContextMap() throws Throwable { + resetContextMapMethodHandle.invokeExact(); + } + static ScheduledExecutorService getSentinelMetricsTaskExecutor(ScheduledExecutorService defaultScheduledExecutorService) { return defaultScheduledExecutorService == null ? newSingleThreadScheduledExecutor(new NamedThreadFactory("sentinel-metrics-task", true)) : diff --git a/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/jmx/JMXSentinelPluginRepository.java b/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/jmx/JMXSentinelPluginRepository.java index ed9c3a9..c2a44f4 100644 --- a/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/jmx/JMXSentinelPluginRepository.java +++ b/microsphere-alibaba-sentinel-commons/src/main/java/io/microsphere/alibaba/sentinel/jmx/JMXSentinelPluginRepository.java @@ -17,11 +17,11 @@ package io.microsphere.alibaba.sentinel.jmx; -import io.microsphere.lang.Prioritized; -import io.microsphere.lang.function.ThrowableBiFunction; import io.microsphere.alibaba.sentinel.common.SentinelPlugin; import io.microsphere.alibaba.sentinel.common.SentinelPluginRepository; import io.microsphere.alibaba.sentinel.common.SimpleSentinelPluginRepository; +import io.microsphere.lang.Prioritized; +import io.microsphere.lang.function.ThrowableBiFunction; import javax.management.MBeanServer; import javax.management.ObjectName; @@ -29,8 +29,8 @@ import java.util.Collection; import java.util.Set; -import static io.microsphere.lang.function.ThrowableSupplier.execute; import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.DEFAULT_PRIORITY; +import static io.microsphere.lang.function.ThrowableSupplier.execute; import static io.microsphere.text.FormatUtils.format; import static java.lang.management.ManagementFactory.getPlatformMBeanServer; import static java.util.Collections.emptyList; diff --git a/microsphere-alibaba-sentinel-commons/src/test/java/io/microsphere/alibaba/sentinel/common/util/SentinelUtilsTest.java b/microsphere-alibaba-sentinel-commons/src/test/java/io/microsphere/alibaba/sentinel/common/util/SentinelUtilsTest.java index 2fa9828..3dafe8a 100644 --- a/microsphere-alibaba-sentinel-commons/src/test/java/io/microsphere/alibaba/sentinel/common/util/SentinelUtilsTest.java +++ b/microsphere-alibaba-sentinel-commons/src/test/java/io/microsphere/alibaba/sentinel/common/util/SentinelUtilsTest.java @@ -9,7 +9,6 @@ import static com.alibaba.csp.sentinel.ResourceTypeConstants.COMMON_DB_SQL; import static com.alibaba.csp.sentinel.ResourceTypeConstants.COMMON_RPC; import static com.alibaba.csp.sentinel.ResourceTypeConstants.COMMON_WEB; -import static io.microsphere.reflect.MethodUtils.findMethod; import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.DEFAULT_CONTEXT_NAME; import static io.microsphere.alibaba.sentinel.common.util.SentinelUtils.buildResourceName; import static io.microsphere.alibaba.sentinel.common.util.SentinelUtils.findSentinelMetricsTaskExecutor; @@ -19,6 +18,8 @@ import static io.microsphere.alibaba.sentinel.common.util.SentinelUtils.getResourceTypeAsString; import static io.microsphere.alibaba.sentinel.common.util.SentinelUtils.getSentinelMetricsTaskExecutor; import static io.microsphere.alibaba.sentinel.common.util.SentinelUtils.isPluginEnabled; +import static io.microsphere.reflect.MethodUtils.findMethod; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -82,4 +83,9 @@ void testGetSentinelMetricsTaskExecutor() { assertSame(findSentinelMetricsTaskExecutor(), getSentinelMetricsTaskExecutor()); assertNotEquals(findSentinelMetricsTaskExecutor(), getSentinelMetricsTaskExecutor(null)); } + + @Test + void testResetContextMap() { + assertDoesNotThrow(SentinelUtils::resetContextMap); + } } \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-parent/pom.xml b/microsphere-alibaba-sentinel-parent/pom.xml index b4b9ce7..fe1ffe1 100644 --- a/microsphere-alibaba-sentinel-parent/pom.xml +++ b/microsphere-alibaba-sentinel-parent/pom.xml @@ -20,11 +20,11 @@ - 0.1.16 - 0.1.16 - 0.1.9 - 0.1.5 - 0.1.8 + 0.1.23 + 0.1.18 + 0.1.13 + 0.1.7 + 0.1.11 1.8.10 2.0.62 diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/pom.xml b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/pom.xml index aebdb1a..a591751 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/pom.xml +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/pom.xml @@ -20,6 +20,13 @@ + + + io.github.microsphere-projects + microsphere-annotation-processor + true + + io.github.microsphere-projects @@ -34,10 +41,12 @@ true - + + + - io.github.microsphere-projects - microsphere-annotation-processor + com.alibaba.csp + sentinel-core true @@ -48,13 +57,6 @@ true - - - com.alibaba.csp - sentinel-core - true - - org.junit.jupiter diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/main/java/io/microsphere/alibaba/sentinel/alibaba/druid/Constants.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/main/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelAlibabaDruidConstants.java similarity index 85% rename from microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/main/java/io/microsphere/alibaba/sentinel/alibaba/druid/Constants.java rename to microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/main/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelAlibabaDruidConstants.java index 34e6107..fa73e3d 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/main/java/io/microsphere/alibaba/sentinel/alibaba/druid/Constants.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/main/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelAlibabaDruidConstants.java @@ -20,17 +20,18 @@ import io.microsphere.annotation.ConfigurationProperty; import io.microsphere.constants.PropertyConstants; +import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.ENABLED_PROPERTY_VALUE; +import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.PROPERTY_NAME_PREFIX; import static io.microsphere.annotation.ConfigurationProperty.SYSTEM_PROPERTIES_SOURCE; import static io.microsphere.constants.SymbolConstants.DOT; -import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.PROPERTY_NAME_PREFIX; /** - * The interface to declare the constants of Alibaba Druid + * The interface to declare the constants of Sentinel x Alibaba Druid * * @author Mercy * @since 1.0.0 */ -public interface Constants { +public interface SentinelAlibabaDruidConstants { /** * The plugin name of Sentinel x Alibaba Druid @@ -48,11 +49,11 @@ public interface Constants { String DEFAULT_ORIGIN = "Filter"; /** - * The property name of the plugin of Sentinel x Alibaba Druid enabled + * The property name of the plugin of Sentinel x Alibaba Druid enabled : "microsphere.sentinel.alibaba-druid.enabled" */ @ConfigurationProperty( type = boolean.class, - defaultValue = "true", + defaultValue = ENABLED_PROPERTY_VALUE, source = SYSTEM_PROPERTIES_SOURCE ) String ENABLED_PROPERTY_NAME = PROPERTY_NAME_PREFIX + PLUGIN_NAME + DOT + PropertyConstants.ENABLED_PROPERTY_NAME; diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/main/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelDruidFilter.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/main/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelAlibabaDruidFilter.java similarity index 87% rename from microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/main/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelDruidFilter.java rename to microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/main/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelAlibabaDruidFilter.java index c4c2d33..7fd5e32 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/main/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelDruidFilter.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/main/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelAlibabaDruidFilter.java @@ -17,7 +17,6 @@ package io.microsphere.alibaba.sentinel.alibaba.druid; import com.alibaba.csp.sentinel.EntryType; -import com.alibaba.druid.filter.AutoLoad; import com.alibaba.druid.filter.Filter; import com.alibaba.druid.filter.FilterAdapter; import com.alibaba.druid.proxy.jdbc.StatementProxy; @@ -30,9 +29,9 @@ import static com.alibaba.csp.sentinel.EntryType.IN; import static com.alibaba.csp.sentinel.ResourceTypeConstants.COMMON_DB_SQL; -import static io.microsphere.alibaba.sentinel.alibaba.druid.Constants.DEFAULT_CONTEXT_NAME; -import static io.microsphere.alibaba.sentinel.alibaba.druid.Constants.DEFAULT_ORIGIN; -import static io.microsphere.alibaba.sentinel.alibaba.druid.Constants.PLUGIN_NAME; +import static io.microsphere.alibaba.sentinel.alibaba.druid.SentinelAlibabaDruidConstants.DEFAULT_CONTEXT_NAME; +import static io.microsphere.alibaba.sentinel.alibaba.druid.SentinelAlibabaDruidConstants.DEFAULT_ORIGIN; +import static io.microsphere.alibaba.sentinel.alibaba.druid.SentinelAlibabaDruidConstants.PLUGIN_NAME; import static io.microsphere.alibaba.sentinel.common.SentinelContext.doInContext; import static io.microsphere.alibaba.sentinel.common.SentinelPlugin.install; @@ -44,18 +43,17 @@ * @see FilterAdapter * @since 1.0.0 */ -@AutoLoad -public class SentinelDruidFilter extends AbstractStatementFilter implements SentinelPlugin { +public class SentinelAlibabaDruidFilter extends AbstractStatementFilter implements SentinelPlugin { private final SentinelPlugin delegate; private final SentinelOperations sentinelOperations; - public SentinelDruidFilter() { + public SentinelAlibabaDruidFilter() { this(DEFAULT_CONTEXT_NAME, DEFAULT_ORIGIN); } - public SentinelDruidFilter(String contextName, String origin) { + public SentinelAlibabaDruidFilter(String contextName, String origin) { this.delegate = new SimpleSentinelPlugin(PLUGIN_NAME, contextName, origin, COMMON_DB_SQL, IN, false); this.sentinelOperations = new SentinelTemplate(getResourceType(), getTrafficType()); install(this); diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/main/resources/META-INF/services/com.alibaba.druid.filter.Filter b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/main/resources/META-INF/services/com.alibaba.druid.filter.Filter index 84de885..bab6855 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/main/resources/META-INF/services/com.alibaba.druid.filter.Filter +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/main/resources/META-INF/services/com.alibaba.druid.filter.Filter @@ -1 +1 @@ -io.microsphere.alibaba.sentinel.alibaba.druid.SentinelDruidFilter \ No newline at end of file +io.microsphere.alibaba.sentinel.alibaba.druid.SentinelAlibabaDruidFilter \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/test/java/io/microsphere/alibaba/sentinel/alibaba/druid/ConstantsTest.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/test/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelAlibabaDruidConstantsTest.java similarity index 73% rename from microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/test/java/io/microsphere/alibaba/sentinel/alibaba/druid/ConstantsTest.java rename to microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/test/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelAlibabaDruidConstantsTest.java index efed977..b364a7c 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/test/java/io/microsphere/alibaba/sentinel/alibaba/druid/ConstantsTest.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/test/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelAlibabaDruidConstantsTest.java @@ -19,21 +19,21 @@ import org.junit.jupiter.api.Test; -import static io.microsphere.alibaba.sentinel.alibaba.druid.Constants.DEFAULT_CONTEXT_NAME; -import static io.microsphere.alibaba.sentinel.alibaba.druid.Constants.DEFAULT_ORIGIN; -import static io.microsphere.alibaba.sentinel.alibaba.druid.Constants.ENABLED_PROPERTY_NAME; -import static io.microsphere.alibaba.sentinel.alibaba.druid.Constants.PLUGIN_NAME; +import static io.microsphere.alibaba.sentinel.alibaba.druid.SentinelAlibabaDruidConstants.DEFAULT_CONTEXT_NAME; +import static io.microsphere.alibaba.sentinel.alibaba.druid.SentinelAlibabaDruidConstants.DEFAULT_ORIGIN; +import static io.microsphere.alibaba.sentinel.alibaba.druid.SentinelAlibabaDruidConstants.ENABLED_PROPERTY_NAME; +import static io.microsphere.alibaba.sentinel.alibaba.druid.SentinelAlibabaDruidConstants.PLUGIN_NAME; import static io.microsphere.alibaba.sentinel.common.util.SentinelUtils.getPluginEnabledPropertyName; import static org.junit.jupiter.api.Assertions.assertEquals; /** - * {@link Constants} Test + * {@link SentinelAlibabaDruidConstants} Test * * @author Mercy - * @see Constants + * @see SentinelAlibabaDruidConstants * @since 1.0.0 */ -class ConstantsTest { +class SentinelAlibabaDruidConstantsTest { @Test void testConstants() { diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/test/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelDruidFilterTest.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/test/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelAlibabaDruidFilterTest.java similarity index 58% rename from microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/test/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelDruidFilterTest.java rename to microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/test/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelAlibabaDruidFilterTest.java index e8e8305..24f6596 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/test/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelDruidFilterTest.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-alibaba-druid/src/test/java/io/microsphere/alibaba/sentinel/alibaba/druid/SentinelAlibabaDruidFilterTest.java @@ -17,44 +17,66 @@ package io.microsphere.alibaba.sentinel.alibaba.druid; +import com.alibaba.csp.sentinel.context.Context; import com.alibaba.druid.filter.Filter; import com.alibaba.druid.pool.DruidDataSource; import io.microsphere.alibaba.druid.test.AbstractAlibabaDruidTest; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.List; -import static io.microsphere.alibaba.sentinel.alibaba.druid.Constants.PLUGIN_NAME; +import static com.alibaba.csp.sentinel.context.ContextUtil.enter; +import static io.microsphere.alibaba.sentinel.alibaba.druid.SentinelAlibabaDruidConstants.PLUGIN_NAME; +import static io.microsphere.alibaba.sentinel.common.util.SentinelUtils.resetContextMap; import static org.junit.jupiter.api.Assertions.assertEquals; /** - * {@link SentinelDruidFilter} Testt + * {@link SentinelAlibabaDruidFilter} Testt * * @author Mercy - * @see SentinelDruidFilter + * @see SentinelAlibabaDruidFilter * @see AbstractAlibabaDruidTest * @since 1.0.0 */ -class SentinelDruidFilterTest extends AbstractAlibabaDruidTest { +class SentinelAlibabaDruidFilterTest extends AbstractAlibabaDruidTest { + + private final SentinelAlibabaDruidFilter filter = new SentinelAlibabaDruidFilter(); + + @BeforeEach + void setUp() throws Throwable { + resetContextMap(); + } + + @Override + protected void customize(DruidDataSource dataSource) { + dataSource.getProxyFilters().add(filter); + } @Test void testEnable() throws Throwable { setEnable(true); super.test(); + + Context context = enter(filter.getContextName(), filter.getOrigin()); + assertEquals("microsphere_sentinel_alibaba_druid_context", context.getName()); } @Test void testDisable() throws Throwable { setEnable(false); super.test(); + + Context context = enter(filter.getContextName(), filter.getOrigin()); + assertEquals("microsphere_sentinel_alibaba_druid_context", context.getName()); } void setEnable(boolean enabled) { DruidDataSource dataSource = getDruidDataSource(); List proxyFilters = dataSource.getProxyFilters(); for (Filter proxyFilter : proxyFilters) { - if (proxyFilter instanceof SentinelDruidFilter) { - SentinelDruidFilter filter = (SentinelDruidFilter) proxyFilter; + if (proxyFilter instanceof SentinelAlibabaDruidFilter) { + SentinelAlibabaDruidFilter filter = (SentinelAlibabaDruidFilter) proxyFilter; assertEquals(PLUGIN_NAME, filter.getName()); filter.setEnabled(enabled); } diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/pom.xml b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/pom.xml index 5bd131e..773d404 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/pom.xml +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/pom.xml @@ -20,6 +20,13 @@ + + + io.github.microsphere-projects + microsphere-annotation-processor + true + + io.github.microsphere-projects @@ -31,15 +38,11 @@ io.github.microsphere-projects microsphere-hibernate-core - - - - - io.github.microsphere-projects - microsphere-annotation-processor true + + com.alibaba.csp diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/main/java/io/microsphere/alibaba/sentinel/hibernate/Constants.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/main/java/io/microsphere/alibaba/sentinel/hibernate/SentinelHibernateConstants.java similarity index 88% rename from microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/main/java/io/microsphere/alibaba/sentinel/hibernate/Constants.java rename to microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/main/java/io/microsphere/alibaba/sentinel/hibernate/SentinelHibernateConstants.java index 6749e1e..431517d 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/main/java/io/microsphere/alibaba/sentinel/hibernate/Constants.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/main/java/io/microsphere/alibaba/sentinel/hibernate/SentinelHibernateConstants.java @@ -20,17 +20,18 @@ import io.microsphere.annotation.ConfigurationProperty; import io.microsphere.constants.PropertyConstants; +import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.ENABLED_PROPERTY_VALUE; +import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.PROPERTY_NAME_PREFIX; import static io.microsphere.annotation.ConfigurationProperty.SYSTEM_PROPERTIES_SOURCE; import static io.microsphere.constants.SymbolConstants.DOT; -import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.PROPERTY_NAME_PREFIX; /** - * The interface to declare the constants of Hibernate + * The interface to declare the constants of Sentinel x Hibernate * * @author Mercy * @since 1.0.0 */ -public interface Constants { +public interface SentinelHibernateConstants { /** * The plugin name of Sentinel x Hibernate @@ -52,7 +53,7 @@ public interface Constants { */ @ConfigurationProperty( type = boolean.class, - defaultValue = "true", + defaultValue = ENABLED_PROPERTY_VALUE, source = SYSTEM_PROPERTIES_SOURCE ) String ENABLED_PROPERTY_NAME = PROPERTY_NAME_PREFIX + PLUGIN_NAME + DOT + PropertyConstants.ENABLED_PROPERTY_NAME; diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/main/java/io/microsphere/alibaba/sentinel/hibernate/entity/SentinelHibernateEntityCallback.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/main/java/io/microsphere/alibaba/sentinel/hibernate/entity/SentinelHibernateEntityCallback.java index 4a1f1bd..1c16e19 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/main/java/io/microsphere/alibaba/sentinel/hibernate/entity/SentinelHibernateEntityCallback.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/main/java/io/microsphere/alibaba/sentinel/hibernate/entity/SentinelHibernateEntityCallback.java @@ -18,25 +18,25 @@ package io.microsphere.alibaba.sentinel.hibernate.entity; import com.alibaba.csp.sentinel.EntryType; -import io.microsphere.annotation.Nonnull; -import io.microsphere.hibernate.entity.EntityCallback; import io.microsphere.alibaba.sentinel.common.SentinelContext; import io.microsphere.alibaba.sentinel.common.SentinelOperations; import io.microsphere.alibaba.sentinel.common.SentinelPlugin; import io.microsphere.alibaba.sentinel.common.SentinelTemplate; import io.microsphere.alibaba.sentinel.common.SimpleSentinelPlugin; +import io.microsphere.annotation.Nonnull; +import io.microsphere.hibernate.entity.EntityCallback; import org.hibernate.type.Type; import java.util.Optional; import static com.alibaba.csp.sentinel.EntryType.IN; import static com.alibaba.csp.sentinel.ResourceTypeConstants.COMMON_DB_SQL; -import static io.microsphere.lang.function.ThrowableSupplier.execute; import static io.microsphere.alibaba.sentinel.common.SentinelContext.doInContext; import static io.microsphere.alibaba.sentinel.common.SentinelPlugin.install; -import static io.microsphere.alibaba.sentinel.hibernate.Constants.DEFAULT_CONTEXT_NAME; -import static io.microsphere.alibaba.sentinel.hibernate.Constants.DEFAULT_ORIGIN; -import static io.microsphere.alibaba.sentinel.hibernate.Constants.PLUGIN_NAME; +import static io.microsphere.alibaba.sentinel.hibernate.SentinelHibernateConstants.DEFAULT_CONTEXT_NAME; +import static io.microsphere.alibaba.sentinel.hibernate.SentinelHibernateConstants.DEFAULT_ORIGIN; +import static io.microsphere.alibaba.sentinel.hibernate.SentinelHibernateConstants.PLUGIN_NAME; +import static io.microsphere.lang.function.ThrowableSupplier.execute; import static java.util.Optional.empty; import static java.util.Optional.of; diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/test/java/io/microsphere/alibaba/sentinel/hibernate/ConstantsTest.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/test/java/io/microsphere/alibaba/sentinel/hibernate/SentinelHibernateConstantsTest.java similarity index 74% rename from microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/test/java/io/microsphere/alibaba/sentinel/hibernate/ConstantsTest.java rename to microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/test/java/io/microsphere/alibaba/sentinel/hibernate/SentinelHibernateConstantsTest.java index 0570565..1145b49 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/test/java/io/microsphere/alibaba/sentinel/hibernate/ConstantsTest.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/test/java/io/microsphere/alibaba/sentinel/hibernate/SentinelHibernateConstantsTest.java @@ -19,21 +19,21 @@ import org.junit.jupiter.api.Test; -import static io.microsphere.alibaba.sentinel.hibernate.Constants.DEFAULT_CONTEXT_NAME; -import static io.microsphere.alibaba.sentinel.hibernate.Constants.DEFAULT_ORIGIN; -import static io.microsphere.alibaba.sentinel.hibernate.Constants.ENABLED_PROPERTY_NAME; -import static io.microsphere.alibaba.sentinel.hibernate.Constants.PLUGIN_NAME; import static io.microsphere.alibaba.sentinel.common.util.SentinelUtils.getPluginEnabledPropertyName; +import static io.microsphere.alibaba.sentinel.hibernate.SentinelHibernateConstants.DEFAULT_CONTEXT_NAME; +import static io.microsphere.alibaba.sentinel.hibernate.SentinelHibernateConstants.DEFAULT_ORIGIN; +import static io.microsphere.alibaba.sentinel.hibernate.SentinelHibernateConstants.ENABLED_PROPERTY_NAME; +import static io.microsphere.alibaba.sentinel.hibernate.SentinelHibernateConstants.PLUGIN_NAME; import static org.junit.jupiter.api.Assertions.assertEquals; /** - * {@link Constants} Test + * {@link SentinelHibernateConstants} Test * * @author Mercy - * @see Constants + * @see SentinelHibernateConstants * @since 1.0.0 */ -class ConstantsTest { +class SentinelHibernateConstantsTest { @Test void testConstants() { diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/test/java/io/microsphere/alibaba/sentinel/hibernate/entity/SentinelHibernateEntityCallbackDisabledTest.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/test/java/io/microsphere/alibaba/sentinel/hibernate/entity/SentinelHibernateEntityCallbackDisabledTest.java index d532649..6a5ba7c 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/test/java/io/microsphere/alibaba/sentinel/hibernate/entity/SentinelHibernateEntityCallbackDisabledTest.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/test/java/io/microsphere/alibaba/sentinel/hibernate/entity/SentinelHibernateEntityCallbackDisabledTest.java @@ -21,7 +21,7 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; -import static io.microsphere.alibaba.sentinel.hibernate.Constants.ENABLED_PROPERTY_NAME; +import static io.microsphere.alibaba.sentinel.hibernate.SentinelHibernateConstants.ENABLED_PROPERTY_NAME; import static java.lang.System.setProperty; /** diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/test/java/io/microsphere/alibaba/sentinel/hibernate/entity/SentinelHibernateEntityCallbackTest.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/test/java/io/microsphere/alibaba/sentinel/hibernate/entity/SentinelHibernateEntityCallbackTest.java index ad0e38f..a65b3bf 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/test/java/io/microsphere/alibaba/sentinel/hibernate/entity/SentinelHibernateEntityCallbackTest.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-hibernate-core/src/test/java/io/microsphere/alibaba/sentinel/hibernate/entity/SentinelHibernateEntityCallbackTest.java @@ -17,7 +17,16 @@ package io.microsphere.alibaba.sentinel.hibernate.entity; +import com.alibaba.csp.sentinel.context.Context; +import com.alibaba.csp.sentinel.node.DefaultNode; import io.microsphere.hibernate.test.AbstractHibernateH2Test; +import org.junit.jupiter.api.AfterEach; + +import static com.alibaba.csp.sentinel.context.ContextUtil.enter; +import static com.alibaba.csp.sentinel.context.ContextUtil.exit; +import static io.microsphere.alibaba.sentinel.hibernate.SentinelHibernateConstants.DEFAULT_CONTEXT_NAME; +import static io.microsphere.alibaba.sentinel.hibernate.SentinelHibernateConstants.DEFAULT_ORIGIN; +import static org.junit.jupiter.api.Assertions.assertNotNull; /** * {@link SentinelHibernateEntityCallback} Test @@ -27,4 +36,12 @@ * @since 1.0.0 */ class SentinelHibernateEntityCallbackTest extends AbstractHibernateH2Test { + + @AfterEach + void postTest() { + Context context = enter(DEFAULT_CONTEXT_NAME, DEFAULT_ORIGIN); + DefaultNode entranceNode = context.getEntranceNode(); + assertNotNull(entranceNode); + exit(); + } } \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/pom.xml b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/pom.xml index 5c37edf..0629976 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/pom.xml +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/pom.xml @@ -20,6 +20,13 @@ + + + io.github.microsphere-projects + microsphere-annotation-processor + true + + io.github.microsphere-projects @@ -27,13 +34,15 @@ ${revision} - + io.github.microsphere-projects - microsphere-annotation-processor + microsphere-mybatis-core true + + com.alibaba.csp @@ -41,12 +50,6 @@ true - - - io.github.microsphere-projects - microsphere-mybatis-core - - org.mybatis diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/main/java/io/microsphere/alibaba/sentinel/mybatis/Constants.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/main/java/io/microsphere/alibaba/sentinel/mybatis/SentinelMyBatisConstants.java similarity index 86% rename from microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/main/java/io/microsphere/alibaba/sentinel/mybatis/Constants.java rename to microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/main/java/io/microsphere/alibaba/sentinel/mybatis/SentinelMyBatisConstants.java index 2073df7..e487daf 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/main/java/io/microsphere/alibaba/sentinel/mybatis/Constants.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/main/java/io/microsphere/alibaba/sentinel/mybatis/SentinelMyBatisConstants.java @@ -20,17 +20,18 @@ import io.microsphere.annotation.ConfigurationProperty; import io.microsphere.constants.PropertyConstants; +import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.ENABLED_PROPERTY_VALUE; +import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.PROPERTY_NAME_PREFIX; import static io.microsphere.annotation.ConfigurationProperty.SYSTEM_PROPERTIES_SOURCE; import static io.microsphere.constants.SymbolConstants.DOT; -import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.PROPERTY_NAME_PREFIX; /** - * The interface to declare the constants of MyBatis + * The interface to declare the constants of Sentinel x MyBatis * * @author Mercy * @since 1.0.0 */ -public interface Constants { +public interface SentinelMyBatisConstants { /** * The plugin name of Sentinel x MyBatis @@ -48,11 +49,11 @@ public interface Constants { String DEFAULT_ORIGIN = "Executor"; /** - * The property name of the plugin of Sentinel x MyBatis enabled + * The property name of the plugin of Sentinel x MyBatis enabled : "microsphere.sentinel.mybatis.enabled" */ @ConfigurationProperty( type = boolean.class, - defaultValue = "true", + defaultValue = ENABLED_PROPERTY_VALUE, source = SYSTEM_PROPERTIES_SOURCE ) String ENABLED_PROPERTY_NAME = PROPERTY_NAME_PREFIX + PLUGIN_NAME + DOT + PropertyConstants.ENABLED_PROPERTY_NAME; diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/main/java/io/microsphere/alibaba/sentinel/mybatis/executor/SentinelMyBatisExecutorFilter.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/main/java/io/microsphere/alibaba/sentinel/mybatis/executor/SentinelMyBatisExecutorFilter.java index 3174353..e989b80 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/main/java/io/microsphere/alibaba/sentinel/mybatis/executor/SentinelMyBatisExecutorFilter.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/main/java/io/microsphere/alibaba/sentinel/mybatis/executor/SentinelMyBatisExecutorFilter.java @@ -17,11 +17,11 @@ package io.microsphere.alibaba.sentinel.mybatis.executor; -import io.microsphere.mybatis.executor.ExecutorFilter; -import io.microsphere.mybatis.executor.ExecutorFilterChain; import io.microsphere.alibaba.sentinel.common.AbstractSentinelPlugin; import io.microsphere.alibaba.sentinel.common.SentinelOperations; import io.microsphere.alibaba.sentinel.common.SentinelTemplate; +import io.microsphere.mybatis.executor.ExecutorFilter; +import io.microsphere.mybatis.executor.ExecutorFilterChain; import org.apache.ibatis.cache.CacheKey; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; @@ -33,9 +33,9 @@ import java.util.concurrent.Callable; import static com.alibaba.csp.sentinel.ResourceTypeConstants.COMMON_DB_SQL; -import static io.microsphere.alibaba.sentinel.mybatis.Constants.DEFAULT_CONTEXT_NAME; -import static io.microsphere.alibaba.sentinel.mybatis.Constants.DEFAULT_ORIGIN; -import static io.microsphere.alibaba.sentinel.mybatis.Constants.PLUGIN_NAME; +import static io.microsphere.alibaba.sentinel.mybatis.SentinelMyBatisConstants.DEFAULT_CONTEXT_NAME; +import static io.microsphere.alibaba.sentinel.mybatis.SentinelMyBatisConstants.DEFAULT_ORIGIN; +import static io.microsphere.alibaba.sentinel.mybatis.SentinelMyBatisConstants.PLUGIN_NAME; import static io.microsphere.util.ExceptionUtils.throwTarget; /** diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/test/java/io/microsphere/alibaba/sentinel/mybatis/ConstantsTest.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/test/java/io/microsphere/alibaba/sentinel/mybatis/SentinelMyBatisConstantsTest.java similarity index 75% rename from microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/test/java/io/microsphere/alibaba/sentinel/mybatis/ConstantsTest.java rename to microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/test/java/io/microsphere/alibaba/sentinel/mybatis/SentinelMyBatisConstantsTest.java index 6cb2e77..63e1e32 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/test/java/io/microsphere/alibaba/sentinel/mybatis/ConstantsTest.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/test/java/io/microsphere/alibaba/sentinel/mybatis/SentinelMyBatisConstantsTest.java @@ -19,21 +19,21 @@ import org.junit.jupiter.api.Test; -import static io.microsphere.alibaba.sentinel.mybatis.Constants.DEFAULT_CONTEXT_NAME; -import static io.microsphere.alibaba.sentinel.mybatis.Constants.DEFAULT_ORIGIN; -import static io.microsphere.alibaba.sentinel.mybatis.Constants.ENABLED_PROPERTY_NAME; -import static io.microsphere.alibaba.sentinel.mybatis.Constants.PLUGIN_NAME; import static io.microsphere.alibaba.sentinel.common.util.SentinelUtils.getPluginEnabledPropertyName; +import static io.microsphere.alibaba.sentinel.mybatis.SentinelMyBatisConstants.DEFAULT_CONTEXT_NAME; +import static io.microsphere.alibaba.sentinel.mybatis.SentinelMyBatisConstants.DEFAULT_ORIGIN; +import static io.microsphere.alibaba.sentinel.mybatis.SentinelMyBatisConstants.ENABLED_PROPERTY_NAME; +import static io.microsphere.alibaba.sentinel.mybatis.SentinelMyBatisConstants.PLUGIN_NAME; import static org.junit.jupiter.api.Assertions.assertEquals; /** - * {@link Constants} Test + * {@link SentinelMyBatisConstants} Test * * @author Mercy - * @see Constants + * @see SentinelMyBatisConstants * @since 1.0.0 */ -class ConstantsTest { +class SentinelMyBatisConstantsTest { @Test void testConstants() { diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/test/java/io/microsphere/alibaba/sentinel/mybatis/executor/SentinelMyBatisExecutorFilterTest.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/test/java/io/microsphere/alibaba/sentinel/mybatis/executor/SentinelMyBatisExecutorFilterTest.java index 908a99a..53e8587 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/test/java/io/microsphere/alibaba/sentinel/mybatis/executor/SentinelMyBatisExecutorFilterTest.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-mybatis/src/test/java/io/microsphere/alibaba/sentinel/mybatis/executor/SentinelMyBatisExecutorFilterTest.java @@ -17,12 +17,23 @@ package io.microsphere.alibaba.sentinel.mybatis.executor; +import com.alibaba.csp.sentinel.context.Context; +import com.alibaba.csp.sentinel.node.DefaultNode; +import com.alibaba.csp.sentinel.node.Node; import io.microsphere.mybatis.plugin.InterceptingExecutorInterceptor; import io.microsphere.mybatis.test.AbstractMapperTest; import org.apache.ibatis.session.Configuration; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import java.util.Set; + +import static com.alibaba.csp.sentinel.context.ContextUtil.enter; +import static com.alibaba.csp.sentinel.context.ContextUtil.exit; +import static io.microsphere.alibaba.sentinel.mybatis.SentinelMyBatisConstants.DEFAULT_CONTEXT_NAME; +import static io.microsphere.alibaba.sentinel.mybatis.SentinelMyBatisConstants.DEFAULT_ORIGIN; import static io.microsphere.util.ArrayUtils.ofArray; +import static org.junit.jupiter.api.Assertions.assertFalse; /** * {@link SentinelMyBatisExecutorFilter} Test @@ -47,4 +58,13 @@ void testDisabled() throws Throwable { this.filter.setEnabled(false); super.testMapper(); } + + @AfterEach + void postTest() { + Context context = enter(DEFAULT_CONTEXT_NAME, DEFAULT_ORIGIN); + DefaultNode entranceNode = context.getEntranceNode(); + Set childList = entranceNode.getChildList(); + assertFalse(childList.isEmpty()); + exit(); + } } \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/pom.xml b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/pom.xml index bb034d6..8ca9f01 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/pom.xml +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/pom.xml @@ -20,20 +20,22 @@ - + io.github.microsphere-projects - microsphere-alibaba-sentinel-commons - ${revision} + microsphere-annotation-processor + true - + io.github.microsphere-projects - microsphere-annotation-processor - true + microsphere-alibaba-sentinel-commons + ${revision} + + com.alibaba.csp diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/main/java/io/microsphere/alibaba/sentinel/p6spy/SentinelJdbcEventListener.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/main/java/io/microsphere/alibaba/sentinel/p6spy/SentinelJdbcEventListener.java index 6efc21f..2a10b1f 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/main/java/io/microsphere/alibaba/sentinel/p6spy/SentinelJdbcEventListener.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/main/java/io/microsphere/alibaba/sentinel/p6spy/SentinelJdbcEventListener.java @@ -21,24 +21,24 @@ import com.p6spy.engine.common.StatementInformation; import com.p6spy.engine.event.JdbcEventListener; import com.p6spy.engine.event.SimpleJdbcEventListener; -import io.microsphere.logging.Logger; import io.microsphere.alibaba.sentinel.common.SentinelContext; import io.microsphere.alibaba.sentinel.common.SentinelOperations; import io.microsphere.alibaba.sentinel.common.SentinelPlugin; import io.microsphere.alibaba.sentinel.common.SentinelTemplate; import io.microsphere.alibaba.sentinel.common.SimpleSentinelPlugin; +import io.microsphere.logging.Logger; import java.sql.SQLException; import static com.alibaba.csp.sentinel.EntryType.IN; import static com.alibaba.csp.sentinel.ResourceTypeConstants.COMMON_DB_SQL; -import static io.microsphere.lang.function.ThrowableAction.execute; -import static io.microsphere.logging.LoggerFactory.getLogger; import static io.microsphere.alibaba.sentinel.common.SentinelContext.doInContext; import static io.microsphere.alibaba.sentinel.common.SentinelPlugin.install; -import static io.microsphere.alibaba.sentinel.p6spy.Constants.DEFAULT_CONTEXT_NAME; -import static io.microsphere.alibaba.sentinel.p6spy.Constants.DEFAULT_ORIGIN; -import static io.microsphere.alibaba.sentinel.p6spy.Constants.PLUGIN_NAME; +import static io.microsphere.alibaba.sentinel.p6spy.SentinelP6SpyConstants.DEFAULT_CONTEXT_NAME; +import static io.microsphere.alibaba.sentinel.p6spy.SentinelP6SpyConstants.DEFAULT_ORIGIN; +import static io.microsphere.alibaba.sentinel.p6spy.SentinelP6SpyConstants.PLUGIN_NAME; +import static io.microsphere.lang.function.ThrowableAction.execute; +import static io.microsphere.logging.LoggerFactory.getLogger; /** * P6Spy {@link JdbcEventListener} based on Alibaba Sentinel diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/main/java/io/microsphere/alibaba/sentinel/p6spy/Constants.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/main/java/io/microsphere/alibaba/sentinel/p6spy/SentinelP6SpyConstants.java similarity index 88% rename from microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/main/java/io/microsphere/alibaba/sentinel/p6spy/Constants.java rename to microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/main/java/io/microsphere/alibaba/sentinel/p6spy/SentinelP6SpyConstants.java index b5cc51d..3597c9d 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/main/java/io/microsphere/alibaba/sentinel/p6spy/Constants.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/main/java/io/microsphere/alibaba/sentinel/p6spy/SentinelP6SpyConstants.java @@ -20,17 +20,18 @@ import io.microsphere.annotation.ConfigurationProperty; import io.microsphere.constants.PropertyConstants; +import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.ENABLED_PROPERTY_VALUE; +import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.PROPERTY_NAME_PREFIX; import static io.microsphere.annotation.ConfigurationProperty.SYSTEM_PROPERTIES_SOURCE; import static io.microsphere.constants.SymbolConstants.DOT; -import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.PROPERTY_NAME_PREFIX; /** - * The interface to declare the constants of P6Spy + * The interface to declare the constants of Sentinel x P6Spy * * @author Mercy * @since 1.0.0 */ -public interface Constants { +public interface SentinelP6SpyConstants { /** * The plugin name of Sentinel x P6Spy @@ -52,7 +53,7 @@ public interface Constants { */ @ConfigurationProperty( type = boolean.class, - defaultValue = "true", + defaultValue = ENABLED_PROPERTY_VALUE, source = SYSTEM_PROPERTIES_SOURCE ) String ENABLED_PROPERTY_NAME = PROPERTY_NAME_PREFIX + PLUGIN_NAME + DOT + PropertyConstants.ENABLED_PROPERTY_NAME; diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/test/java/io/microsphere/alibaba/sentinel/p6spy/SentinelJdbcEventListenerTest.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/test/java/io/microsphere/alibaba/sentinel/p6spy/SentinelJdbcEventListenerTest.java index f19c08a..83a97e5 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/test/java/io/microsphere/alibaba/sentinel/p6spy/SentinelJdbcEventListenerTest.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/test/java/io/microsphere/alibaba/sentinel/p6spy/SentinelJdbcEventListenerTest.java @@ -17,13 +17,21 @@ package io.microsphere.alibaba.sentinel.p6spy; +import com.alibaba.csp.sentinel.context.Context; +import com.alibaba.csp.sentinel.node.DefaultNode; +import com.alibaba.csp.sentinel.node.Node; import com.alibaba.druid.pool.DruidDataSource; import io.microsphere.alibaba.druid.test.AbstractAlibabaDruidTest; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; -import static io.microsphere.alibaba.sentinel.p6spy.Constants.DEFAULT_CONTEXT_NAME; -import static io.microsphere.alibaba.sentinel.p6spy.Constants.DEFAULT_ORIGIN; -import static io.microsphere.alibaba.sentinel.p6spy.Constants.PLUGIN_NAME; +import java.util.Set; + +import static com.alibaba.csp.sentinel.context.ContextUtil.enter; +import static com.alibaba.csp.sentinel.context.ContextUtil.exit; +import static io.microsphere.alibaba.sentinel.p6spy.SentinelP6SpyConstants.DEFAULT_CONTEXT_NAME; +import static io.microsphere.alibaba.sentinel.p6spy.SentinelP6SpyConstants.DEFAULT_ORIGIN; +import static io.microsphere.alibaba.sentinel.p6spy.SentinelP6SpyConstants.PLUGIN_NAME; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -64,4 +72,13 @@ void testDisabled() { listener.onAfterAnyExecute(null, 0L, null); assertFalse(listener.isEnabled()); } + + @AfterEach + void postTest() { + Context context = enter(DEFAULT_CONTEXT_NAME, DEFAULT_ORIGIN); + DefaultNode entranceNode = context.getEntranceNode(); + Set childList = entranceNode.getChildList(); + assertFalse(childList.isEmpty()); + exit(); + } } \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/test/java/io/microsphere/alibaba/sentinel/p6spy/ConstantsTest.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/test/java/io/microsphere/alibaba/sentinel/p6spy/SentinelP6SpyConstantsTest.java similarity index 76% rename from microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/test/java/io/microsphere/alibaba/sentinel/p6spy/ConstantsTest.java rename to microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/test/java/io/microsphere/alibaba/sentinel/p6spy/SentinelP6SpyConstantsTest.java index e92c112..48c6248 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/test/java/io/microsphere/alibaba/sentinel/p6spy/ConstantsTest.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-p6spy/src/test/java/io/microsphere/alibaba/sentinel/p6spy/SentinelP6SpyConstantsTest.java @@ -19,21 +19,21 @@ import org.junit.jupiter.api.Test; -import static io.microsphere.alibaba.sentinel.p6spy.Constants.DEFAULT_CONTEXT_NAME; -import static io.microsphere.alibaba.sentinel.p6spy.Constants.DEFAULT_ORIGIN; -import static io.microsphere.alibaba.sentinel.p6spy.Constants.ENABLED_PROPERTY_NAME; -import static io.microsphere.alibaba.sentinel.p6spy.Constants.PLUGIN_NAME; import static io.microsphere.alibaba.sentinel.common.util.SentinelUtils.getPluginEnabledPropertyName; +import static io.microsphere.alibaba.sentinel.p6spy.SentinelP6SpyConstants.DEFAULT_CONTEXT_NAME; +import static io.microsphere.alibaba.sentinel.p6spy.SentinelP6SpyConstants.DEFAULT_ORIGIN; +import static io.microsphere.alibaba.sentinel.p6spy.SentinelP6SpyConstants.ENABLED_PROPERTY_NAME; +import static io.microsphere.alibaba.sentinel.p6spy.SentinelP6SpyConstants.PLUGIN_NAME; import static org.junit.jupiter.api.Assertions.assertEquals; /** - * {@link Constants} Test + * {@link SentinelP6SpyConstants} Test * * @author Mercy - * @see Constants + * @see SentinelP6SpyConstants * @since 1.0.0 */ -class ConstantsTest { +class SentinelP6SpyConstantsTest { @Test void testConstants() { diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/pom.xml b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/pom.xml index 6162b87..5b74a6b 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/pom.xml +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/pom.xml @@ -20,6 +20,13 @@ + + + io.github.microsphere-projects + microsphere-annotation-processor + true + + io.github.microsphere-projects @@ -34,12 +41,7 @@ true - - - io.github.microsphere-projects - microsphere-annotation-processor - true - + @@ -76,6 +78,13 @@ test + + + io.github.microsphere-projects + microsphere-redis-spring-test + test + + org.springframework diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/main/java/io/microsphere/alibaba/sentinel/redis/Constants.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/main/java/io/microsphere/alibaba/sentinel/redis/SentinelRedisConstants.java similarity index 90% rename from microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/main/java/io/microsphere/alibaba/sentinel/redis/Constants.java rename to microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/main/java/io/microsphere/alibaba/sentinel/redis/SentinelRedisConstants.java index 8021864..17bc442 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/main/java/io/microsphere/alibaba/sentinel/redis/Constants.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/main/java/io/microsphere/alibaba/sentinel/redis/SentinelRedisConstants.java @@ -20,10 +20,11 @@ import io.microsphere.annotation.ConfigurationProperty; import io.microsphere.constants.PropertyConstants; +import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.ENABLED_PROPERTY_VALUE; +import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.PROPERTY_NAME_PREFIX; import static io.microsphere.annotation.ConfigurationProperty.APPLICATION_SOURCE; import static io.microsphere.annotation.ConfigurationProperty.SYSTEM_PROPERTIES_SOURCE; import static io.microsphere.constants.SymbolConstants.DOT; -import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.PROPERTY_NAME_PREFIX; /** * The interface to declare the constants of Redis @@ -31,7 +32,7 @@ * @author Mercy * @since 1.0.0 */ -public interface Constants { +public interface SentinelRedisConstants { /** * The plugin name of Sentinel x Redis @@ -49,11 +50,11 @@ public interface Constants { String DEFAULT_ORIGIN = "RedisConnection"; /** - * The property name of the plugin of Sentinel x Redis enabled + * The property name of the plugin of Sentinel x Redis enabled : "microsphere.sentinel.redis.enabled" */ @ConfigurationProperty( type = boolean.class, - defaultValue = "true", + defaultValue = ENABLED_PROPERTY_VALUE, source = { SYSTEM_PROPERTIES_SOURCE, APPLICATION_SOURCE diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/main/java/io/microsphere/alibaba/sentinel/redis/spring/SentinelRedisCommandInterceptor.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/main/java/io/microsphere/alibaba/sentinel/redis/spring/SentinelRedisCommandInterceptor.java index 1ec7b3d..23dbf13 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/main/java/io/microsphere/alibaba/sentinel/redis/spring/SentinelRedisCommandInterceptor.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/main/java/io/microsphere/alibaba/sentinel/redis/spring/SentinelRedisCommandInterceptor.java @@ -16,13 +16,13 @@ */ package io.microsphere.alibaba.sentinel.redis.spring; -import io.microsphere.logging.Logger; -import io.microsphere.redis.spring.interceptor.RedisConnectionInterceptor; -import io.microsphere.redis.spring.interceptor.RedisMethodContext; import io.microsphere.alibaba.sentinel.common.AbstractSentinelPlugin; import io.microsphere.alibaba.sentinel.common.SentinelContext; import io.microsphere.alibaba.sentinel.common.SentinelOperations; import io.microsphere.alibaba.sentinel.common.SentinelTemplate; +import io.microsphere.logging.Logger; +import io.microsphere.redis.spring.interceptor.RedisConnectionInterceptor; +import io.microsphere.redis.spring.interceptor.RedisMethodContext; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.InitializingBean; import org.springframework.data.redis.connection.RedisClusterConnection; @@ -31,14 +31,14 @@ import java.lang.reflect.Method; import java.util.Map; -import static io.microsphere.collection.MapUtils.newFixedHashMap; -import static io.microsphere.logging.LoggerFactory.getLogger; import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.DEFAULT_PRIORITY; import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.SENTINEL_CONTEXT_ATTRIBUTE_NAME; import static io.microsphere.alibaba.sentinel.common.util.SentinelUtils.buildResourceName; -import static io.microsphere.alibaba.sentinel.redis.Constants.DEFAULT_CONTEXT_NAME; -import static io.microsphere.alibaba.sentinel.redis.Constants.DEFAULT_ORIGIN; -import static io.microsphere.alibaba.sentinel.redis.Constants.PLUGIN_NAME; +import static io.microsphere.alibaba.sentinel.redis.SentinelRedisConstants.DEFAULT_CONTEXT_NAME; +import static io.microsphere.alibaba.sentinel.redis.SentinelRedisConstants.DEFAULT_ORIGIN; +import static io.microsphere.alibaba.sentinel.redis.SentinelRedisConstants.PLUGIN_NAME; +import static io.microsphere.collection.MapUtils.newFixedHashMap; +import static io.microsphere.logging.LoggerFactory.getLogger; import static org.springframework.util.ClassUtils.getAllInterfacesForClass; /** diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/test/java/io/microsphere/alibaba/sentinel/redis/ConstantsTest.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/test/java/io/microsphere/alibaba/sentinel/redis/SentinelRedisConstantsTest.java similarity index 76% rename from microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/test/java/io/microsphere/alibaba/sentinel/redis/ConstantsTest.java rename to microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/test/java/io/microsphere/alibaba/sentinel/redis/SentinelRedisConstantsTest.java index cd09c76..2c0908f 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/test/java/io/microsphere/alibaba/sentinel/redis/ConstantsTest.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/test/java/io/microsphere/alibaba/sentinel/redis/SentinelRedisConstantsTest.java @@ -20,20 +20,20 @@ import org.junit.jupiter.api.Test; import static io.microsphere.alibaba.sentinel.common.util.SentinelUtils.getPluginEnabledPropertyName; -import static io.microsphere.alibaba.sentinel.redis.Constants.DEFAULT_CONTEXT_NAME; -import static io.microsphere.alibaba.sentinel.redis.Constants.DEFAULT_ORIGIN; -import static io.microsphere.alibaba.sentinel.redis.Constants.ENABLED_PROPERTY_NAME; -import static io.microsphere.alibaba.sentinel.redis.Constants.PLUGIN_NAME; +import static io.microsphere.alibaba.sentinel.redis.SentinelRedisConstants.DEFAULT_CONTEXT_NAME; +import static io.microsphere.alibaba.sentinel.redis.SentinelRedisConstants.DEFAULT_ORIGIN; +import static io.microsphere.alibaba.sentinel.redis.SentinelRedisConstants.ENABLED_PROPERTY_NAME; +import static io.microsphere.alibaba.sentinel.redis.SentinelRedisConstants.PLUGIN_NAME; import static org.junit.jupiter.api.Assertions.assertEquals; /** - * {@link Constants} Test + * {@link SentinelRedisConstants} Test * * @author Mercy - * @see Constants + * @see SentinelRedisConstants * @since 1.0.0 */ -class ConstantsTest { +class SentinelRedisConstantsTest { @Test void testConstants() { diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/test/java/io/microsphere/alibaba/sentinel/redis/spring/SentinelRedisCommandInterceptorTest.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/test/java/io/microsphere/alibaba/sentinel/redis/spring/SentinelRedisCommandInterceptorTest.java index 8601348..65a9318 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/test/java/io/microsphere/alibaba/sentinel/redis/spring/SentinelRedisCommandInterceptorTest.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/test/java/io/microsphere/alibaba/sentinel/redis/spring/SentinelRedisCommandInterceptorTest.java @@ -17,10 +17,13 @@ package io.microsphere.alibaba.sentinel.redis.spring; -import io.microsphere.alibaba.sentinel.redis.spring.test.RedisContextConfig; +import com.alibaba.csp.sentinel.context.Context; +import com.alibaba.csp.sentinel.node.DefaultNode; +import com.alibaba.csp.sentinel.node.Node; import io.microsphere.redis.spring.annotation.EnableRedisInterceptor; import io.microsphere.redis.spring.context.RedisContext; import io.microsphere.redis.spring.interceptor.RedisMethodContext; +import io.microsphere.redis.spring.test.config.RedisConfig; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; @@ -29,8 +32,13 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import java.lang.reflect.Method; +import java.util.Set; +import static com.alibaba.csp.sentinel.context.ContextUtil.enter; +import static com.alibaba.csp.sentinel.context.ContextUtil.exit; import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.DEFAULT_PRIORITY; +import static io.microsphere.alibaba.sentinel.redis.SentinelRedisConstants.DEFAULT_CONTEXT_NAME; +import static io.microsphere.alibaba.sentinel.redis.SentinelRedisConstants.DEFAULT_ORIGIN; import static io.microsphere.util.ArrayUtils.ofArray; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -45,7 +53,7 @@ * @since 1.0.0 */ @SpringJUnitConfig(classes = { - RedisContextConfig.class, + RedisConfig.class, SentinelRedisCommandInterceptorTest.class }) @TestPropertySource(properties = { @@ -72,6 +80,7 @@ void test() { valueOperations.set(key, value); assertEquals(value, valueOperations.get(key)); assertEquals(DEFAULT_PRIORITY, interceptor.getOrder()); + postTest(); } @Test @@ -90,4 +99,12 @@ void testResourceNotFound() { assertDoesNotThrow(() -> this.interceptor.beforeExecute(context)); assertDoesNotThrow(() -> this.interceptor.afterExecute(context, null, null)); } + + void postTest() { + Context context = enter(DEFAULT_CONTEXT_NAME, DEFAULT_ORIGIN); + DefaultNode entranceNode = context.getEntranceNode(); + Set childList = entranceNode.getChildList(); + assertFalse(childList.isEmpty()); + exit(); + } } \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/test/java/io/microsphere/alibaba/sentinel/redis/spring/test/RedisConfig.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/test/java/io/microsphere/alibaba/sentinel/redis/spring/test/RedisConfig.java deleted file mode 100644 index 1bc8d10..0000000 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/test/java/io/microsphere/alibaba/sentinel/redis/spring/test/RedisConfig.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.microsphere.alibaba.sentinel.redis.spring.test; - -import org.springframework.context.annotation.Bean; -import org.springframework.data.redis.connection.RedisConnectionFactory; -import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.core.StringRedisTemplate; - -/** - * The configuration class for Redis testing - * - * @author Mercy - * @see RedisTemplate - * @see StringRedisTemplate - * @see RedisConnectionFactory - * @since 1.0.0 - */ -public class RedisConfig { - - @Bean - public static RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { - RedisTemplate redisTemplate = new RedisTemplate(); - redisTemplate.setConnectionFactory(redisConnectionFactory); - return redisTemplate; - } - - @Bean - public static StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { - StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); - stringRedisTemplate.setConnectionFactory(redisConnectionFactory); - return stringRedisTemplate; - } - - @Bean - public static RedisConnectionFactory redisConnectionFactory() { - LettuceConnectionFactory redisConnectionFactory = new LettuceConnectionFactory("127.0.0.1", 6379); - redisConnectionFactory.afterPropertiesSet(); - redisConnectionFactory.validateConnection(); - return redisConnectionFactory; - } -} \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/test/java/io/microsphere/alibaba/sentinel/redis/spring/test/RedisContextConfig.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/test/java/io/microsphere/alibaba/sentinel/redis/spring/test/RedisContextConfig.java deleted file mode 100644 index 5fe8ccf..0000000 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-redis/src/test/java/io/microsphere/alibaba/sentinel/redis/spring/test/RedisContextConfig.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.microsphere.alibaba.sentinel.redis.spring.test; - -import io.microsphere.redis.spring.config.RedisConfiguration; -import io.microsphere.redis.spring.context.RedisContext; -import org.springframework.context.annotation.Bean; - -import static io.microsphere.redis.spring.context.RedisContext.BEAN_NAME; - -/** - * The configuration class for testing RedisContext - * - * @author Mercy - * @see RedisContext - * @see RedisConfiguration - * @since 1.0.0 - */ -public class RedisContextConfig extends RedisConfig { - - @Bean(BEAN_NAME) - public static RedisContext redisContext() { - return new RedisContext(); - } - - @Bean(RedisConfiguration.BEAN_NAME) - public static RedisConfiguration redisConfiguration() { - return new RedisConfiguration(); - } -} \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/pom.xml b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/pom.xml index 6b46314..2fdcff7 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/pom.xml +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/pom.xml @@ -20,18 +20,18 @@ - + io.github.microsphere-projects - microsphere-alibaba-sentinel-commons - ${revision} + microsphere-annotation-processor + true - + io.github.microsphere-projects - microsphere-spring-context - true + microsphere-alibaba-sentinel-commons + ${revision} @@ -41,18 +41,13 @@ true - - - io.github.microsphere-projects - microsphere-annotation-processor - true - + - + - jakarta.servlet - jakarta.servlet-api - provided + com.alibaba.csp + sentinel-core + true @@ -62,11 +57,11 @@ true - + - com.alibaba.csp - sentinel-core - true + javax.servlet + javax.servlet-api + provided diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/main/java/io/microsphere/alibaba/sentinel/spring/web/SentinelHandlerMethodInterceptor.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/main/java/io/microsphere/alibaba/sentinel/spring/web/SentinelHandlerMethodInterceptor.java index da36b8d..aa3be6b 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/main/java/io/microsphere/alibaba/sentinel/spring/web/SentinelHandlerMethodInterceptor.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/main/java/io/microsphere/alibaba/sentinel/spring/web/SentinelHandlerMethodInterceptor.java @@ -16,13 +16,13 @@ */ package io.microsphere.alibaba.sentinel.spring.web; -import io.microsphere.annotation.Nonnull; -import io.microsphere.logging.Logger; import io.microsphere.alibaba.sentinel.common.AbstractSentinelPlugin; import io.microsphere.alibaba.sentinel.common.SentinelContext; import io.microsphere.alibaba.sentinel.common.SentinelOperations; import io.microsphere.alibaba.sentinel.common.SentinelPlugin; import io.microsphere.alibaba.sentinel.common.SentinelTemplate; +import io.microsphere.annotation.Nonnull; +import io.microsphere.logging.Logger; import io.microsphere.spring.web.annotation.EnableWebExtension; import io.microsphere.spring.web.event.WebEndpointMappingsReadyEvent; import io.microsphere.spring.web.metadata.WebEndpointMapping; @@ -38,13 +38,13 @@ import java.util.Map; import static com.alibaba.csp.sentinel.ResourceTypeConstants.COMMON_WEB; -import static io.microsphere.collection.MapUtils.newFixedHashMap; -import static io.microsphere.logging.LoggerFactory.getLogger; import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.DEFAULT_PRIORITY; import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.SENTINEL_CONTEXT_ATTRIBUTE_NAME; -import static io.microsphere.alibaba.sentinel.spring.web.Constants.DEFAULT_CONTEXT_NAME; -import static io.microsphere.alibaba.sentinel.spring.web.Constants.DEFAULT_ORIGIN; -import static io.microsphere.alibaba.sentinel.spring.web.Constants.PLUGIN_NAME; +import static io.microsphere.alibaba.sentinel.spring.web.SentinelSpringWebConstants.DEFAULT_CONTEXT_NAME; +import static io.microsphere.alibaba.sentinel.spring.web.SentinelSpringWebConstants.DEFAULT_ORIGIN; +import static io.microsphere.alibaba.sentinel.spring.web.SentinelSpringWebConstants.PLUGIN_NAME; +import static io.microsphere.collection.MapUtils.newFixedHashMap; +import static io.microsphere.logging.LoggerFactory.getLogger; import static io.microsphere.spring.web.util.WebScope.REQUEST; /** diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/main/java/io/microsphere/alibaba/sentinel/spring/web/Constants.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/main/java/io/microsphere/alibaba/sentinel/spring/web/SentinelSpringWebConstants.java similarity index 86% rename from microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/main/java/io/microsphere/alibaba/sentinel/spring/web/Constants.java rename to microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/main/java/io/microsphere/alibaba/sentinel/spring/web/SentinelSpringWebConstants.java index c5e9def..a8f0e30 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/main/java/io/microsphere/alibaba/sentinel/spring/web/Constants.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/main/java/io/microsphere/alibaba/sentinel/spring/web/SentinelSpringWebConstants.java @@ -20,18 +20,19 @@ import io.microsphere.annotation.ConfigurationProperty; import io.microsphere.constants.PropertyConstants; +import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.ENABLED_PROPERTY_VALUE; +import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.PROPERTY_NAME_PREFIX; import static io.microsphere.annotation.ConfigurationProperty.APPLICATION_SOURCE; import static io.microsphere.annotation.ConfigurationProperty.SYSTEM_PROPERTIES_SOURCE; import static io.microsphere.constants.SymbolConstants.DOT; -import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.PROPERTY_NAME_PREFIX; /** - * The interface to declare the constants of Spring Web + * The interface to declare the constants of Sentinel x Spring Web * * @author Mercy * @since 1.0.0 */ -public interface Constants { +public interface SentinelSpringWebConstants { /** * The plugin name of Sentinel x Spring Web @@ -49,11 +50,11 @@ public interface Constants { String DEFAULT_ORIGIN = "HandlerMethod"; /** - * The property name of the plugin of Sentinel x Spring Web enabled + * The property name of the plugin of Sentinel x Spring Web enabled : "microsphere.sentinel.spring-web.enabled" */ @ConfigurationProperty( type = boolean.class, - defaultValue = "true", + defaultValue = ENABLED_PROPERTY_VALUE, source = { SYSTEM_PROPERTIES_SOURCE, APPLICATION_SOURCE diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/test/java/io/microsphere/alibaba/sentinel/spring/web/SentinelHandlerMethodInterceptorTest.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/test/java/io/microsphere/alibaba/sentinel/spring/web/SentinelHandlerMethodInterceptorTest.java index 9f22c0a..5a29918 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/test/java/io/microsphere/alibaba/sentinel/spring/web/SentinelHandlerMethodInterceptorTest.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/test/java/io/microsphere/alibaba/sentinel/spring/web/SentinelHandlerMethodInterceptorTest.java @@ -17,6 +17,9 @@ package io.microsphere.alibaba.sentinel.spring.web; +import com.alibaba.csp.sentinel.context.Context; +import com.alibaba.csp.sentinel.node.DefaultNode; +import com.alibaba.csp.sentinel.node.Node; import io.microsphere.spring.test.web.context.request.MockServletWebRequest; import io.microsphere.spring.test.webmvc.AbstractWebMvcTest; import io.microsphere.spring.webmvc.annotation.EnableWebMvcExtension; @@ -27,8 +30,14 @@ import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import java.util.Set; + +import static com.alibaba.csp.sentinel.context.ContextUtil.enter; +import static com.alibaba.csp.sentinel.context.ContextUtil.exit; import static io.microsphere.alibaba.sentinel.spring.web.SentinelHandlerMethodInterceptor.BEAN_NAME; import static io.microsphere.alibaba.sentinel.spring.web.SentinelHandlerMethodInterceptor.getSentinelContext; +import static io.microsphere.alibaba.sentinel.spring.web.SentinelSpringWebConstants.DEFAULT_CONTEXT_NAME; +import static io.microsphere.alibaba.sentinel.spring.web.SentinelSpringWebConstants.DEFAULT_ORIGIN; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; @@ -67,6 +76,7 @@ public void setUp() { void test() throws Exception { this.testHelloWorld(); this.testGreeting(); + postTest(); } @Test @@ -84,4 +94,12 @@ void testAfterExecuteWithoutSentinelContext() { this.interceptor.afterExecute(null, null, null, null, request); assertNull(getSentinelContext(request)); } + + void postTest() { + Context context = enter(DEFAULT_CONTEXT_NAME, DEFAULT_ORIGIN); + DefaultNode entranceNode = context.getEntranceNode(); + Set childList = entranceNode.getChildList(); + assertFalse(childList.isEmpty()); + exit(); + } } \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/test/java/io/microsphere/alibaba/sentinel/spring/web/ConstantsTest.java b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/test/java/io/microsphere/alibaba/sentinel/spring/web/SentinelSpringWebConstantsTest.java similarity index 74% rename from microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/test/java/io/microsphere/alibaba/sentinel/spring/web/ConstantsTest.java rename to microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/test/java/io/microsphere/alibaba/sentinel/spring/web/SentinelSpringWebConstantsTest.java index 5b063b5..b12a8c1 100644 --- a/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/test/java/io/microsphere/alibaba/sentinel/spring/web/ConstantsTest.java +++ b/microsphere-alibaba-sentinel-plugins/microsphere-alibaba-sentinel-spring-web/src/test/java/io/microsphere/alibaba/sentinel/spring/web/SentinelSpringWebConstantsTest.java @@ -20,20 +20,20 @@ import org.junit.jupiter.api.Test; import static io.microsphere.alibaba.sentinel.common.util.SentinelUtils.getPluginEnabledPropertyName; -import static io.microsphere.alibaba.sentinel.spring.web.Constants.DEFAULT_CONTEXT_NAME; -import static io.microsphere.alibaba.sentinel.spring.web.Constants.DEFAULT_ORIGIN; -import static io.microsphere.alibaba.sentinel.spring.web.Constants.ENABLED_PROPERTY_NAME; -import static io.microsphere.alibaba.sentinel.spring.web.Constants.PLUGIN_NAME; +import static io.microsphere.alibaba.sentinel.spring.web.SentinelSpringWebConstants.DEFAULT_CONTEXT_NAME; +import static io.microsphere.alibaba.sentinel.spring.web.SentinelSpringWebConstants.DEFAULT_ORIGIN; +import static io.microsphere.alibaba.sentinel.spring.web.SentinelSpringWebConstants.ENABLED_PROPERTY_NAME; +import static io.microsphere.alibaba.sentinel.spring.web.SentinelSpringWebConstants.PLUGIN_NAME; import static org.junit.jupiter.api.Assertions.assertEquals; /** - * {@link Constants} Test + * {@link SentinelSpringWebConstants} Test * * @author Mercy - * @see Constants + * @see SentinelSpringWebConstants * @since 1.0.0 */ -class ConstantsTest { +class SentinelSpringWebConstantsTest { @Test void testConstants() { diff --git a/microsphere-alibaba-sentinel-spring-boot/pom.xml b/microsphere-alibaba-sentinel-spring-boot/pom.xml index f90ef66..6d541cb 100644 --- a/microsphere-alibaba-sentinel-spring-boot/pom.xml +++ b/microsphere-alibaba-sentinel-spring-boot/pom.xml @@ -33,16 +33,80 @@ microsphere-spring-boot-core - + - org.springframework.boot - spring-boot-autoconfigure + io.github.microsphere-projects + microsphere-alibaba-sentinel-alibaba-druid + ${revision} + + + + + io.github.microsphere-projects + microsphere-alibaba-druid-spring-boot true + - org.springframework.boot - spring-boot-actuator + io.github.microsphere-projects + microsphere-alibaba-sentinel-hibernate-core + ${revision} + + + + + io.github.microsphere-projects + microsphere-hibernate-core + true + + + + + io.github.microsphere-projects + microsphere-alibaba-sentinel-mybatis + ${revision} + + + + + io.github.microsphere-projects + microsphere-mybatis-spring-boot + true + + + + + io.github.microsphere-projects + microsphere-alibaba-sentinel-redis + ${revision} + + + + + io.github.microsphere-projects + microsphere-redis-spring-boot + true + + + + + io.github.microsphere-projects + microsphere-alibaba-sentinel-spring-web + ${revision} + + + + + io.github.microsphere-projects + microsphere-spring-boot-webmvc + true + + + + + io.github.microsphere-projects + microsphere-spring-boot-webflux true @@ -55,10 +119,17 @@ true - + - org.springframework.data - spring-data-redis + com.alibaba + druid + true + + + + + org.hibernate + hibernate-core true @@ -81,25 +152,111 @@ true + + + org.springframework.data + spring-data-redis + true + + + + + org.springframework.boot + spring-boot-autoconfigure + true + + + + org.springframework.boot + spring-boot-actuator + true + + - junit - junit + org.junit.jupiter + junit-jupiter + true + + + + + org.assertj + assertj-core test + org.springframework spring-test test + + + org.springframework.boot + spring-boot-test + test + + + + + org.springframework.boot + spring-boot-starter-web + test + + + + + org.springframework.boot + spring-boot-starter-webflux + true + + + io.github.microsphere-projects microsphere-spring-test test + + + io.github.microsphere-projects + microsphere-alibaba-druid-spring-test + test + + + + + io.github.microsphere-projects + microsphere-hibernate-test + test + + + + + io.github.microsphere-projects + microsphere-mybatis-spring-test + test + + + + + io.github.microsphere-projects + microsphere-redis-spring-test + test + + + + + com.h2database + h2 + test + + + io.lettuce lettuce-core diff --git a/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelAlibabaDruidAutoConfiguration.java b/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelAlibabaDruidAutoConfiguration.java new file mode 100644 index 0000000..d3b429d --- /dev/null +++ b/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelAlibabaDruidAutoConfiguration.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.microsphere.alibaba.sentinel.spring.boot.autoconfigure; + +import io.microsphere.alibaba.druid.spring.boot.condition.ConditionalOnAlibabaDruidAvailable; +import io.microsphere.alibaba.sentinel.alibaba.druid.SentinelAlibabaDruidFilter; +import io.microsphere.alibaba.sentinel.spring.boot.condition.ConditionalOnSentinelAvailable; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; + +import static io.microsphere.alibaba.sentinel.alibaba.druid.SentinelAlibabaDruidConstants.ENABLED_PROPERTY_NAME; + +/** + * The Spring Boot Auto-Configuration class of Alibaba Sentinel x Alibaba Druid + * + * @author Mercy + * @see com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration + * @see io.microsphere.alibaba.druid.spring.boot.autoconfigure.AlibabaDruidAutoConfiguration + * @since 1.0.0 + */ +@ConditionalOnSentinelAvailable +@ConditionalOnProperty(name = ENABLED_PROPERTY_NAME, matchIfMissing = true) +@ConditionalOnClass(name = { + "io.microsphere.alibaba.druid.spring.boot.condition.ConditionalOnAlibabaDruidAvailable", // Microsphere Alibaba Druid Spring Boot + "io.microsphere.alibaba.sentinel.alibaba.druid.SentinelAlibabaDruidFilter" // Microsphere Alibaba Sentinel x Alibaba Druid +}) +@AutoConfigureAfter(name = { + "com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration", // Spring Cloud Alibaba Sentinel + "io.microsphere.alibaba.druid.spring.boot.autoconfigure.AlibabaDruidAutoConfiguration" // Microsphere Alibaba Sentinel Spring Boot x Alibaba Druid +}) +@Import(value = { + SentinelAlibabaDruidAutoConfiguration.Config.class +}) +public class SentinelAlibabaDruidAutoConfiguration { + + @ConditionalOnAlibabaDruidAvailable + static class Config { + + @Bean + @ConditionalOnMissingBean + public SentinelAlibabaDruidFilter sentinelAlibabaDruidFilter() { + return new SentinelAlibabaDruidFilter(); + } + } +} diff --git a/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelAutoConfiguration.java b/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelAutoConfiguration.java deleted file mode 100644 index 5538176..0000000 --- a/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelAutoConfiguration.java +++ /dev/null @@ -1,93 +0,0 @@ -package io.microsphere.alibaba.sentinel.spring.boot.autoconfigure; - -import io.microsphere.alibaba.sentinel.spring.boot.condition.ConditionalOnSentinelEnabled; -import org.springframework.boot.autoconfigure.AutoConfigureAfter; - -/** - * Microsphere Sentinel Spring Boot Auto-Configuration - * - * @author Mercy - * @see com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration - * @since 1.0.0 - */ -@ConditionalOnSentinelEnabled -@AutoConfigureAfter(name = { - "org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration", - "org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration", - "com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration", -}) -public class SentinelAutoConfiguration { - -// @ConditionalOnProperty( -// prefix = PREFIX + "redis", -// name = ENABLED_PROPERTY_NAME, -// matchIfMissing = true -// ) -// @ConditionalOnClass(name = { -// "org.springframework.data.redis.connection.RedisConnection", -// "io.microsphere.redis.spring.interceptor.RedisConnectionInterceptor" -// }) -// static class RedisConfiguration { -// -// @Bean -// @ConditionalOnMissingBean -// public SentinelRedisCommandInterceptor sentinelRedisCommandInterceptor() { -// return new SentinelRedisCommandInterceptor(); -// } -// } - -// @ConditionalOnProperty( -// prefix = PREFIX + "hibernate", -// name = ENABLED_PROPERTY_NAME, -// matchIfMissing = true -// ) -// @ConditionalOnClass(name = { -// "org.hibernate.SessionFactory", // Hibernate -// "org.springframework.orm.hibernate5.LocalSessionFactoryBean" // Spring ORM -// }) -// static class HibernateConfiguration { -// -// @Bean -// @ConditionalOnMissingBean -// public BeanPostProcessor sentinelHibernateInterceptorBeanPostProcessor() { -// return new SentinelHibernateInterceptorBeanPostProcessor(); -// } -// } -// -// @ConditionalOnProperty( -// prefix = PREFIX + "druid", -// name = ENABLED_PROPERTY_NAME, -// matchIfMissing = true -// ) -// @ConditionalOnClass(name = { -// "com.alibaba.druid.pool.DruidDataSource" -// }) -// static class DruidConfiguration { -// -// @Bean -// @ConditionalOnMissingBean -// public BeanPostProcessor sentinelDruidFilterBeanPostProcessor() { -// return new SentinelDruidFilterBeanPostProcessor(); -// } -// } -// -// @ConditionalOnProperty( -// prefix = PREFIX + "mybatis", -// name = ENABLED_PROPERTY_NAME, -// matchIfMissing = true -// ) -// @ConditionalOnClass(name = { -// "org.apache.ibatis.executor.Executor" -// }) -// static class MyBatisConfiguration { -// -// @Autowired -// public void initSentinelMyBatisInterceptor(ObjectProvider sqlSessionFactoryProvider) { -// SentinelMyBatisInterceptor interceptor = new SentinelMyBatisInterceptor(); -// SqlSessionFactory[] sqlSessionFactories = sqlSessionFactoryProvider.getIfAvailable(); -// for (SqlSessionFactory sqlSessionFactory : sqlSessionFactories) { -// sqlSessionFactory.getConfiguration().addInterceptor(interceptor); -// } -// } -// } -} \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelMyBatisAutoConfiguration.java b/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelMyBatisAutoConfiguration.java new file mode 100644 index 0000000..9de6a42 --- /dev/null +++ b/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelMyBatisAutoConfiguration.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.microsphere.alibaba.sentinel.spring.boot.autoconfigure; + +import io.microsphere.alibaba.sentinel.mybatis.executor.SentinelMyBatisExecutorFilter; +import io.microsphere.alibaba.sentinel.spring.boot.condition.ConditionalOnSentinelAvailable; +import io.microsphere.mybatis.spring.annotation.EnableMyBatisExtension; +import io.microsphere.mybatis.spring.boot.autoconfigure.condition.ConditionalOnMyBatisAvailable; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; + +import static io.microsphere.alibaba.sentinel.mybatis.SentinelMyBatisConstants.ENABLED_PROPERTY_NAME; + + +/** + * The Spring Boot Auto-Configuration class of Alibaba Sentinel x MyBatis + * + * @author Mercy + * @see com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration + * @see org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration + * @see com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration + * @see io.microsphere.mybatis.spring.boot.autoconfigure.MyBatisAutoConfiguration + * @since 1.0.0 + */ +@ConditionalOnSentinelAvailable +@ConditionalOnMyBatisAvailable +@ConditionalOnProperty(name = ENABLED_PROPERTY_NAME, matchIfMissing = true) +@ConditionalOnClass(name = { + "io.microsphere.mybatis.spring.annotation.EnableMyBatisExtension", // Microsphere MyBatis Spring + "io.microsphere.alibaba.sentinel.mybatis.executor.SentinelMyBatisExecutorFilter" // Microsphere Alibaba Sentinel x Mybatis +}) +@AutoConfigureAfter(name = { + "com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration", // Spring Cloud Alibaba Sentinel + "org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration", // MyBatis Spring Boot + "com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration", // MyBatis Plus Spring Boot + "io.microsphere.mybatis.spring.boot.autoconfigure.MyBatisAutoConfiguration" // Microsphere Alibaba Sentinel Spring Boot x Mybatis +}) +@Import(value = { + SentinelMyBatisAutoConfiguration.Config.class +}) +public class SentinelMyBatisAutoConfiguration { + + @EnableMyBatisExtension + static class Config { + + @Bean + @ConditionalOnMissingBean + public SentinelMyBatisExecutorFilter sentinelMyBatisExecutorFilter() { + return new SentinelMyBatisExecutorFilter(); + } + } +} diff --git a/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelRedisAutoConfiguration.java b/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelRedisAutoConfiguration.java new file mode 100644 index 0000000..62357b7 --- /dev/null +++ b/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelRedisAutoConfiguration.java @@ -0,0 +1,48 @@ +package io.microsphere.alibaba.sentinel.spring.boot.autoconfigure; + +import io.microsphere.alibaba.sentinel.redis.spring.SentinelRedisCommandInterceptor; +import io.microsphere.alibaba.sentinel.spring.boot.condition.ConditionalOnSentinelAvailable; +import io.microsphere.redis.spring.boot.autoconfigure.condition.ConditionalOnRedisAvailable; +import io.microsphere.redis.spring.boot.autoconfigure.condition.ConditionalOnRedisInterceptorEnabled; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; + +import static io.microsphere.alibaba.sentinel.redis.SentinelRedisConstants.ENABLED_PROPERTY_NAME; + +/** + * The Spring Boot Auto-Configuration class of Alibaba Sentinel x Redis + * + * @author Mercy + * @see com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration + * @see org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration + * @since 1.0.0 + */ +@ConditionalOnSentinelAvailable +@ConditionalOnRedisAvailable +@ConditionalOnProperty(name = ENABLED_PROPERTY_NAME, matchIfMissing = true) +@ConditionalOnClass(name = { + "io.microsphere.alibaba.sentinel.redis.spring.SentinelRedisCommandInterceptor" // Microsphere Alibaba Sentinel x Redis +}) +@AutoConfigureAfter(name = { + "com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration", // Spring Cloud Alibaba Sentinel + "org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration", // Spring Boot Redis +}) +@Import(value = { + SentinelRedisAutoConfiguration.Config.class, +}) +public class SentinelRedisAutoConfiguration { + + @ConditionalOnRedisInterceptorEnabled + static class Config { + + @Bean + @ConditionalOnMissingBean + public SentinelRedisCommandInterceptor sentinelRedisCommandInterceptor() { + return new SentinelRedisCommandInterceptor(); + } + } +} \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelSpringWebAutoConfiguration.java b/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelSpringWebAutoConfiguration.java new file mode 100644 index 0000000..8c59be5 --- /dev/null +++ b/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelSpringWebAutoConfiguration.java @@ -0,0 +1,47 @@ +package io.microsphere.alibaba.sentinel.spring.boot.autoconfigure; + +import io.microsphere.alibaba.sentinel.spring.boot.condition.ConditionalOnSentinelAvailable; +import io.microsphere.alibaba.sentinel.spring.web.SentinelHandlerMethodInterceptor; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.annotation.Bean; + + +import static io.microsphere.alibaba.sentinel.spring.web.SentinelSpringWebConstants.ENABLED_PROPERTY_NAME; +import static org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type.ANY; + +/** + * The Spring Boot Auto-Configuration class of Alibaba Sentinel x Spring Web + * + * @author Mercy + * @see com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration + * @see org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration + * @see io.microsphere.spring.boot.webmvc.autoconfigure.WebMvcAutoConfiguration + * @see org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration + * @see io.microsphere.spring.boot.webflux.autoconfigure.WebFluxAutoConfiguration + * @since 1.0.0 + */ +@ConditionalOnWebApplication(type = ANY) +@ConditionalOnSentinelAvailable +@ConditionalOnProperty(name = ENABLED_PROPERTY_NAME, matchIfMissing = true) +@ConditionalOnClass(name = { + "org.springframework.web.method.HandlerMethod", // Spring Web + "io.microsphere.spring.web.method.support.HandlerMethodInterceptor", // Microsphere Spring Web + "io.microsphere.alibaba.sentinel.spring.web.SentinelHandlerMethodInterceptor" // Microsphere Alibaba Sentinel x Spring Web +}) +@AutoConfigureAfter(name = { + "com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration", // Spring Cloud Alibaba Sentinel + "io.microsphere.spring.boot.webmvc.autoconfigure.WebMvcAutoConfiguration", // Microsphere Spring Boot WebMVC + "io.microsphere.spring.boot.webflux.autoconfigure.WebFluxAutoConfiguration" // Microsphere Spring Boot WebFlux +}) +public class SentinelSpringWebAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public SentinelHandlerMethodInterceptor sentinelHandlerMethodInterceptor() { + return new SentinelHandlerMethodInterceptor(); + } +} \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/condition/ConditionalOnSentinelAvailable.java b/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/condition/ConditionalOnSentinelAvailable.java new file mode 100644 index 0000000..07cc066 --- /dev/null +++ b/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/condition/ConditionalOnSentinelAvailable.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.microsphere.alibaba.sentinel.spring.boot.condition; + +import io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelRedisAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Indicates that a component is eligible for registration when Alibaba Sentinel is available. + *

+ * This annotation combines {@link ConditionalOnSentinelEnabled @ConditionalOnSentinelEnabled} and + * {@link ConditionalOnClass @ConditionalOnClass(name = {"com.alibaba.csp.sentinel.SphU" , + * "io.microsphere.alibaba.sentinel.common.SentinelPlugin"})}, meaning the annotated component will be registered + * only if Sentinel is explicitly enabled and the Sentinel core classes are present on the classpath. + *

+ * + *

Example Usage

+ *
{@code
+ * @Configuration
+ * @ConditionalOnSentinelAvailiable
+ * public class MySentinelConfiguration {
+ *     // This configuration will only be active if Sentinel is available
+ * }
+ * }
+ * + * @author
Mercy + * @see SentinelRedisAutoConfiguration + * @since 1.0.0 + */ +@Retention(RUNTIME) +@Target({TYPE, METHOD}) +@Documented +@ConditionalOnSentinelEnabled +@ConditionalOnClass(name = { + "com.alibaba.csp.sentinel.SphU", // Sentinel Core API + "io.microsphere.alibaba.sentinel.common.SentinelPlugin" // Microsphere Sentinel Commons API +}) +public @interface ConditionalOnSentinelAvailable { +} \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/condition/ConditionalOnSentinelEnabled.java b/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/condition/ConditionalOnSentinelEnabled.java index c1051fc..2096c4b 100644 --- a/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/condition/ConditionalOnSentinelEnabled.java +++ b/microsphere-alibaba-sentinel-spring-boot/src/main/java/io/microsphere/alibaba/sentinel/spring/boot/condition/ConditionalOnSentinelEnabled.java @@ -16,37 +16,28 @@ */ package io.microsphere.alibaba.sentinel.spring.boot.condition; -import io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelAutoConfiguration; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelRedisAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; -import static io.microsphere.constants.PropertyConstants.ENABLED_PROPERTY_NAME; +import static io.microsphere.alibaba.sentinel.common.constants.SentinelConstants.ENABLED_PROPERTY_NAME; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.RUNTIME; /** - * Alibaba Sentinel Spring Boot Condition + * {@link ConditionalOnProperty} based annotation for Alibaba Sentinel enabled property. * * @author Mercy - * @see SentinelAutoConfiguration + * @see SentinelRedisAutoConfiguration * @since 1.0.0 */ @Retention(RUNTIME) @Target({TYPE, METHOD}) @Documented -@ConditionalOnProperty(prefix = ConditionalOnSentinelEnabled.PREFIX, name = ENABLED_PROPERTY_NAME, matchIfMissing = true) -@ConditionalOnClass(name = { - "com.alibaba.csp.sentinel.SphU" -}) +@ConditionalOnProperty(name = ENABLED_PROPERTY_NAME, matchIfMissing = true) public @interface ConditionalOnSentinelEnabled { - - /** - * The property name prefix for Alibaba Sentinel : "microsphere.sentinel." - */ - String PREFIX = "microsphere.sentinel."; } \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-spring-boot/src/main/resources/META-INF/spring.factories b/microsphere-alibaba-sentinel-spring-boot/src/main/resources/META-INF/spring.factories index 0ede9fd..cbc2b17 100644 --- a/microsphere-alibaba-sentinel-spring-boot/src/main/resources/META-INF/spring.factories +++ b/microsphere-alibaba-sentinel-spring-boot/src/main/resources/META-INF/spring.factories @@ -1,2 +1,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelAutoConfiguration \ No newline at end of file +io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelAlibabaDruidAutoConfiguration,\ +io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelMyBatisAutoConfiguration,\ +io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelRedisAutoConfiguration,\ +io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelSpringWebAutoConfiguration \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-spring-boot/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/microsphere-alibaba-sentinel-spring-boot/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 8344d3f..36775aa 100644 --- a/microsphere-alibaba-sentinel-spring-boot/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/microsphere-alibaba-sentinel-spring-boot/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1 +1,4 @@ -io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelAutoConfiguration \ No newline at end of file +io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelAlibabaDruidAutoConfiguration +io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelMyBatisAutoConfiguration +io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelRedisAutoConfiguration +io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelSpringWebAutoConfiguration \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/AutoConfigurationTest.java b/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/AutoConfigurationTest.java new file mode 100644 index 0000000..8ce5a32 --- /dev/null +++ b/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/AutoConfigurationTest.java @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.microsphere.alibaba.sentinel.spring.boot.autoconfigure; + +import com.alibaba.csp.sentinel.SphU; +import io.microsphere.alibaba.sentinel.common.SentinelPlugin; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.FilteredClassLoader; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; + +import java.util.Set; + +import static io.microsphere.collection.SetUtils.newLinkedHashSet; +import static io.microsphere.util.ArrayUtils.EMPTY_CLASS_ARRAY; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.boot.autoconfigure.AutoConfigurations.of; +import static org.springframework.core.ResolvableType.forClass; + +/** + * Abstract class for auto-configuration tests + * + * @param the type of auto-configuration class + * @author Mercy + * @see ApplicationContextRunner + * @since 1.0.0 + */ +abstract class AutoConfigurationTest { + + ApplicationContextRunner applicationContextRunner; + + WebApplicationContextRunner webApplicationContextRunner; + + final Class autoConfigurationClass; + + AutoConfigurationTest() { + this.autoConfigurationClass = (Class) forClass(getClass()) + .as(AutoConfigurationTest.class).resolveGeneric(0); + } + + @BeforeEach + void setUp() { + this.applicationContextRunner = new ApplicationContextRunner() + .withConfiguration(of(autoConfigurationClass)); + this.webApplicationContextRunner = new WebApplicationContextRunner() + .withConfiguration(of(this.autoConfigurationClass)); + } + + @Test + void testAutoConfiguredClasses() { + this.applicationContextRunner.run(context -> { + for (Class beanClass : getAutoConfiguredClasses()) { + assertThat(context).hasSingleBean(beanClass); + } + }); + + this.webApplicationContextRunner.run(context -> { + for (Class beanClass : getAutoConfiguredClasses()) { + assertThat(context).hasSingleBean(beanClass); + } + }); + } + + @Test + void testOnGlobalDisabledProperty() { + for (String propertyValue : getGlobalDisabledPropertyValues()) { + assertDisabledProperty(propertyValue, getAutoConfiguredClasses()); + } + } + + @Test + void testOnGlobalMissingClass() { + for (Class missingClass : getGlobalMissingClasses()) { + assertFilteredClass(missingClass.getName(), getAutoConfiguredClasses()); + } + } + + protected final Class[] getAutoConfiguredClasses() { + Set> autoConfiguredClasses = newLinkedHashSet(); + autoConfiguredClasses.add(this.autoConfigurationClass); + configureAutoConfiguredClasses(autoConfiguredClasses); + return autoConfiguredClasses.toArray(EMPTY_CLASS_ARRAY); + } + + protected final Set getGlobalDisabledPropertyValues() { + Set globalDisabledPropertyValues = newLinkedHashSet(); + globalDisabledPropertyValues.add("microsphere.sentinel.enabled=false"); + configureGlobalDisabledPropertyValues(globalDisabledPropertyValues); + return globalDisabledPropertyValues; + } + + protected final Set> getGlobalMissingClasses() { + Set> globalMissingClasses = newLinkedHashSet(); + globalMissingClasses.add(SphU.class); + globalMissingClasses.add(SentinelPlugin.class); + configureGlobalMissingClasses(globalMissingClasses); + return globalMissingClasses; + } + + protected abstract void configureAutoConfiguredClasses(Set> autoConfiguredClasses); + + protected abstract void configureGlobalDisabledPropertyValues(Set globalDisabledPropertyValues); + + protected abstract void configureGlobalMissingClasses(Set> globalMissingClasses); + + void assertDisabledProperty(String propertyValue, Class... beanClasses) { + this.applicationContextRunner.withPropertyValues(propertyValue) + .run(context -> { + for (Class beanClass : beanClasses) { + assertThat(context).doesNotHaveBean(beanClass); + } + }); + + this.webApplicationContextRunner.withPropertyValues(propertyValue) + .run(context -> { + for (Class beanClass : beanClasses) { + assertThat(context).doesNotHaveBean(beanClass); + } + }); + } + + void assertFilteredClass(String filteredClass, Class... beanClasses) { + this.applicationContextRunner.withClassLoader(new FilteredClassLoader(filteredClass)) + .run(context -> { + for (Class beanClass : beanClasses) { + assertThat(context).doesNotHaveBean(beanClass); + } + }); + + this.webApplicationContextRunner.withClassLoader(new FilteredClassLoader(filteredClass)) + .run(context -> { + for (Class beanClass : beanClasses) { + assertThat(context).doesNotHaveBean(beanClass); + } + }); + } +} diff --git a/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelAlibabaDruidAutoConfigurationTest.java b/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelAlibabaDruidAutoConfigurationTest.java new file mode 100644 index 0000000..61f28b1 --- /dev/null +++ b/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelAlibabaDruidAutoConfigurationTest.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.microsphere.alibaba.sentinel.spring.boot.autoconfigure; + + +import io.microsphere.alibaba.druid.spring.boot.condition.ConditionalOnAlibabaDruidAvailable; +import io.microsphere.alibaba.sentinel.alibaba.druid.SentinelAlibabaDruidFilter; +import io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelAlibabaDruidAutoConfiguration.Config; +import org.junit.jupiter.api.Test; + +import java.util.Set; + +/** + * {@link SentinelAlibabaDruidAutoConfiguration} Test + * + * @author Mercy + * @see SentinelAlibabaDruidAutoConfiguration + * @since 1.0.0 + */ +class SentinelAlibabaDruidAutoConfigurationTest extends AutoConfigurationTest { + + @Test + void tesOnDisabledProperty() { + assertDisabledProperty("microsphere.alibaba.druid.enabled=false", + Config.class, SentinelAlibabaDruidFilter.class); + } + + @Test + void testOnMissingClass() { + assertFilteredClass("com.alibaba.druid.pool.DruidDataSource", + Config.class, SentinelAlibabaDruidFilter.class); + } + + @Override + protected void configureAutoConfiguredClasses(Set> autoConfiguredClasses) { + autoConfiguredClasses.add(Config.class); + autoConfiguredClasses.add(SentinelAlibabaDruidFilter.class); + } + + @Override + protected void configureGlobalDisabledPropertyValues(Set globalDisabledPropertyValues) { + globalDisabledPropertyValues.add("microsphere.sentinel.alibaba-druid.enabled=false"); + } + + @Override + protected void configureGlobalMissingClasses(Set> globalMissingClasses) { + globalMissingClasses.add(ConditionalOnAlibabaDruidAvailable.class); + globalMissingClasses.add(SentinelAlibabaDruidFilter.class); + } +} \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelAutoConfigurationTest.java b/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelAutoConfigurationTest.java new file mode 100644 index 0000000..f09de87 --- /dev/null +++ b/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelAutoConfigurationTest.java @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.microsphere.alibaba.sentinel.spring.boot.autoconfigure; + + +import com.alibaba.csp.sentinel.context.Context; +import com.alibaba.druid.pool.DruidDataSource; +import io.microsphere.alibaba.sentinel.redis.spring.SentinelRedisCommandInterceptor; +import io.microsphere.mybatis.spring.test.config.MyBatisDataBaseTestConfiguration; +import io.microsphere.mybatis.test.mapper.UserMapper; +import io.microsphere.redis.spring.context.RedisContext; +import io.microsphere.redis.spring.test.config.RedisConfig; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.core.ValueOperations; + +import java.io.IOException; + +import static com.alibaba.csp.sentinel.context.ContextUtil.enter; +import static io.microsphere.alibaba.druid.test.AlibabaDruidTestUtils.buildDefaultDruidDataSource; +import static io.microsphere.mybatis.test.AbstractMapperTest.assertUserMapper; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +/** + * SentinelAutoConfiguration Test + * + * @author Mercy + * @see SentinelAlibabaDruidAutoConfiguration + * @see SentinelMyBatisAutoConfiguration + * @see SentinelRedisAutoConfiguration + * @since 1.0.0 + */ +@SpringBootTest(classes = { + MyBatisDataBaseTestConfiguration.class, + RedisConfig.class, + SentinelAutoConfigurationTest.TestConfig.class +}, + webEnvironment = RANDOM_PORT, + properties = { + "microsphere.redis.enabled=true", + "spring.application.name=test-app", + "mybatis.configLocation=classpath:/META-INF/mybatis/config.xml" + }) +@EnableAutoConfiguration +class SentinelAutoConfigurationTest { + + @Autowired + private RedisContext redisContext; + + @Autowired + private StringRedisTemplate stringRedisTemplate; + + @Autowired + private SentinelRedisCommandInterceptor interceptor; + + @Autowired + private SqlSessionFactory sessionFactory; + + @Test + void test() { + testAlibabaDruidAndMyBatisPlugin(); + testRedisPlugin(); + } + + private void testAlibabaDruidAndMyBatisPlugin() { + Configuration configuration = this.sessionFactory.getConfiguration(); + SqlSession sqlSession = this.sessionFactory.openSession(); + UserMapper userMapper = configuration.getMapper(UserMapper.class, sqlSession); + assertUserMapper(userMapper); + + Context context = enter("microsphere_sentinel_alibaba_druid_context", "Filter"); + assertNotNull(context); + + context = enter("microsphere_sentinel_mybatis_context", "Executor"); + assertNotNull(context); + } + + void testRedisPlugin() { + String key = "key"; + String value = "value"; + ValueOperations valueOperations = this.stringRedisTemplate.opsForValue(); + valueOperations.set(key, value); + assertEquals(value, valueOperations.get(key)); + + Context context = enter("microsphere_sentinel_redis_context", "RedisConnection"); + assertNotNull(context); + } + + static class TestConfig { + + @Bean(initMethod = "init", destroyMethod = "close") + public DruidDataSource dataSource() throws IOException { + return buildDefaultDruidDataSource(); + } + } +} \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelMyBatisAutoConfigurationTest.java b/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelMyBatisAutoConfigurationTest.java new file mode 100644 index 0000000..d8f1348 --- /dev/null +++ b/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelMyBatisAutoConfigurationTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.microsphere.alibaba.sentinel.spring.boot.autoconfigure; + + +import io.microsphere.alibaba.sentinel.mybatis.executor.SentinelMyBatisExecutorFilter; +import io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelMyBatisAutoConfiguration.Config; +import io.microsphere.mybatis.spring.annotation.EnableMyBatisExtension; +import org.apache.ibatis.session.SqlSessionFactory; + +import java.util.Set; + +/** + * {@link SentinelMyBatisAutoConfiguration} Test + * + * @author Mercy + * @see SentinelMyBatisAutoConfiguration + * @since 1.0.0 + */ +class SentinelMyBatisAutoConfigurationTest extends AutoConfigurationTest { + + @Override + protected void configureAutoConfiguredClasses(Set> autoConfiguredClasses) { + autoConfiguredClasses.add(Config.class); + autoConfiguredClasses.add(SentinelMyBatisExecutorFilter.class); + } + + @Override + protected void configureGlobalDisabledPropertyValues(Set globalDisabledPropertyValues) { + globalDisabledPropertyValues.add("microsphere.mybatis.enabled=false"); + globalDisabledPropertyValues.add("microsphere.sentinel.mybatis.enabled=false"); + } + + @Override + protected void configureGlobalMissingClasses(Set> globalMissingClasses) { + globalMissingClasses.add(SqlSessionFactory.class); + globalMissingClasses.add(EnableMyBatisExtension.class); + globalMissingClasses.add(SentinelMyBatisExecutorFilter.class); + } +} \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelRedisAutoConfigurationTest.java b/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelRedisAutoConfigurationTest.java new file mode 100644 index 0000000..52a2d75 --- /dev/null +++ b/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelRedisAutoConfigurationTest.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.microsphere.alibaba.sentinel.spring.boot.autoconfigure; + + +import io.microsphere.alibaba.sentinel.redis.spring.SentinelRedisCommandInterceptor; +import io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelRedisAutoConfiguration.Config; +import io.microsphere.redis.metadata.RedisMetadataLoader; +import io.microsphere.redis.spring.interceptor.RedisMethodInterceptor; +import org.junit.jupiter.api.Test; +import org.springframework.data.redis.connection.RedisConnection; + +import java.util.Set; + +/** + * {@link SentinelRedisAutoConfiguration} Test + * + * @author Mercy + * @see SentinelRedisAutoConfiguration + * @since 1.0.0 + */ +class SentinelRedisAutoConfigurationTest extends AutoConfigurationTest { + + @Test + void tesOntDisabledProperty() { + assertDisabledProperty("microsphere.redis.interceptor.enabled=false", + Config.class, SentinelRedisCommandInterceptor.class); + } + + @Override + protected void configureAutoConfiguredClasses(Set> autoConfiguredClasses) { + autoConfiguredClasses.add(Config.class); + autoConfiguredClasses.add(SentinelRedisCommandInterceptor.class); + } + + @Override + protected void configureGlobalDisabledPropertyValues(Set globalDisabledPropertyValues) { + globalDisabledPropertyValues.add("microsphere.redis.enabled=false"); + globalDisabledPropertyValues.add("microsphere.sentinel.redis.enabled=false"); + } + + @Override + protected void configureGlobalMissingClasses(Set> globalMissingClasses) { + globalMissingClasses.add(RedisConnection.class); + globalMissingClasses.add(RedisMetadataLoader.class); + globalMissingClasses.add(RedisMethodInterceptor.class); + globalMissingClasses.add(SentinelRedisCommandInterceptor.class); + } +} \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelSpringWebAutoConfigurationTest.java b/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelSpringWebAutoConfigurationTest.java new file mode 100644 index 0000000..b46290e --- /dev/null +++ b/microsphere-alibaba-sentinel-spring-boot/src/test/java/io/microsphere/alibaba/sentinel/spring/boot/autoconfigure/SentinelSpringWebAutoConfigurationTest.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.microsphere.alibaba.sentinel.spring.boot.autoconfigure; + + +import io.microsphere.alibaba.sentinel.spring.web.SentinelHandlerMethodInterceptor; +import io.microsphere.spring.web.method.support.HandlerMethodInterceptor; +import org.junit.jupiter.api.Test; +import org.springframework.web.method.HandlerMethod; + +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * {@link SentinelSpringWebAutoConfiguration} Test + * + * @author Mercy + * @see SentinelSpringWebAutoConfiguration + * @since 1.0.0 + */ +class SentinelSpringWebAutoConfigurationTest extends AutoConfigurationTest { + + @Test + void testAutoConfiguredClasses() { + this.applicationContextRunner.run(context -> { + assertThat(context).doesNotHaveBean(this.autoConfigurationClass) + .doesNotHaveBean(SentinelHandlerMethodInterceptor.class); + }); + + this.webApplicationContextRunner.run(context -> { + assertThat(context).hasSingleBean(this.autoConfigurationClass) + .hasSingleBean(SentinelHandlerMethodInterceptor.class); + }); + } + + @Override + protected void configureAutoConfiguredClasses(Set> autoConfiguredClasses) { + autoConfiguredClasses.add(SentinelHandlerMethodInterceptor.class); + } + + @Override + protected void configureGlobalDisabledPropertyValues(Set globalDisabledPropertyValues) { + globalDisabledPropertyValues.add("microsphere.sentinel.spring-web.enabled=false"); + } + + @Override + protected void configureGlobalMissingClasses(Set> globalMissingClasses) { + globalMissingClasses.add(HandlerMethod.class); + globalMissingClasses.add(HandlerMethodInterceptor.class); + globalMissingClasses.add(SentinelHandlerMethodInterceptor.class); + } +} \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-spring-cloud/pom.xml b/microsphere-alibaba-sentinel-spring-cloud/pom.xml new file mode 100644 index 0000000..281d9ca --- /dev/null +++ b/microsphere-alibaba-sentinel-spring-cloud/pom.xml @@ -0,0 +1,250 @@ + + + + io.github.microsphere-projects + microsphere-alibaba-sentinel-parent + ${revision} + ../microsphere-alibaba-sentinel-parent/pom.xml + + 4.0.0 + + io.github.microsphere-projects + microsphere-alibaba-sentinel-spring-cloud + ${revision} + jar + + Microsphere :: Alibaba Sentinel :: Spring Cloud + Microsphere Alibaba Sentinel Spring Cloud + + + + + + io.github.microsphere-projects + microsphere-alibaba-sentinel-spring-boot + ${revision} + + + + + io.github.microsphere-projects + microsphere-spring-cloud-commons + + + + + io.github.microsphere-projects + microsphere-alibaba-druid-spring-cloud + true + + + + + io.github.microsphere-projects + microsphere-hibernate-core + true + + + + + io.github.microsphere-projects + microsphere-mybatis-spring-cloud + true + + + + + io.github.microsphere-projects + microsphere-redis-spring-cloud + true + + + + + io.github.microsphere-projects + microsphere-spring-boot-webmvc + true + + + + + io.github.microsphere-projects + microsphere-spring-boot-webflux + true + + + + + + + com.alibaba.csp + sentinel-core + true + + + + + com.alibaba + druid + true + + + + + org.hibernate + hibernate-core + true + + + + + org.mybatis + mybatis + true + + + + org.mybatis + mybatis-spring + true + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + true + + + + + org.springframework.data + spring-data-redis + true + + + + + org.springframework.cloud + spring-cloud-commons + true + + + + org.springframework.cloud + spring-cloud-context + true + + + + + org.springframework.boot + spring-boot-autoconfigure + true + + + + org.springframework.boot + spring-boot-actuator + true + + + + + org.junit.jupiter + junit-jupiter + true + + + + + org.assertj + assertj-core + test + + + + + org.springframework + spring-test + test + + + + + org.springframework.boot + spring-boot-test + test + + + + + org.springframework.boot + spring-boot-starter-web + test + + + + + org.springframework.boot + spring-boot-starter-webflux + true + + + + + io.github.microsphere-projects + microsphere-spring-test + test + + + + + io.github.microsphere-projects + microsphere-alibaba-druid-spring-test + test + + + + + io.github.microsphere-projects + microsphere-hibernate-test + test + + + + + io.github.microsphere-projects + microsphere-mybatis-spring-test + test + + + + + io.github.microsphere-projects + microsphere-redis-spring-test + test + + + + + com.h2database + h2 + test + + + + + io.lettuce + lettuce-core + test + + + + org.apache.commons + commons-pool2 + test + + + \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-spring-cloud/src/main/java/io/microsphere/alibaba/sentinel/spring/cloud/SentinelCloudAutoConfiguration.java b/microsphere-alibaba-sentinel-spring-cloud/src/main/java/io/microsphere/alibaba/sentinel/spring/cloud/SentinelCloudAutoConfiguration.java new file mode 100644 index 0000000..007dccc --- /dev/null +++ b/microsphere-alibaba-sentinel-spring-cloud/src/main/java/io/microsphere/alibaba/sentinel/spring/cloud/SentinelCloudAutoConfiguration.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.microsphere.alibaba.sentinel.spring.cloud; + +import io.microsphere.alibaba.sentinel.common.SentinelPlugin; +import io.microsphere.alibaba.sentinel.common.SentinelPluginRepository; +import io.microsphere.alibaba.sentinel.spring.boot.condition.ConditionalOnSentinelAvailable; +import io.microsphere.spring.cloud.client.condition.ConditionalOnFeaturesAvailable; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.cloud.client.actuator.HasFeatures; +import org.springframework.cloud.client.actuator.NamedFeature; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; + +import java.util.Collection; +import java.util.List; + +import static io.microsphere.alibaba.sentinel.common.SentinelPluginRepository.INSTANCE; +import static io.microsphere.collection.ListUtils.newLinkedList; + +/** + * The Spring Cloud Auto-Configuration Class of Alibaba Sentinel + * + * @author Mercy + * @see com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration + * @see io.microsphere.alibaba.druid.spring.cloud.autoconfigure.AlibabaDruidCloudAutoConfiguration + * @see io.microsphere.mybatis.spring.cloud.autoconfigure.MyBatisCloudAutoConfiguration + * @see io.microsphere.redis.spring.cloud.autoconfigure.RedisCloudAutoConfiguration + * @see io.microsphere.spring.cloud.client.actuator.ConfigurationPropertyHasFeaturesAutoConfiguration + * @see io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelAlibabaDruidAutoConfiguration + * @see io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelMyBatisAutoConfiguration + * @see io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelRedisAutoConfiguration + * @see io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelSpringWebAutoConfiguration + * @since 1.0.0 + */ +@ConditionalOnSentinelAvailable +@AutoConfigureAfter(name = { + "com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration", + "io.microsphere.alibaba.druid.spring.cloud.autoconfigure.AlibabaDruidCloudAutoConfiguration", // Microsphere Alibaba Druid Spring Cloud + "io.microsphere.mybatis.spring.cloud.autoconfigure.MyBatisCloudAutoConfiguration", // Microsphere MyBatis Spring Cloud + "io.microsphere.redis.spring.cloud.autoconfigure.RedisCloudAutoConfiguration", // Microsphere Redis Spring Cloud + "io.microsphere.spring.cloud.client.actuator.ConfigurationPropertyHasFeaturesAutoConfiguration", // Microsphere Spring Cloud + "io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelAlibabaDruidAutoConfiguration", // Microsphere Sentinel Spring Boot x Alibaba Druid + "io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelMyBatisAutoConfiguration", // Microsphere Sentinel Spring Boot x MyBatis + "io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelRedisAutoConfiguration", // Microsphere Sentinel Spring Boot x Redis + "io.microsphere.alibaba.sentinel.spring.boot.autoconfigure.SentinelSpringWebAutoConfiguration" // Microsphere Sentinel Spring Boot x Spring Web +}) +@Import(value = { + SentinelCloudAutoConfiguration.FeaturesConfig.class +}) +public class SentinelCloudAutoConfiguration { + + @ConditionalOnFeaturesAvailable + public static class FeaturesConfig { + + public static final String BEAN_NAME = "alibaba-sentinel.features"; + + @Bean(name = BEAN_NAME) + public HasFeatures features() { + SentinelPluginRepository sentinelPluginRepository = INSTANCE; + Collection sentinelPlugins = sentinelPluginRepository.getAll(); + List> abstractFeatures = newLinkedList(); + List namedFeatures = newLinkedList(); + for (SentinelPlugin sentinelPlugin : sentinelPlugins) { + Class type = sentinelPlugin.getClass(); + if (sentinelPlugin.isEnabled()) { + String name = sentinelPlugin.getName(); + NamedFeature namedFeature = new NamedFeature(name, type); + namedFeatures.add(namedFeature); + } else { + abstractFeatures.add(type); + } + } + HasFeatures hasFeatures = new HasFeatures(abstractFeatures, namedFeatures); + return hasFeatures; + } + } +} diff --git a/microsphere-alibaba-sentinel-spring-cloud/src/main/resources/META-INF/spring.factories b/microsphere-alibaba-sentinel-spring-cloud/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..28e933f --- /dev/null +++ b/microsphere-alibaba-sentinel-spring-cloud/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +io.microsphere.alibaba.sentinel.spring.cloud.SentinelCloudAutoConfiguration \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-spring-cloud/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/microsphere-alibaba-sentinel-spring-cloud/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..dfd85db --- /dev/null +++ b/microsphere-alibaba-sentinel-spring-cloud/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +io.microsphere.alibaba.sentinel.spring.cloud.SentinelCloudAutoConfiguration \ No newline at end of file diff --git a/microsphere-alibaba-sentinel-spring-cloud/src/test/java/io/microsphere/alibaba/sentinel/spring/cloud/SentinelCloudAutoConfigurationTest.java b/microsphere-alibaba-sentinel-spring-cloud/src/test/java/io/microsphere/alibaba/sentinel/spring/cloud/SentinelCloudAutoConfigurationTest.java new file mode 100644 index 0000000..00cf988 --- /dev/null +++ b/microsphere-alibaba-sentinel-spring-cloud/src/test/java/io/microsphere/alibaba/sentinel/spring/cloud/SentinelCloudAutoConfigurationTest.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.microsphere.alibaba.sentinel.spring.cloud; + + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.client.actuator.FeaturesEndpoint; +import org.springframework.cloud.client.actuator.HasFeatures; + +import java.util.Map; + +import static io.microsphere.alibaba.sentinel.spring.cloud.SentinelCloudAutoConfiguration.FeaturesConfig.BEAN_NAME; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE; + +/** + * {@link SentinelCloudAutoConfiguration} Test + * + * @author Mercy + * @see SentinelCloudAutoConfiguration + * @since 1.0.0 + */ +@SpringBootTest( + classes = { + SentinelCloudAutoConfigurationTest.class + }, + webEnvironment = NONE, + properties = { + "spring.application.name=test-app", + "management.endpoints.web.exposure.include=features", + } +) +@EnableAutoConfiguration +class SentinelCloudAutoConfigurationTest { + + @Autowired + private Map hasFeaturesMap; + + @Autowired + private FeaturesEndpoint featuresEndpoint; + + @Test + void test() { + assertEquals("alibaba-sentinel.features", BEAN_NAME); + HasFeatures hasFeatures = this.hasFeaturesMap.get(BEAN_NAME); + assertNotNull(hasFeatures); + assertNotNull(this.featuresEndpoint.features()); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 35ae772..b25449a 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.github.microsphere-projects microsphere-spring-cloud-parent - 0.1.16 + 0.1.23 io.github.microsphere-projects @@ -55,6 +55,7 @@ microsphere-alibaba-sentinel-plugins microsphere-alibaba-sentinel-spring microsphere-alibaba-sentinel-spring-boot + microsphere-alibaba-sentinel-spring-cloud microsphere-alibaba-sentinel-dependencies