mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Polish #615
This commit is contained in:
@@ -27,6 +27,12 @@
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-api-gateway-adapter-common</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-datasource-nacos</artifactId>
|
||||
|
@@ -6,6 +6,8 @@ import java.util.Optional;
|
||||
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;
|
||||
@@ -40,7 +42,15 @@ public enum RuleType {
|
||||
/**
|
||||
* authority
|
||||
*/
|
||||
AUTHORITY("authority", AuthorityRule.class);
|
||||
AUTHORITY("authority", AuthorityRule.class),
|
||||
/**
|
||||
* gateway flow
|
||||
*/
|
||||
GATEWAY("gateway-flow", GatewayFlowRule.class),
|
||||
/**
|
||||
* api
|
||||
*/
|
||||
API("api", ApiDefinition.class);
|
||||
|
||||
/**
|
||||
* alias for {@link AbstractRule}
|
||||
|
@@ -5,6 +5,8 @@ import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.springframework.cloud.alibaba.sentinel.datasource.RuleType;
|
||||
|
||||
import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
|
||||
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
|
||||
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
|
||||
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
|
||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
|
||||
@@ -82,6 +84,12 @@ public class AbstractDataSourceProperties {
|
||||
case AUTHORITY:
|
||||
AuthorityRuleManager.register2Property(dataSource.getProperty());
|
||||
break;
|
||||
case GATEWAY:
|
||||
GatewayRuleManager.register2Property(dataSource.getProperty());
|
||||
break;
|
||||
case API:
|
||||
GatewayApiDefinitionManager.register2Property(dataSource.getProperty());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@@ -1,7 +1,20 @@
|
||||
package org.springframework.cloud.alibaba.sentinel.datasource.converter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
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.AbstractRule;
|
||||
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;
|
||||
@@ -13,15 +26,6 @@ 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;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.alibaba.sentinel.datasource.RuleType;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Convert sentinel rules for json or xml array Using strict mode to parse json or xml
|
||||
@@ -34,8 +38,8 @@ import java.util.Optional;
|
||||
* @see ParamFlowRule
|
||||
* @see ObjectMapper
|
||||
*/
|
||||
public abstract class SentinelConverter<T extends AbstractRule>
|
||||
implements Converter<String, List<AbstractRule>> {
|
||||
public abstract class SentinelConverter<T extends Object>
|
||||
implements Converter<String, Collection<Object>> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SentinelConverter.class);
|
||||
|
||||
@@ -49,11 +53,20 @@ public abstract class SentinelConverter<T extends AbstractRule>
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AbstractRule> convert(String source) {
|
||||
List<AbstractRule> ruleList = new ArrayList<>();
|
||||
public Collection<Object> convert(String source) {
|
||||
Collection<Object> ruleCollection;
|
||||
|
||||
// hard code
|
||||
if (ruleClass == GatewayFlowRule.class || ruleClass == ApiDefinition.class) {
|
||||
ruleCollection = new HashSet<>();
|
||||
}
|
||||
else {
|
||||
ruleCollection = new ArrayList<>();
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(source)) {
|
||||
log.warn("converter can not convert rules because source is empty");
|
||||
return ruleList;
|
||||
return ruleCollection;
|
||||
}
|
||||
try {
|
||||
List sourceArray = objectMapper.readValue(source,
|
||||
@@ -70,11 +83,11 @@ public abstract class SentinelConverter<T extends AbstractRule>
|
||||
}
|
||||
|
||||
Optional.ofNullable(convertRule(item))
|
||||
.ifPresent(convertRule -> ruleList.add(convertRule));
|
||||
.ifPresent(convertRule -> ruleCollection.add(convertRule));
|
||||
});
|
||||
|
||||
if (ruleList.size() != sourceArray.size()) {
|
||||
throw new IllegalArgumentException("convert " + ruleList.size()
|
||||
if (ruleCollection.size() != sourceArray.size()) {
|
||||
throw new IllegalArgumentException("convert " + ruleCollection.size()
|
||||
+ " rules but there are " + sourceArray.size()
|
||||
+ " rules from datasource. RuleClass: "
|
||||
+ ruleClass.getSimpleName());
|
||||
@@ -83,12 +96,12 @@ public abstract class SentinelConverter<T extends AbstractRule>
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException("convert error: " + e.getMessage(), e);
|
||||
}
|
||||
return ruleList;
|
||||
return ruleCollection;
|
||||
}
|
||||
|
||||
private AbstractRule convertRule(String ruleStr) {
|
||||
private Object convertRule(String ruleStr) {
|
||||
try {
|
||||
final AbstractRule rule = objectMapper.readValue(ruleStr, ruleClass);
|
||||
final Object rule = objectMapper.readValue(ruleStr, ruleClass);
|
||||
RuleType ruleType = RuleType.getByClass(ruleClass).get();
|
||||
switch (ruleType) {
|
||||
case FLOW:
|
||||
|
@@ -46,7 +46,7 @@ public class SentinelConverterTests {
|
||||
@Test
|
||||
public void testJsonConverter() {
|
||||
JsonConverter jsonConverter = new JsonConverter(objectMapper, FlowRule.class);
|
||||
List<FlowRule> flowRules = jsonConverter
|
||||
List<FlowRule> flowRules = (List<FlowRule>) jsonConverter
|
||||
.convert(readFileContent("classpath: flowrule.json"));
|
||||
assertEquals("json converter flow rule size was wrong", 1, flowRules.size());
|
||||
assertEquals("json converter flow rule resource name was wrong", "resource",
|
||||
@@ -67,7 +67,7 @@ public class SentinelConverterTests {
|
||||
@Test
|
||||
public void testConverterEmptyContent() {
|
||||
JsonConverter jsonConverter = new JsonConverter(objectMapper, FlowRule.class);
|
||||
List<FlowRule> flowRules = jsonConverter.convert("");
|
||||
List<FlowRule> flowRules = (List<FlowRule>) jsonConverter.convert("");
|
||||
assertEquals("json converter flow rule size was not empty", 0, flowRules.size());
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ public class SentinelConverterTests {
|
||||
@Test
|
||||
public void testXmlConverter() {
|
||||
XmlConverter jsonConverter = new XmlConverter(xmlMapper, FlowRule.class);
|
||||
List<FlowRule> flowRules = jsonConverter
|
||||
List<FlowRule> flowRules = (List<FlowRule>) jsonConverter
|
||||
.convert(readFileContent("classpath: flowrule.xml"));
|
||||
assertEquals("xml converter flow rule size was wrong", 2, flowRules.size());
|
||||
assertEquals("xml converter flow rule1 resource name was wrong", "resource",
|
||||
|
Reference in New Issue
Block a user