Merge pull request #338 from fangjian0423/binder-dev

remove sentinel commercialization logic &  optimize NacosDataSource
pull/339/head
xiaojing 6 years ago committed by GitHub
commit 13097fd32c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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";
}

@ -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;
@ -18,13 +16,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 +32,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 +94,9 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties {
this.secretKey = secretKey;
}
public static NacosDataSourceProperties buildFlowByEDAS() {
return buildByEDAS("flow");
}
public static NacosDataSourceProperties buildDegradeByEDAS() {
return buildByEDAS("degrade");
public boolean acmPropertiesInvalid() {
return StringUtils.isEmpty(endpoint) || StringUtils.isEmpty(accessKey)
|| StringUtils.isEmpty(secretKey) || StringUtils.isEmpty(namespace);
}
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;
}
}

@ -3,7 +3,6 @@ package org.springframework.cloud.alibaba.sentinel.datasource.factorybean;
import java.util.Properties;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.cloud.alibaba.sentinel.datasource.SentinelDataSourceConstants;
import org.springframework.util.StringUtils;
import com.alibaba.csp.sentinel.datasource.Converter;
@ -30,16 +29,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 properties = new Properties();
if (!StringUtils.isEmpty(this.serverAddr)) {
properties.setProperty(PropertyKeyConst.SERVER_ADDR, this.serverAddr);
}
else {
properties.setProperty(PropertyKeyConst.ACCESS_KEY, this.accessKey);
properties.setProperty(PropertyKeyConst.SERVER_ADDR, this.secretKey);
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);
}
return new NacosDataSource(serverAddr, groupId, dataId, converter);
return new NacosDataSource(properties, groupId, dataId, converter);
}
@Override

@ -17,7 +17,6 @@
package org.springframework.cloud.alibaba.sentinel.datasource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.springframework.cloud.alibaba.sentinel.datasource.config.NacosDataSourceProperties;
@ -71,70 +70,4 @@ public class NacosDataSourcePropertiesTests {
nacosDataSourceProperties.getRuleType());
}
@Test
public void testNacosWithEDASAndWithoutSystemProperties() {
NacosDataSourceProperties nacosDataSourceProperties = NacosDataSourceProperties
.buildByEDAS(RuleType.DEGRADE.getName());
assertEquals("Nacos groupId was wrong", "nacos-sentinel",
nacosDataSourceProperties.getGroupId());
assertEquals("Nacos dataId was wrong", "null-" + RuleType.DEGRADE.getName(),
nacosDataSourceProperties.getDataId());
assertEquals("Nacos default data type was wrong", "json",
nacosDataSourceProperties.getDataType());
assertEquals("Nacos rule type was wrong", RuleType.DEGRADE,
nacosDataSourceProperties.getRuleType());
assertNull("Nacos ak was not null", nacosDataSourceProperties.getAccessKey());
assertNull("Nacos sk was not null", nacosDataSourceProperties.getSecretKey());
assertNull("Nacos endpoint was not null",
nacosDataSourceProperties.getEndpoint());
assertNull("Nacos namespace was not null",
nacosDataSourceProperties.getNamespace());
assertNull("Nacos serverAddr was not null",
nacosDataSourceProperties.getServerAddr());
}
@Test
public void testNacosWithEDASDegradeAndWithoutSystemProperties() {
NacosDataSourceProperties nacosDataSourceProperties = NacosDataSourceProperties
.buildDegradeByEDAS();
assertEquals("Nacos groupId was wrong", "nacos-sentinel",
nacosDataSourceProperties.getGroupId());
assertEquals("Nacos dataId was wrong", "null-" + RuleType.DEGRADE.getName(),
nacosDataSourceProperties.getDataId());
assertEquals("Nacos default data type was wrong", "json",
nacosDataSourceProperties.getDataType());
assertEquals("Nacos rule type was wrong", RuleType.DEGRADE,
nacosDataSourceProperties.getRuleType());
assertNull("Nacos ak was not null", nacosDataSourceProperties.getAccessKey());
assertNull("Nacos sk was not null", nacosDataSourceProperties.getSecretKey());
assertNull("Nacos endpoint was not null",
nacosDataSourceProperties.getEndpoint());
assertNull("Nacos namespace was not null",
nacosDataSourceProperties.getNamespace());
assertNull("Nacos serverAddr was not null",
nacosDataSourceProperties.getServerAddr());
}
@Test
public void testNacosWithEDASFlowAndWithoutSystemProperties() {
NacosDataSourceProperties nacosDataSourceProperties = NacosDataSourceProperties
.buildFlowByEDAS();
assertEquals("Nacos groupId was wrong", "nacos-sentinel",
nacosDataSourceProperties.getGroupId());
assertEquals("Nacos dataId was wrong", "null-" + RuleType.FLOW.getName(),
nacosDataSourceProperties.getDataId());
assertEquals("Nacos default data type was wrong", "json",
nacosDataSourceProperties.getDataType());
assertEquals("Nacos rule type was wrong", RuleType.FLOW,
nacosDataSourceProperties.getRuleType());
assertNull("Nacos ak was not null", nacosDataSourceProperties.getAccessKey());
assertNull("Nacos sk was not null", nacosDataSourceProperties.getSecretKey());
assertNull("Nacos endpoint was not null",
nacosDataSourceProperties.getEndpoint());
assertNull("Nacos namespace was not null",
nacosDataSourceProperties.getNamespace());
assertNull("Nacos serverAddr was not null",
nacosDataSourceProperties.getServerAddr());
}
}

@ -1,151 +0,0 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alibaba.sentinel.datasource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.springframework.cloud.alibaba.sentinel.datasource.config.NacosDataSourceProperties;
/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
public class NacosDataSourcePropertiesWithSystemPropertiesTests {
@Test
public void testNacosWithSystemProperties() {
setSystemProperties();
NacosDataSourceProperties nacosDataSourceProperties = new NacosDataSourceProperties();
nacosDataSourceProperties.setServerAddr("127.0.0.1:8848");
nacosDataSourceProperties.setGroupId("custom-group");
nacosDataSourceProperties.setDataId("sentinel");
nacosDataSourceProperties.preCheck("test-ds");
assertEquals("Nacos groupId was wrong", "custom-group",
nacosDataSourceProperties.getGroupId());
assertEquals("Nacos dataId was wrong", "sentinel",
nacosDataSourceProperties.getDataId());
assertEquals("Nacos default data type was wrong", "json",
nacosDataSourceProperties.getDataType());
assertEquals("Nacos ak was wrong", "ak",
nacosDataSourceProperties.getAccessKey());
assertEquals("Nacos sk was wrong", "sk",
nacosDataSourceProperties.getSecretKey());
assertEquals("Nacos endpoint was wrong", "endpoint",
nacosDataSourceProperties.getEndpoint());
assertEquals("Nacos namespace was wrong", "namespace",
nacosDataSourceProperties.getNamespace());
assertNull("Nacos serverAddr was not null",
nacosDataSourceProperties.getServerAddr());
}
@Test
public void testNacosWithEDASAndSystemProperties() {
setSystemProperties();
NacosDataSourceProperties nacosDataSourceProperties = NacosDataSourceProperties
.buildByEDAS(RuleType.FLOW.getName());
assertEquals("Nacos groupId was wrong", "nacos-sentinel",
nacosDataSourceProperties.getGroupId());
assertEquals("Nacos dataId was wrong", "project-name-" + RuleType.FLOW.getName(),
nacosDataSourceProperties.getDataId());
assertEquals("Nacos default data type was wrong", "json",
nacosDataSourceProperties.getDataType());
assertEquals("Nacos rule type was wrong", RuleType.FLOW,
nacosDataSourceProperties.getRuleType());
assertEquals("Nacos ak was wrong", "ak",
nacosDataSourceProperties.getAccessKey());
assertEquals("Nacos sk was wrong", "sk",
nacosDataSourceProperties.getSecretKey());
assertEquals("Nacos endpoint was wrong", "endpoint",
nacosDataSourceProperties.getEndpoint());
assertEquals("Nacos namespace was wrong", "namespace",
nacosDataSourceProperties.getNamespace());
assertNull("Nacos serverAddr was not null",
nacosDataSourceProperties.getServerAddr());
}
@Test
public void testNacosWithEDASDegradeAndSystemProperties() {
setSystemProperties();
NacosDataSourceProperties nacosDataSourceProperties = NacosDataSourceProperties
.buildDegradeByEDAS();
assertEquals("Nacos groupId was wrong", "nacos-sentinel",
nacosDataSourceProperties.getGroupId());
assertEquals("Nacos dataId was wrong",
"project-name-" + RuleType.DEGRADE.getName(),
nacosDataSourceProperties.getDataId());
assertEquals("Nacos default data type was wrong", "json",
nacosDataSourceProperties.getDataType());
assertEquals("Nacos rule type was wrong", RuleType.DEGRADE,
nacosDataSourceProperties.getRuleType());
assertEquals("Nacos ak was wrong", "ak",
nacosDataSourceProperties.getAccessKey());
assertEquals("Nacos sk was wrong", "sk",
nacosDataSourceProperties.getSecretKey());
assertEquals("Nacos endpoint was wrong", "endpoint",
nacosDataSourceProperties.getEndpoint());
assertEquals("Nacos namespace was wrong", "namespace",
nacosDataSourceProperties.getNamespace());
assertNull("Nacos serverAddr was not null",
nacosDataSourceProperties.getServerAddr());
}
@Test
public void testNacosWithEDASFlowAndSystemProperties() {
setSystemProperties();
NacosDataSourceProperties nacosDataSourceProperties = NacosDataSourceProperties
.buildFlowByEDAS();
assertEquals("Nacos groupId was wrong", "nacos-sentinel",
nacosDataSourceProperties.getGroupId());
assertEquals("Nacos dataId was wrong", "project-name-" + RuleType.FLOW.getName(),
nacosDataSourceProperties.getDataId());
assertEquals("Nacos default data type was wrong", "json",
nacosDataSourceProperties.getDataType());
assertEquals("Nacos rule type was wrong", RuleType.FLOW,
nacosDataSourceProperties.getRuleType());
assertEquals("Nacos ak was wrong", "ak",
nacosDataSourceProperties.getAccessKey());
assertEquals("Nacos sk was wrong", "sk",
nacosDataSourceProperties.getSecretKey());
assertEquals("Nacos endpoint was wrong", "endpoint",
nacosDataSourceProperties.getEndpoint());
assertEquals("Nacos namespace was wrong", "namespace",
nacosDataSourceProperties.getNamespace());
assertNull("Nacos serverAddr was not null",
nacosDataSourceProperties.getServerAddr());
}
private void setSystemProperties() {
System.setProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_ENDPOINT,
"endpoint");
System.setProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_NAMESPACE,
"namespace");
System.setProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_AK, "ak");
System.setProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_SK, "sk");
System.setProperty(SentinelDataSourceConstants.PROJECT_NAME, "project-name");
}
}

@ -6,7 +6,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -14,12 +13,8 @@ import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.cloud.alibaba.sentinel.SentinelConstants;
import org.springframework.cloud.alibaba.sentinel.SentinelProperties;
import org.springframework.cloud.alibaba.sentinel.datasource.SentinelDataSourceConstants;
import org.springframework.cloud.alibaba.sentinel.datasource.config.AbstractDataSourceProperties;
import org.springframework.cloud.alibaba.sentinel.datasource.config.DataSourcePropertiesConfiguration;
import org.springframework.cloud.alibaba.sentinel.datasource.config.NacosDataSourceProperties;
import org.springframework.cloud.alibaba.sentinel.datasource.converter.JsonConverter;
import org.springframework.cloud.alibaba.sentinel.datasource.converter.XmlConverter;
import org.springframework.util.CollectionUtils;
@ -29,8 +24,6 @@ import org.springframework.util.StringUtils;
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.slots.block.AbstractRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
/**
* Sentinel {@link ReadableDataSource} Handler Handle the configurations of
@ -63,27 +56,6 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
@Override
public void afterSingletonsInstantiated() {
// commercialization
if (!StringUtils.isEmpty(System.getProperties()
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_ENDPOINT))) {
Map<String, DataSourcePropertiesConfiguration> newDataSourceMap = new TreeMap<>(
String.CASE_INSENSITIVE_ORDER);
for (Map.Entry<String, DataSourcePropertiesConfiguration> entry : sentinelProperties
.getDatasource().entrySet()) {
if (entry.getValue().getValidDataSourceProperties()
.getClass() != NacosDataSourceProperties.class) {
newDataSourceMap.put(entry.getKey(), entry.getValue());
}
}
newDataSourceMap.put(SentinelConstants.FLOW_DATASOURCE_NAME,
new DataSourcePropertiesConfiguration(
NacosDataSourceProperties.buildFlowByEDAS()));
newDataSourceMap.put(SentinelConstants.DEGRADE_DATASOURCE_NAME,
new DataSourcePropertiesConfiguration(
NacosDataSourceProperties.buildDegradeByEDAS()));
sentinelProperties.setDatasource(newDataSourceMap);
}
sentinelProperties.getDatasource()
.forEach((dataSourceName, dataSourceProperties) -> {
try {
@ -214,17 +186,6 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
// register property in RuleManager
dataSourceProperties.postRegister(newDataSource);
// commercialization
if (!StringUtils.isEmpty(System.getProperties()
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_ENDPOINT))) {
if (dataSourceName.contains(SentinelConstants.FLOW_DATASOURCE_NAME)) {
FlowRuleManager.register2Property(newDataSource.getProperty());
}
else if (dataSourceName.contains(SentinelConstants.DEGRADE_DATASOURCE_NAME)) {
DegradeRuleManager.register2Property(newDataSource.getProperty());
}
}
}
private void logAndCheckRuleType(AbstractDataSource dataSource, String dataSourceName,

Loading…
Cancel
Save