From 8023b07ebe2ed0dfb30f75d2d795887b05465cf1 Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Tue, 30 Jul 2019 12:55:21 +0800 Subject: [PATCH] sentinel HTTP_METHOD_SPECIFY --- .../sentinel-core-example/pom.xml | 8 +-- .../src/main/resources/application.properties | 1 + .../converter/SentinelConverter.java | 58 +++++-------------- .../cloud/sentinel/SentinelProperties.java | 13 +++++ .../SentinelWebAutoConfiguration.java | 2 + 5 files changed, 36 insertions(+), 46 deletions(-) diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml index a07c6c9b..10358495 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml @@ -43,10 +43,10 @@ - - com.alibaba.csp - sentinel-datasource-redis - + + + + diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties index 20708bd9..cc772c49 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties @@ -3,6 +3,7 @@ server.port=18083 management.endpoints.web.exposure.include=* spring.cloud.sentinel.transport.dashboard=localhost:8080 spring.cloud.sentinel.eager=true +#spring.cloud.sentinel.http-method-specify=false spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json spring.cloud.sentinel.datasource.ds1.file.data-type=json diff --git a/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/converter/SentinelConverter.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/converter/SentinelConverter.java index 1d8269d7..f388cbb8 100644 --- a/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/converter/SentinelConverter.java +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/converter/SentinelConverter.java @@ -16,6 +16,7 @@ package com.alibaba.cloud.sentinel.datasource.converter; +import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -27,17 +28,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.StringUtils; -import com.alibaba.cloud.sentinel.datasource.RuleType; 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; -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.FlowRuleUtil; import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule; import com.alibaba.csp.sentinel.slots.system.SystemRule; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; @@ -88,56 +85,33 @@ public abstract class SentinelConverter List sourceArray = objectMapper.readValue(source, new TypeReference>() { }); - sourceArray.stream().forEach(obj -> { - + for (Object obj : sourceArray) { String item = null; try { item = objectMapper.writeValueAsString(obj); + Optional.ofNullable(convertRule(item)) + .ifPresent(convertRule -> ruleCollection.add(convertRule)); } - catch (JsonProcessingException e) { - // won't be happen + catch (IOException e) { + log.error("sentinel rule convert error: " + e.getMessage(), e); + throw new IllegalArgumentException( + "sentinel rule convert error: " + e.getMessage(), e); } - - Optional.ofNullable(convertRule(item)) - .ifPresent(convertRule -> ruleCollection.add(convertRule)); - }); - - if (ruleCollection.size() != sourceArray.size()) { - throw new IllegalArgumentException("convert " + ruleCollection.size() - + " rules but there are " + sourceArray.size() - + " rules from datasource. RuleClass: " - + ruleClass.getSimpleName()); } } catch (Exception e) { - throw new RuntimeException("convert error: " + e.getMessage(), e); + if (e instanceof RuntimeException) { + throw (RuntimeException) e; + } + else { + throw new RuntimeException("convert error: " + e.getMessage(), e); + } } return ruleCollection; } - private Object convertRule(String ruleStr) { - try { - final Object rule = objectMapper.readValue(ruleStr, ruleClass); - RuleType ruleType = RuleType.getByClass(ruleClass).get(); - switch (ruleType) { - case FLOW: - if (!FlowRuleUtil.isValidRule((FlowRule) rule)) { - return null; - } - break; - case DEGRADE: - if (!DegradeRuleManager.isValidRule((DegradeRule) rule)) { - return null; - } - default: - break; - } - return rule; - } - catch (Exception e) { - // ignore - } - return null; + private Object convertRule(String ruleStr) throws IOException { + return objectMapper.readValue(ruleStr, ruleClass); } } diff --git a/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelProperties.java b/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelProperties.java index 8fab12bf..1f00c16d 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelProperties.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelProperties.java @@ -89,6 +89,11 @@ public class SentinelProperties { */ private Log log = new Log(); + /** + * Add HTTP method prefix for Sentinel Resource. + */ + private Boolean httpMethodSpecify = false; + public boolean isEager() { return eager; } @@ -161,6 +166,14 @@ public class SentinelProperties { this.log = log; } + public Boolean getHttpMethodSpecify() { + return httpMethodSpecify; + } + + public void setHttpMethodSpecify(Boolean httpMethodSpecify) { + this.httpMethodSpecify = httpMethodSpecify; + } + public static class Flow { /** diff --git a/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelWebAutoConfiguration.java b/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelWebAutoConfiguration.java index dadb235f..5dfe6884 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelWebAutoConfiguration.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelWebAutoConfiguration.java @@ -91,6 +91,8 @@ public class SentinelWebAutoConfiguration { Filter filter = new CommonFilter(); registration.setFilter(filter); registration.setOrder(filterConfig.getOrder()); + registration.addInitParameter("HTTP_METHOD_SPECIFY", + String.valueOf(properties.getHttpMethodSpecify())); log.info( "[Sentinel Starter] register Sentinel CommonFilter with urlPatterns: {}.", filterConfig.getUrlPatterns());