mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
sync sentinel in finchley:
1. support spring cloud gateway and webflux 2. fix bug about feign 3. enhance converter
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
|
||||
*/
|
||||
GW_FLOW("gw-flow", GatewayFlowRule.class),
|
||||
/**
|
||||
* api
|
||||
*/
|
||||
GW_API_GROUP("gw-api-group", ApiDefinition.class);
|
||||
|
||||
/**
|
||||
* alias for {@link AbstractRule}
|
||||
|
@@ -4,7 +4,10 @@ import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.springframework.cloud.alibaba.sentinel.datasource.RuleType;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
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;
|
||||
@@ -28,6 +31,8 @@ public class AbstractDataSourceProperties {
|
||||
private String converterClass;
|
||||
@JsonIgnore
|
||||
private final String factoryBeanName;
|
||||
@JsonIgnore
|
||||
private Environment env;
|
||||
|
||||
public AbstractDataSourceProperties(String factoryBeanName) {
|
||||
this.factoryBeanName = factoryBeanName;
|
||||
@@ -61,6 +66,14 @@ public class AbstractDataSourceProperties {
|
||||
return factoryBeanName;
|
||||
}
|
||||
|
||||
protected Environment getEnv() {
|
||||
return env;
|
||||
}
|
||||
|
||||
public void setEnv(Environment env) {
|
||||
this.env = env;
|
||||
}
|
||||
|
||||
public void preCheck(String dataSourceName) {
|
||||
|
||||
}
|
||||
@@ -82,6 +95,12 @@ public class AbstractDataSourceProperties {
|
||||
case AUTHORITY:
|
||||
AuthorityRuleManager.register2Property(dataSource.getProperty());
|
||||
break;
|
||||
case GW_FLOW:
|
||||
GatewayRuleManager.register2Property(dataSource.getProperty());
|
||||
break;
|
||||
case GW_API_GROUP:
|
||||
GatewayApiDefinitionManager.register2Property(dataSource.getProperty());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@@ -2,8 +2,6 @@ package org.springframework.cloud.alibaba.sentinel.datasource.config;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
import org.springframework.cloud.alibaba.sentinel.datasource.RuleType;
|
||||
import org.springframework.cloud.alibaba.sentinel.datasource.SentinelDataSourceConstants;
|
||||
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.NacosDataSourceFactoryBean;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -34,9 +32,13 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties {
|
||||
|
||||
@Override
|
||||
public void preCheck(String dataSourceName) {
|
||||
if (StringUtils.isEmpty(serverAddr) && acmPropertiesInvalid()) {
|
||||
throw new IllegalArgumentException(
|
||||
"NacosDataSource properties value not correct. serverAddr is empty but there is empty value in accessKey, secretKey, endpoint, namespace property");
|
||||
if (StringUtils.isEmpty(serverAddr)) {
|
||||
serverAddr = this.getEnv().getProperty(
|
||||
"spring.cloud.sentinel.datasource.nacos.server-addr", "");
|
||||
if (StringUtils.isEmpty(serverAddr)) {
|
||||
throw new IllegalArgumentException(
|
||||
"NacosDataSource server-addr is empty");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,9 +98,4 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties {
|
||||
this.secretKey = secretKey;
|
||||
}
|
||||
|
||||
public boolean acmPropertiesInvalid() {
|
||||
return StringUtils.isEmpty(endpoint) || StringUtils.isEmpty(accessKey)
|
||||
|| StringUtils.isEmpty(secretKey) || StringUtils.isEmpty(namespace);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ package org.springframework.cloud.alibaba.sentinel.datasource.config;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.ZookeeperDataSourceFactoryBean;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Zookeeper Properties class Using by {@link DataSourcePropertiesConfiguration} and
|
||||
@@ -25,6 +26,18 @@ public class ZookeeperDataSourceProperties extends AbstractDataSourceProperties
|
||||
|
||||
private String dataId;
|
||||
|
||||
@Override
|
||||
public void preCheck(String dataSourceName) {
|
||||
if (StringUtils.isEmpty(serverAddr)) {
|
||||
serverAddr = this.getEnv()
|
||||
.getProperty("spring.cloud.sentinel.datasource.zk.server-addr", "");
|
||||
if (StringUtils.isEmpty(serverAddr)) {
|
||||
throw new IllegalArgumentException(
|
||||
"ZookeeperDataSource server-addr is empty");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getServerAddr() {
|
||||
return serverAddr;
|
||||
}
|
||||
|
@@ -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