1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

Merge remote-tracking branch 'upstream/master'

# Conflicts:
#	spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java
#	spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java
#	spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/config/NacosDataSourceProperties.java
#	spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelDataSourceHandler.java
This commit is contained in:
mercyblitz
2019-02-01 22:08:21 +08:00
107 changed files with 5467 additions and 561 deletions

View File

@@ -23,14 +23,4 @@ public interface SentinelDataSourceConstants {
String PROPERTY_PREFIX = "spring.cloud.sentinel";
String NACOS_DATASOURCE_AK = PROPERTY_PREFIX + ".nacos.config.access-key";
String NACOS_DATASOURCE_SK = PROPERTY_PREFIX + ".nacos.config.secret-key";
String NACOS_DATASOURCE_NAMESPACE = PROPERTY_PREFIX + ".nacos.config.namespace";
String NACOS_DATASOURCE_ENDPOINT = PROPERTY_PREFIX + ".nacos.config.endpoint";
String PROJECT_NAME = PROPERTY_PREFIX + ".nacos.config.project-name";
}

View File

@@ -27,7 +27,7 @@ public class AbstractDataSourceProperties {
private RuleType ruleType;
private String converterClass;
@JsonIgnore
protected String factoryBeanName;
private final String factoryBeanName;
public AbstractDataSourceProperties(String factoryBeanName) {
this.factoryBeanName = factoryBeanName;
@@ -61,10 +61,6 @@ public class AbstractDataSourceProperties {
return factoryBeanName;
}
public void setFactoryBeanName(String factoryBeanName) {
this.factoryBeanName = factoryBeanName;
}
public void preCheck(String dataSourceName) {
}

View File

@@ -1,12 +1,12 @@
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;
import javax.validation.constraints.NotEmpty;
/**
* Nacos Properties class Using by {@link DataSourcePropertiesConfiguration} and
* {@link NacosDataSourceFactoryBean}
@@ -18,13 +18,11 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties {
private String serverAddr;
@NotEmpty
private String groupId;
private String groupId = "DEFAULT_GROUP";
@NotEmpty
private String dataId;
// commercialized usage
private String endpoint;
private String namespace;
private String accessKey;
@@ -36,17 +34,9 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties {
@Override
public void preCheck(String dataSourceName) {
if (!StringUtils.isEmpty(System.getProperties()
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_ENDPOINT))) {
this.setServerAddr(null);
this.setEndpoint(System.getProperties()
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_ENDPOINT));
this.setNamespace(System.getProperties()
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_NAMESPACE));
this.setAccessKey(System.getProperties()
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_AK));
this.setSecretKey(System.getProperties()
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_SK));
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");
}
}
@@ -106,34 +96,9 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties {
this.secretKey = secretKey;
}
public static NacosDataSourceProperties buildFlowByEDAS() {
return buildByEDAS("flow");
public boolean acmPropertiesInvalid() {
return StringUtils.isEmpty(endpoint) || StringUtils.isEmpty(accessKey)
|| StringUtils.isEmpty(secretKey) || StringUtils.isEmpty(namespace);
}
public static NacosDataSourceProperties buildDegradeByEDAS() {
return buildByEDAS("degrade");
}
public static NacosDataSourceProperties buildByEDAS(String type) {
NacosDataSourceProperties result = new NacosDataSourceProperties();
result.setEndpoint(System.getProperties()
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_ENDPOINT));
result.setNamespace(System.getProperties()
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_NAMESPACE));
result.setAccessKey(System.getProperties()
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_AK));
result.setSecretKey(System.getProperties()
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_SK));
result.setDataType("json");
result.setDataId(System.getProperties()
.getProperty(SentinelDataSourceConstants.PROJECT_NAME) + "-" + type);
result.setGroupId("nacos-sentinel");
if (type.equals(RuleType.FLOW.getName())) {
result.setRuleType(RuleType.FLOW);
}
else {
result.setRuleType(RuleType.DEGRADE);
}
return result;
}
}

View File

@@ -4,7 +4,6 @@ import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.nacos.api.PropertyKeyConst;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.cloud.alibaba.sentinel.datasource.SentinelDataSourceConstants;
import org.springframework.util.StringUtils;
import java.util.Properties;
@@ -29,16 +28,19 @@ public class NacosDataSourceFactoryBean implements FactoryBean<NacosDataSource>
@Override
public NacosDataSource getObject() throws Exception {
if (!StringUtils.isEmpty(System.getProperties()
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_ENDPOINT))) {
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.ACCESS_KEY, this.accessKey);
properties.setProperty(PropertyKeyConst.SERVER_ADDR, this.secretKey);
properties.setProperty(PropertyKeyConst.ENDPOINT, this.endpoint);
properties.setProperty(PropertyKeyConst.NAMESPACE, this.namespace);
return new NacosDataSource(properties, groupId, dataId, converter);
Properties properties = new Properties();
if (!StringUtils.isEmpty(this.serverAddr)) {
properties.setProperty(PropertyKeyConst.SERVER_ADDR, this.serverAddr);
}
return new NacosDataSource(serverAddr, groupId, dataId, converter);
else {
properties.setProperty(PropertyKeyConst.ACCESS_KEY, this.accessKey);
properties.setProperty(PropertyKeyConst.SECRET_KEY, this.secretKey);
properties.setProperty(PropertyKeyConst.ENDPOINT, this.endpoint);
}
if (!StringUtils.isEmpty(this.namespace)) {
properties.setProperty(PropertyKeyConst.NAMESPACE, this.namespace);
}
return new NacosDataSource(properties, groupId, dataId, converter);
}
@Override