From cd6f79b16b171064b938f06fbe914ba1248ed84b Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Thu, 6 Dec 2018 16:02:18 +0800 Subject: [PATCH] Polish #115 --- spring-cloud-alibaba-dependencies/pom.xml | 5 +++++ .../pom.xml | 7 +++++++ .../datasource/converter/JsonConverter.java | 19 ++++++++++++++---- .../datasource/converter/XmlConverter.java | 20 ++++++++++++++----- spring-cloud-alibaba-sentinel/pom.xml | 5 +++++ .../sentinel/endpoint/SentinelEndpoint.java | 4 ++++ 6 files changed, 51 insertions(+), 9 deletions(-) diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 4d2b1ce27..edbcde127 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -89,6 +89,11 @@ sentinel-core ${sentinel.version} + + com.alibaba.csp + sentinel-parameter-flow-control + ${sentinel.version} + com.alibaba.csp sentinel-datasource-extension diff --git a/spring-cloud-alibaba-sentinel-datasource/pom.xml b/spring-cloud-alibaba-sentinel-datasource/pom.xml index 5d7d85154..2a77bf5a6 100644 --- a/spring-cloud-alibaba-sentinel-datasource/pom.xml +++ b/spring-cloud-alibaba-sentinel-datasource/pom.xml @@ -18,6 +18,13 @@ com.alibaba.csp sentinel-datasource-extension + true + + + + com.alibaba.csp + sentinel-parameter-flow-control + true diff --git a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/converter/JsonConverter.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/converter/JsonConverter.java index 0247ba80d..591d61bd8 100644 --- a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/converter/JsonConverter.java +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/converter/JsonConverter.java @@ -18,6 +18,7 @@ import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; +import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule; import com.alibaba.csp.sentinel.slots.system.SystemRule; import com.fasterxml.jackson.core.JsonProcessingException; @@ -32,6 +33,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; * @see DegradeRule * @see SystemRule * @see AuthorityRule + * @see ParamFlowRule * @see ObjectMapper */ public class JsonConverter implements Converter> { @@ -48,7 +50,7 @@ public class JsonConverter implements Converter> { public List convert(String source) { List ruleList = new ArrayList<>(); if (StringUtils.isEmpty(source)) { - logger.info( + logger.warn( "Sentinel JsonConverter can not convert rules because source is empty"); return ruleList; } @@ -68,7 +70,7 @@ public class JsonConverter implements Converter> { List rules = Arrays.asList(convertFlowRule(itemJson), convertDegradeRule(itemJson), convertSystemRule(itemJson), - convertAuthorityRule(itemJson)); + convertAuthorityRule(itemJson), convertParamFlowRule(itemJson)); List convertRuleList = rules.stream() .filter(rule -> !ObjectUtils.isEmpty(rule)) @@ -101,8 +103,6 @@ public class JsonConverter implements Converter> { throw new RuntimeException( "Sentinel JsonConverter convert error: " + e.getMessage(), e); } - logger.info("Sentinel JsonConverter convert {} rules: {}", ruleList.size(), - ruleList); return ruleList; } @@ -154,4 +154,15 @@ public class JsonConverter implements Converter> { return rule; } + private ParamFlowRule convertParamFlowRule(String json) { + ParamFlowRule rule = null; + try { + rule = objectMapper.readValue(json, ParamFlowRule.class); + } + catch (Exception e) { + // ignore + } + return rule; + } + } diff --git a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/converter/XmlConverter.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/converter/XmlConverter.java index b4a95a992..6ff3f8bb8 100644 --- a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/converter/XmlConverter.java +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/converter/XmlConverter.java @@ -18,6 +18,7 @@ import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; +import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule; import com.alibaba.csp.sentinel.slots.system.SystemRule; import com.fasterxml.jackson.core.JsonProcessingException; @@ -32,7 +33,7 @@ import com.fasterxml.jackson.dataformat.xml.XmlMapper; * @see DegradeRule * @see SystemRule * @see AuthorityRule - * @see XmlMapper + * @see ParamFlowRule */ public class XmlConverter implements Converter> { @@ -48,7 +49,7 @@ public class XmlConverter implements Converter> { public List convert(String source) { List ruleList = new ArrayList<>(); if (StringUtils.isEmpty(source)) { - logger.info( + logger.warn( "Sentinel XmlConverter can not convert rules because source is empty"); return ruleList; } @@ -68,7 +69,7 @@ public class XmlConverter implements Converter> { List rules = Arrays.asList(convertFlowRule(itemXml), convertDegradeRule(itemXml), convertSystemRule(itemXml), - convertAuthorityRule(itemXml)); + convertAuthorityRule(itemXml), convertParamFlowRule(itemXml)); List convertRuleList = rules.stream() .filter(rule -> !ObjectUtils.isEmpty(rule)) @@ -101,8 +102,6 @@ public class XmlConverter implements Converter> { throw new RuntimeException( "Sentinel XmlConverter convert error: " + e.getMessage(), e); } - logger.info("Sentinel XmlConverter convert {} rules: {}", ruleList.size(), - ruleList); return ruleList; } @@ -154,4 +153,15 @@ public class XmlConverter implements Converter> { return rule; } + private ParamFlowRule convertParamFlowRule(String json) { + ParamFlowRule rule = null; + try { + rule = xmlMapper.readValue(json, ParamFlowRule.class); + } + catch (Exception e) { + // ignore + } + return rule; + } + } diff --git a/spring-cloud-alibaba-sentinel/pom.xml b/spring-cloud-alibaba-sentinel/pom.xml index d635b2a0b..f2068eb31 100644 --- a/spring-cloud-alibaba-sentinel/pom.xml +++ b/spring-cloud-alibaba-sentinel/pom.xml @@ -42,6 +42,11 @@ true + + com.alibaba.csp + sentinel-parameter-flow-control + + org.springframework.cloud spring-cloud-alibaba-sentinel-datasource diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/endpoint/SentinelEndpoint.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/endpoint/SentinelEndpoint.java index f20098c2b..03b6c0f8a 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/endpoint/SentinelEndpoint.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/endpoint/SentinelEndpoint.java @@ -32,6 +32,8 @@ import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; +import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule; +import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager; import com.alibaba.csp.sentinel.slots.system.SystemRule; import com.alibaba.csp.sentinel.slots.system.SystemRuleManager; @@ -58,10 +60,12 @@ public class SentinelEndpoint { List flowRules = FlowRuleManager.getRules(); List degradeRules = DegradeRuleManager.getRules(); List systemRules = SystemRuleManager.getRules(); + List paramFlowRules = ParamFlowRuleManager.getRules(); result.put("properties", sentinelProperties); result.put("FlowRules", flowRules); result.put("DegradeRules", degradeRules); result.put("SystemRules", systemRules); + result.put("ParamFlowRule", paramFlowRules); result.put("datasources", new HashMap()); dataSourceHandler.getDataSourceBeanNameList().forEach(dataSourceBeanName -> { ReadableDataSource dataSource = applicationContext.getBean(dataSourceBeanName,