mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
sentinel HTTP_METHOD_SPECIFY
This commit is contained in:
parent
0039974737
commit
8023b07ebe
@ -43,10 +43,10 @@
|
|||||||
<!--<groupId>com.alibaba.csp</groupId>-->
|
<!--<groupId>com.alibaba.csp</groupId>-->
|
||||||
<!--<artifactId>sentinel-datasource-apollo</artifactId>-->
|
<!--<artifactId>sentinel-datasource-apollo</artifactId>-->
|
||||||
<!--</dependency>-->
|
<!--</dependency>-->
|
||||||
<dependency>
|
<!--<dependency>-->
|
||||||
<groupId>com.alibaba.csp</groupId>
|
<!--<groupId>com.alibaba.csp</groupId>-->
|
||||||
<artifactId>sentinel-datasource-redis</artifactId>
|
<!--<artifactId>sentinel-datasource-redis</artifactId>-->
|
||||||
</dependency>
|
<!--</dependency>-->
|
||||||
<!-- define in spring-boot-autoconfigure module -->
|
<!-- define in spring-boot-autoconfigure module -->
|
||||||
<!--<dependency>-->
|
<!--<dependency>-->
|
||||||
<!--<groupId>com.fasterxml.jackson.dataformat</groupId>-->
|
<!--<groupId>com.fasterxml.jackson.dataformat</groupId>-->
|
||||||
|
@ -3,6 +3,7 @@ server.port=18083
|
|||||||
management.endpoints.web.exposure.include=*
|
management.endpoints.web.exposure.include=*
|
||||||
spring.cloud.sentinel.transport.dashboard=localhost:8080
|
spring.cloud.sentinel.transport.dashboard=localhost:8080
|
||||||
spring.cloud.sentinel.eager=true
|
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.file=classpath: flowrule.json
|
||||||
spring.cloud.sentinel.datasource.ds1.file.data-type=json
|
spring.cloud.sentinel.datasource.ds1.file.data-type=json
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package com.alibaba.cloud.sentinel.datasource.converter;
|
package com.alibaba.cloud.sentinel.datasource.converter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -27,17 +28,13 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import com.alibaba.cloud.sentinel.datasource.RuleType;
|
|
||||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||||
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
|
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.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.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.block.flow.param.ParamFlowRule;
|
||||||
import com.alibaba.csp.sentinel.slots.system.SystemRule;
|
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.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
@ -88,56 +85,33 @@ public abstract class SentinelConverter<T extends Object>
|
|||||||
List sourceArray = objectMapper.readValue(source,
|
List sourceArray = objectMapper.readValue(source,
|
||||||
new TypeReference<List<HashMap>>() {
|
new TypeReference<List<HashMap>>() {
|
||||||
});
|
});
|
||||||
sourceArray.stream().forEach(obj -> {
|
for (Object obj : sourceArray) {
|
||||||
|
|
||||||
String item = null;
|
String item = null;
|
||||||
try {
|
try {
|
||||||
item = objectMapper.writeValueAsString(obj);
|
item = objectMapper.writeValueAsString(obj);
|
||||||
|
Optional.ofNullable(convertRule(item))
|
||||||
|
.ifPresent(convertRule -> ruleCollection.add(convertRule));
|
||||||
}
|
}
|
||||||
catch (JsonProcessingException e) {
|
catch (IOException e) {
|
||||||
// won't be happen
|
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) {
|
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;
|
return ruleCollection;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object convertRule(String ruleStr) {
|
private Object convertRule(String ruleStr) throws IOException {
|
||||||
try {
|
return objectMapper.readValue(ruleStr, ruleClass);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -89,6 +89,11 @@ public class SentinelProperties {
|
|||||||
*/
|
*/
|
||||||
private Log log = new Log();
|
private Log log = new Log();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add HTTP method prefix for Sentinel Resource.
|
||||||
|
*/
|
||||||
|
private Boolean httpMethodSpecify = false;
|
||||||
|
|
||||||
public boolean isEager() {
|
public boolean isEager() {
|
||||||
return eager;
|
return eager;
|
||||||
}
|
}
|
||||||
@ -161,6 +166,14 @@ public class SentinelProperties {
|
|||||||
this.log = log;
|
this.log = log;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Boolean getHttpMethodSpecify() {
|
||||||
|
return httpMethodSpecify;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHttpMethodSpecify(Boolean httpMethodSpecify) {
|
||||||
|
this.httpMethodSpecify = httpMethodSpecify;
|
||||||
|
}
|
||||||
|
|
||||||
public static class Flow {
|
public static class Flow {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -91,6 +91,8 @@ public class SentinelWebAutoConfiguration {
|
|||||||
Filter filter = new CommonFilter();
|
Filter filter = new CommonFilter();
|
||||||
registration.setFilter(filter);
|
registration.setFilter(filter);
|
||||||
registration.setOrder(filterConfig.getOrder());
|
registration.setOrder(filterConfig.getOrder());
|
||||||
|
registration.addInitParameter("HTTP_METHOD_SPECIFY",
|
||||||
|
String.valueOf(properties.getHttpMethodSpecify()));
|
||||||
log.info(
|
log.info(
|
||||||
"[Sentinel Starter] register Sentinel CommonFilter with urlPatterns: {}.",
|
"[Sentinel Starter] register Sentinel CommonFilter with urlPatterns: {}.",
|
||||||
filterConfig.getUrlPatterns());
|
filterConfig.getUrlPatterns());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user