diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 758c0758..12644859 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -16,7 +16,7 @@ Spring Cloud Alibaba Dependencies - 1.6.1 + 1.6.2 3.1.0 0.5.1 1.0.1 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/TestController.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/TestController.java index d96ed802..3db79b08 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/TestController.java +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/TestController.java @@ -23,6 +23,12 @@ public class TestController { return "Hello"; } + @RequestMapping(value = "/aa", method = RequestMethod.GET) + @SentinelResource("aa") + public String aa(int b, int a) { + return "Hello test"; + } + @RequestMapping(value = "/test", method = RequestMethod.GET) public String test1() { return "Hello test"; diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/flowrule.json b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/flowrule.json index 8aacfc33..3637fb4f 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/flowrule.json +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/flowrule.json @@ -1,6 +1,6 @@ [ { - "resource": "resource", + "resource": "/hello", "controlBehavior": 0, "count": 1, "grade": 1, @@ -8,9 +8,9 @@ "strategy": 0 }, { - "resource": "p", + "resource": "/test", "controlBehavior": 0, - "count": 1, + "count": 0, "grade": 1, "limitApp": "default", "strategy": 0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/param-flow.json b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/param-flow.json index 72e1c2dc..b21fea86 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/param-flow.json +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/param-flow.json @@ -1,6 +1,6 @@ [ { - "resource": "hotResource", + "resource": "aa", "count": 0, "grade": 1, "limitApp": "default", diff --git a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/RuleType.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/RuleType.java index a5004d83..6447adf4 100644 --- a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/RuleType.java +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/RuleType.java @@ -19,8 +19,6 @@ package org.springframework.cloud.alibaba.sentinel.datasource; import org.springframework.cloud.alibaba.sentinel.datasource.config.AbstractDataSourceProperties; import org.springframework.util.StringUtils; -import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition; -import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule; import com.alibaba.csp.sentinel.slots.block.AbstractRule; import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; @@ -59,11 +57,13 @@ public enum RuleType { /** * gateway flow */ - GW_FLOW("gw-flow", GatewayFlowRule.class), + GW_FLOW("gw-flow", + "com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule"), /** * api */ - GW_API_GROUP("gw-api-group", ApiDefinition.class); + GW_API_GROUP("gw-api-group", + "com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition"); /** * alias for {@link AbstractRule} @@ -73,19 +73,39 @@ public enum RuleType { /** * concrete {@link AbstractRule} class */ - private final Class clazz; + private Class clazz; + + /** + * concrete {@link AbstractRule} class name + */ + private String clazzName; RuleType(String name, Class clazz) { this.name = name; this.clazz = clazz; } + RuleType(String name, String clazzName) { + this.name = name; + this.clazzName = clazzName; + } + public String getName() { return name; } public Class getClazz() { - return clazz; + if (clazz != null) { + return clazz; + } + else { + try { + return Class.forName(clazzName); + } + catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } } public static RuleType getByName(String name) { diff --git a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/converter/SentinelConverter.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/converter/SentinelConverter.java index 2f7ad975..586cd1ad 100644 --- a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/converter/SentinelConverter.java +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/converter/SentinelConverter.java @@ -27,8 +27,6 @@ import org.slf4j.LoggerFactory; import org.springframework.cloud.alibaba.sentinel.datasource.RuleType; import org.springframework.util.StringUtils; -import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition; -import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule; import com.alibaba.csp.sentinel.datasource.Converter; import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; @@ -72,11 +70,13 @@ public abstract class SentinelConverter Collection ruleCollection; // hard code - if (ruleClass == GatewayFlowRule.class || ruleClass == ApiDefinition.class) { - ruleCollection = new HashSet<>(); + if (ruleClass == FlowRule.class || ruleClass == DegradeRule.class + || ruleClass == SystemRule.class || ruleClass == AuthorityRule.class + || ruleClass == ParamFlowRule.class) { + ruleCollection = new ArrayList<>(); } else { - ruleCollection = new ArrayList<>(); + ruleCollection = new HashSet<>(); } if (StringUtils.isEmpty(source)) { diff --git a/spring-cloud-alibaba-sentinel-gateway/pom.xml b/spring-cloud-alibaba-sentinel-gateway/pom.xml index d943b976..21461c37 100644 --- a/spring-cloud-alibaba-sentinel-gateway/pom.xml +++ b/spring-cloud-alibaba-sentinel-gateway/pom.xml @@ -19,10 +19,27 @@ spring-cloud-starter-netflix-zuul provided + + com.alibaba.csp + sentinel-api-gateway-adapter-common + + + com.alibaba.csp + sentinel-parameter-flow-control + com.alibaba.csp sentinel-zuul-adapter + + org.springframework.cloud + spring-cloud-alibaba-sentinel-datasource + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + provided + org.springframework.boot spring-boot-configuration-processor diff --git a/spring-cloud-alibaba-sentinel-gateway/src/main/java/org/springframework/cloud/alibaba/sentinel/zuul/SentinelGatewayAutoConfiguration.java b/spring-cloud-alibaba-sentinel-gateway/src/main/java/org/springframework/cloud/alibaba/sentinel/zuul/SentinelGatewayAutoConfiguration.java new file mode 100644 index 00000000..462b0dc2 --- /dev/null +++ b/spring-cloud-alibaba-sentinel-gateway/src/main/java/org/springframework/cloud/alibaba/sentinel/zuul/SentinelGatewayAutoConfiguration.java @@ -0,0 +1,161 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed 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 + * + * https://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 org.springframework.cloud.alibaba.sentinel.zuul; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.cloud.alibaba.sentinel.datasource.converter.JsonConverter; +import org.springframework.cloud.alibaba.sentinel.datasource.converter.XmlConverter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition; +import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem; +import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateGroupItem; +import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem; +import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.Version; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; + +/** + * @author Jim + */ +@Configuration +@ConditionalOnProperty(name = "spring.cloud.sentinel.enabled", matchIfMissing = true) +public class SentinelGatewayAutoConfiguration { + + @ConditionalOnClass(ObjectMapper.class) + @Configuration + protected static class SentinelConverterConfiguration { + + static class ApiPredicateItemDeserializer + extends StdDeserializer { + private Map> registry = new HashMap>(); + + ApiPredicateItemDeserializer() { + super(ApiPredicateItem.class); + } + + void registerApiPredicateItem(String uniqueAttribute, + Class apiPredicateItemClass) { + registry.put(uniqueAttribute, apiPredicateItemClass); + } + + @Override + public ApiPredicateItem deserialize(JsonParser jp, + DeserializationContext ctxt) throws IOException { + ObjectMapper mapper = (ObjectMapper) jp.getCodec(); + ObjectNode root = mapper.readTree(jp); + Class apiPredicateItemClass = null; + Iterator> elementsIterator = root.fields(); + while (elementsIterator.hasNext()) { + Entry element = elementsIterator.next(); + String name = element.getKey(); + if (registry.containsKey(name)) { + apiPredicateItemClass = registry.get(name); + break; + } + } + if (apiPredicateItemClass == null) { + return null; + } + return mapper.readValue(root.toString(), apiPredicateItemClass); + } + } + + @Configuration + protected static class SentinelJsonConfiguration { + + private ObjectMapper objectMapper = new ObjectMapper(); + + public SentinelJsonConfiguration() { + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, + false); + + ApiPredicateItemDeserializer deserializer = new ApiPredicateItemDeserializer(); + deserializer.registerApiPredicateItem("pattern", + ApiPathPredicateItem.class); + deserializer.registerApiPredicateItem("items", + ApiPredicateGroupItem.class); + SimpleModule module = new SimpleModule( + "PolymorphicApiPredicateItemDeserializerModule", + new Version(1, 0, 0, null)); + module.addDeserializer(ApiPredicateItem.class, deserializer); + objectMapper.registerModule(module); + } + + @Bean("sentinel-json-gw-flow-converter") + public JsonConverter jsonGatewayFlowConverter() { + return new JsonConverter(objectMapper, GatewayFlowRule.class); + } + + @Bean("sentinel-json-gw-api-group-converter") + public JsonConverter jsonApiConverter() { + return new JsonConverter(objectMapper, ApiDefinition.class); + } + } + + @ConditionalOnClass(XmlMapper.class) + @Configuration + protected static class SentinelXmlConfiguration { + + private XmlMapper xmlMapper = new XmlMapper(); + + public SentinelXmlConfiguration() { + xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, + false); + ApiPredicateItemDeserializer deserializer = new ApiPredicateItemDeserializer(); + deserializer.registerApiPredicateItem("pattern", + ApiPathPredicateItem.class); + deserializer.registerApiPredicateItem("items", + ApiPredicateGroupItem.class); + SimpleModule module = new SimpleModule( + "PolymorphicGatewayDeserializerModule", + new Version(1, 0, 0, null)); + module.addDeserializer(ApiPredicateItem.class, deserializer); + xmlMapper.registerModule(module); + } + + @Bean("sentinel-xml-gw-flow-converter") + public XmlConverter xmlGatewayFlowConverter() { + return new XmlConverter(xmlMapper, GatewayFlowRule.class); + } + + @Bean("sentinel-xml-gw-api-group-converter") + public XmlConverter xmlApiConverter() { + return new XmlConverter(xmlMapper, ApiDefinition.class); + } + + } + } + +} diff --git a/spring-cloud-alibaba-sentinel-gateway/src/main/resources/META-INF/spring.factories b/spring-cloud-alibaba-sentinel-gateway/src/main/resources/META-INF/spring.factories index 2304b8a3..e29b6862 100644 --- a/spring-cloud-alibaba-sentinel-gateway/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-alibaba-sentinel-gateway/src/main/resources/META-INF/spring.factories @@ -1,2 +1,3 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -org.springframework.cloud.alibaba.sentinel.zuul.SentinelZuulAutoConfiguration +org.springframework.cloud.alibaba.sentinel.zuul.SentinelZuulAutoConfiguration,\ +org.springframework.cloud.alibaba.sentinel.zuul.SentinelGatewayAutoConfiguration diff --git a/spring-cloud-alibaba-sentinel/pom.xml b/spring-cloud-alibaba-sentinel/pom.xml index d5081390..f0e01d30 100644 --- a/spring-cloud-alibaba-sentinel/pom.xml +++ b/spring-cloud-alibaba-sentinel/pom.xml @@ -84,6 +84,7 @@ com.alibaba.csp sentinel-api-gateway-adapter-common + true diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelAutoConfiguration.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelAutoConfiguration.java index f8f914c2..50e61aa2 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelAutoConfiguration.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelAutoConfiguration.java @@ -16,12 +16,6 @@ package org.springframework.cloud.alibaba.sentinel.custom; -import java.io.IOException; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Map.Entry; - import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; @@ -40,11 +34,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.util.StringUtils; -import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition; -import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem; -import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateGroupItem; -import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem; -import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule; import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser; import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler; import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner; @@ -62,15 +51,8 @@ import com.alibaba.csp.sentinel.slots.system.SystemRule; import com.alibaba.csp.sentinel.transport.config.TransportConfig; import com.alibaba.csp.sentinel.util.AppNameUtil; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.Version; -import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.deser.std.StdDeserializer; -import com.fasterxml.jackson.databind.module.SimpleModule; -import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.dataformat.xml.XmlMapper; /** @@ -203,41 +185,6 @@ public class SentinelAutoConfiguration { @Configuration protected static class SentinelConverterConfiguration { - static class ApiPredicateItemDeserializer - extends StdDeserializer { - private Map> registry = new HashMap>(); - - ApiPredicateItemDeserializer() { - super(ApiPredicateItem.class); - } - - void registerApiPredicateItem(String uniqueAttribute, - Class apiPredicateItemClass) { - registry.put(uniqueAttribute, apiPredicateItemClass); - } - - @Override - public ApiPredicateItem deserialize(JsonParser jp, - DeserializationContext ctxt) throws IOException { - ObjectMapper mapper = (ObjectMapper) jp.getCodec(); - ObjectNode root = mapper.readTree(jp); - Class apiPredicateItemClass = null; - Iterator> elementsIterator = root.fields(); - while (elementsIterator.hasNext()) { - Entry element = elementsIterator.next(); - String name = element.getKey(); - if (registry.containsKey(name)) { - apiPredicateItemClass = registry.get(name); - break; - } - } - if (apiPredicateItemClass == null) { - return null; - } - return mapper.readValue(root.toString(), apiPredicateItemClass); - } - } - @Configuration protected static class SentinelJsonConfiguration { @@ -246,17 +193,6 @@ public class SentinelAutoConfiguration { public SentinelJsonConfiguration() { objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - - ApiPredicateItemDeserializer deserializer = new ApiPredicateItemDeserializer(); - deserializer.registerApiPredicateItem("pattern", - ApiPathPredicateItem.class); - deserializer.registerApiPredicateItem("items", - ApiPredicateGroupItem.class); - SimpleModule module = new SimpleModule( - "PolymorphicApiPredicateItemDeserializerModule", - new Version(1, 0, 0, null)); - module.addDeserializer(ApiPredicateItem.class, deserializer); - objectMapper.registerModule(module); } @Bean("sentinel-json-flow-converter") @@ -284,15 +220,6 @@ public class SentinelAutoConfiguration { return new JsonConverter(objectMapper, ParamFlowRule.class); } - @Bean("sentinel-json-gw-flow-converter") - public JsonConverter jsonGatewayFlowConverter() { - return new JsonConverter(objectMapper, GatewayFlowRule.class); - } - - @Bean("sentinel-json-gw-api-group-converter") - public JsonConverter jsonApiConverter() { - return new JsonConverter(objectMapper, ApiDefinition.class); - } } @ConditionalOnClass(XmlMapper.class) @@ -304,16 +231,6 @@ public class SentinelAutoConfiguration { public SentinelXmlConfiguration() { xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - ApiPredicateItemDeserializer deserializer = new ApiPredicateItemDeserializer(); - deserializer.registerApiPredicateItem("pattern", - ApiPathPredicateItem.class); - deserializer.registerApiPredicateItem("items", - ApiPredicateGroupItem.class); - SimpleModule module = new SimpleModule( - "PolymorphicGatewayDeserializerModule", - new Version(1, 0, 0, null)); - module.addDeserializer(ApiPredicateItem.class, deserializer); - xmlMapper.registerModule(module); } @Bean("sentinel-xml-flow-converter") @@ -341,16 +258,6 @@ public class SentinelAutoConfiguration { return new XmlConverter(xmlMapper, ParamFlowRule.class); } - @Bean("sentinel-xml-gw-flow-converter") - public XmlConverter xmlGatewayFlowConverter() { - return new XmlConverter(xmlMapper, GatewayFlowRule.class); - } - - @Bean("sentinel-xml-gw-api-group-converter") - public XmlConverter xmlApiConverter() { - return new XmlConverter(xmlMapper, ApiDefinition.class); - } - } }