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-docs/src/main/asciidoc-zh/sentinel.adoc
#	spring-cloud-alibaba-docs/src/main/asciidoc/sentinel.adoc
This commit is contained in:
mercyblitz
2019-02-21 14:49:26 +08:00
97 changed files with 1617 additions and 1532 deletions

View File

@@ -1,15 +1,5 @@
package org.springframework.cloud.alibaba.sentinel.datasource.converter;
import java.util.ArrayList;
import java.util.HashMap;
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.datasource.Converter;
import com.alibaba.csp.sentinel.slots.block.AbstractRule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
@@ -23,6 +13,15 @@ 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
@@ -38,7 +37,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
public abstract class SentinelConverter<T extends AbstractRule>
implements Converter<String, List<AbstractRule>> {
private static final Logger logger = LoggerFactory.getLogger(SentinelConverter.class);
private static final Logger log = LoggerFactory.getLogger(SentinelConverter.class);
private final ObjectMapper objectMapper;
@@ -53,7 +52,7 @@ public abstract class SentinelConverter<T extends AbstractRule>
public List<AbstractRule> convert(String source) {
List<AbstractRule> ruleList = new ArrayList<>();
if (StringUtils.isEmpty(source)) {
logger.warn("converter can not convert rules because source is empty");
log.warn("converter can not convert rules because source is empty");
return ruleList;
}
try {

View File

@@ -1,230 +1,230 @@
/*
* 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.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.springframework.cloud.alibaba.sentinel.datasource.config.ApolloDataSourceProperties;
import org.springframework.cloud.alibaba.sentinel.datasource.config.DataSourcePropertiesConfiguration;
import org.springframework.cloud.alibaba.sentinel.datasource.config.FileDataSourceProperties;
import org.springframework.cloud.alibaba.sentinel.datasource.config.NacosDataSourceProperties;
import org.springframework.cloud.alibaba.sentinel.datasource.config.ZookeeperDataSourceProperties;
/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
public class DataSourcePropertiesConfigurationTests {
@Test
public void testFileAttr() {
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration();
assertEquals("DataSourcePropertiesConfiguration valid field size was wrong", 0,
dataSourcePropertiesConfiguration.getValidField().size());
assertNull("DataSourcePropertiesConfiguration valid properties was not null",
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
FileDataSourceProperties fileDataSourceProperties = buildFileProperties();
dataSourcePropertiesConfiguration.setFile(fileDataSourceProperties);
assertEquals(
"DataSourcePropertiesConfiguration valid field size was wrong after set file attribute",
1, dataSourcePropertiesConfiguration.getValidField().size());
assertNotNull(
"DataSourcePropertiesConfiguration file properties was null after set file attribute",
dataSourcePropertiesConfiguration.getFile());
assertNotNull(
"DataSourcePropertiesConfiguration valid properties was null after set file attribute",
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
}
@Test
public void testNacosAttr() {
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration();
assertEquals("DataSourcePropertiesConfiguration valid field size was wrong", 0,
dataSourcePropertiesConfiguration.getValidField().size());
assertNull("DataSourcePropertiesConfiguration valid properties was not null",
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
NacosDataSourceProperties nacosDataSourceProperties = buildNacosProperties();
dataSourcePropertiesConfiguration.setNacos(nacosDataSourceProperties);
assertEquals(
"DataSourcePropertiesConfiguration valid field size was wrong after set nacos attribute",
1, dataSourcePropertiesConfiguration.getValidField().size());
assertNotNull(
"DataSourcePropertiesConfiguration nacos properties was null after set nacos attribute",
dataSourcePropertiesConfiguration.getNacos());
assertNotNull(
"DataSourcePropertiesConfiguration valid properties was null after set nacos attribute",
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
}
@Test
public void testZKAttr() {
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration();
assertEquals("DataSourcePropertiesConfiguration valid field size was wrong", 0,
dataSourcePropertiesConfiguration.getValidField().size());
assertNull("DataSourcePropertiesConfiguration valid properties was not null",
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
ZookeeperDataSourceProperties zookeeperDataSourceProperties = buildZKProperties();
dataSourcePropertiesConfiguration.setZk(zookeeperDataSourceProperties);
assertEquals(
"DataSourcePropertiesConfiguration valid field size was wrong after set zk attribute",
1, dataSourcePropertiesConfiguration.getValidField().size());
assertNotNull(
"DataSourcePropertiesConfiguration zk properties was null after set zk attribute",
dataSourcePropertiesConfiguration.getZk());
assertNotNull(
"DataSourcePropertiesConfiguration valid properties was null after set zk attribute",
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
}
@Test
public void testApolloAttr() {
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration();
assertEquals("DataSourcePropertiesConfiguration valid field size was wrong", 0,
dataSourcePropertiesConfiguration.getValidField().size());
assertNull("DataSourcePropertiesConfiguration valid properties was not null",
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
ApolloDataSourceProperties apolloDataSourceProperties = buildApolloProperties();
dataSourcePropertiesConfiguration.setApollo(apolloDataSourceProperties);
assertEquals(
"DataSourcePropertiesConfiguration valid field size was wrong after set apollo attribute",
1, dataSourcePropertiesConfiguration.getValidField().size());
assertNotNull(
"DataSourcePropertiesConfiguration apollo properties was null after set apollo attribute",
dataSourcePropertiesConfiguration.getApollo());
assertNotNull(
"DataSourcePropertiesConfiguration valid properties was null after set apollo attribute",
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
}
@Test
public void testMultiAttr() {
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration();
assertEquals("DataSourcePropertiesConfiguration valid field size was wrong", 0,
dataSourcePropertiesConfiguration.getValidField().size());
assertNull("DataSourcePropertiesConfiguration valid properties was not null",
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
FileDataSourceProperties fileDataSourceProperties = buildFileProperties();
NacosDataSourceProperties nacosDataSourceProperties = buildNacosProperties();
dataSourcePropertiesConfiguration.setFile(fileDataSourceProperties);
dataSourcePropertiesConfiguration.setNacos(nacosDataSourceProperties);
assertEquals(
"DataSourcePropertiesConfiguration valid field size was wrong after set file and nacos attribute",
2, dataSourcePropertiesConfiguration.getValidField().size());
assertNull(
"DataSourcePropertiesConfiguration valid properties was not null after set file and nacos attribute",
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
}
@Test
public void testFileConstructor() {
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration(
buildFileProperties());
assertEquals(
"DataSourcePropertiesConfiguration file constructor valid field size was wrong",
1, dataSourcePropertiesConfiguration.getValidField().size());
assertNotNull(
"DataSourcePropertiesConfiguration file constructor valid properties was null",
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
}
@Test
public void testNacosConstructor() {
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration(
buildNacosProperties());
assertEquals(
"DataSourcePropertiesConfiguration nacos constructor valid field size was wrong",
1, dataSourcePropertiesConfiguration.getValidField().size());
assertNotNull(
"DataSourcePropertiesConfiguration nacos constructor valid properties was null",
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
}
@Test
public void testApolloConstructor() {
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration(
buildApolloProperties());
assertEquals(
"DataSourcePropertiesConfiguration apollo constructor valid field size was wrong",
1, dataSourcePropertiesConfiguration.getValidField().size());
assertNotNull(
"DataSourcePropertiesConfiguration apollo constructor valid properties was null",
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
}
@Test
public void testZKConstructor() {
DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration(
buildZKProperties());
assertEquals(
"DataSourcePropertiesConfiguration zk constructor valid field size was wrong",
1, dataSourcePropertiesConfiguration.getValidField().size());
assertNotNull(
"DataSourcePropertiesConfiguration zk constructor valid properties was null",
dataSourcePropertiesConfiguration.getValidDataSourceProperties());
}
private FileDataSourceProperties buildFileProperties() {
FileDataSourceProperties fileDataSourceProperties = new FileDataSourceProperties();
fileDataSourceProperties.setFile("/tmp/test.json");
fileDataSourceProperties.setBufSize(1024);
fileDataSourceProperties.setRecommendRefreshMs(2000);
return fileDataSourceProperties;
}
private NacosDataSourceProperties buildNacosProperties() {
NacosDataSourceProperties nacosDataSourceProperties = new NacosDataSourceProperties();
nacosDataSourceProperties.setServerAddr("127.0.0.1:8848");
nacosDataSourceProperties.setDataId("sentinel");
nacosDataSourceProperties.setGroupId("custom-group");
return nacosDataSourceProperties;
}
private ApolloDataSourceProperties buildApolloProperties() {
ApolloDataSourceProperties apolloDataSourceProperties = new ApolloDataSourceProperties();
apolloDataSourceProperties.setFlowRulesKey("test-key");
apolloDataSourceProperties.setDefaultFlowRuleValue("dft-val");
apolloDataSourceProperties.setNamespaceName("namespace");
return apolloDataSourceProperties;
}
private ZookeeperDataSourceProperties buildZKProperties() {
ZookeeperDataSourceProperties zookeeperDataSourceProperties = new ZookeeperDataSourceProperties();
zookeeperDataSourceProperties.setServerAddr("localhost:2181");
zookeeperDataSourceProperties.setPath("/path");
return zookeeperDataSourceProperties;
}
}
///*
// * 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.assertNotNull;
//import static org.junit.Assert.assertNull;
//
//import org.junit.Test;
//import org.springframework.cloud.alibaba.sentinel.datasource.config.ApolloDataSourceProperties;
//import org.springframework.cloud.alibaba.sentinel.datasource.config.DataSourcePropertiesConfiguration;
//import org.springframework.cloud.alibaba.sentinel.datasource.config.FileDataSourceProperties;
//import org.springframework.cloud.alibaba.sentinel.datasource.config.NacosDataSourceProperties;
//import org.springframework.cloud.alibaba.sentinel.datasource.config.ZookeeperDataSourceProperties;
//
///**
// * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
// */
//public class DataSourcePropertiesConfigurationTests {
//
// @Test
// public void testFileAttr() {
// DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration();
// assertEquals("DataSourcePropertiesConfiguration valid field size was wrong", 0,
// dataSourcePropertiesConfiguration.getValidField().size());
// assertNull("DataSourcePropertiesConfiguration valid properties was not null",
// dataSourcePropertiesConfiguration.getValidDataSourceProperties());
//
// FileDataSourceProperties fileDataSourceProperties = buildFileProperties();
//
// dataSourcePropertiesConfiguration.setFile(fileDataSourceProperties);
//
// assertEquals(
// "DataSourcePropertiesConfiguration valid field size was wrong after set file attribute",
// 1, dataSourcePropertiesConfiguration.getValidField().size());
// assertNotNull(
// "DataSourcePropertiesConfiguration file properties was null after set file attribute",
// dataSourcePropertiesConfiguration.getFile());
// assertNotNull(
// "DataSourcePropertiesConfiguration valid properties was null after set file attribute",
// dataSourcePropertiesConfiguration.getValidDataSourceProperties());
// }
//
// @Test
// public void testNacosAttr() {
// DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration();
// assertEquals("DataSourcePropertiesConfiguration valid field size was wrong", 0,
// dataSourcePropertiesConfiguration.getValidField().size());
// assertNull("DataSourcePropertiesConfiguration valid properties was not null",
// dataSourcePropertiesConfiguration.getValidDataSourceProperties());
//
// NacosDataSourceProperties nacosDataSourceProperties = buildNacosProperties();
//
// dataSourcePropertiesConfiguration.setNacos(nacosDataSourceProperties);
//
// assertEquals(
// "DataSourcePropertiesConfiguration valid field size was wrong after set nacos attribute",
// 1, dataSourcePropertiesConfiguration.getValidField().size());
// assertNotNull(
// "DataSourcePropertiesConfiguration nacos properties was null after set nacos attribute",
// dataSourcePropertiesConfiguration.getNacos());
// assertNotNull(
// "DataSourcePropertiesConfiguration valid properties was null after set nacos attribute",
// dataSourcePropertiesConfiguration.getValidDataSourceProperties());
// }
//
// @Test
// public void testZKAttr() {
// DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration();
// assertEquals("DataSourcePropertiesConfiguration valid field size was wrong", 0,
// dataSourcePropertiesConfiguration.getValidField().size());
// assertNull("DataSourcePropertiesConfiguration valid properties was not null",
// dataSourcePropertiesConfiguration.getValidDataSourceProperties());
//
// ZookeeperDataSourceProperties zookeeperDataSourceProperties = buildZKProperties();
//
// dataSourcePropertiesConfiguration.setZk(zookeeperDataSourceProperties);
//
// assertEquals(
// "DataSourcePropertiesConfiguration valid field size was wrong after set zk attribute",
// 1, dataSourcePropertiesConfiguration.getValidField().size());
// assertNotNull(
// "DataSourcePropertiesConfiguration zk properties was null after set zk attribute",
// dataSourcePropertiesConfiguration.getZk());
// assertNotNull(
// "DataSourcePropertiesConfiguration valid properties was null after set zk attribute",
// dataSourcePropertiesConfiguration.getValidDataSourceProperties());
// }
//
// @Test
// public void testApolloAttr() {
// DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration();
// assertEquals("DataSourcePropertiesConfiguration valid field size was wrong", 0,
// dataSourcePropertiesConfiguration.getValidField().size());
// assertNull("DataSourcePropertiesConfiguration valid properties was not null",
// dataSourcePropertiesConfiguration.getValidDataSourceProperties());
//
// ApolloDataSourceProperties apolloDataSourceProperties = buildApolloProperties();
//
// dataSourcePropertiesConfiguration.setApollo(apolloDataSourceProperties);
//
// assertEquals(
// "DataSourcePropertiesConfiguration valid field size was wrong after set apollo attribute",
// 1, dataSourcePropertiesConfiguration.getValidField().size());
// assertNotNull(
// "DataSourcePropertiesConfiguration apollo properties was null after set apollo attribute",
// dataSourcePropertiesConfiguration.getApollo());
// assertNotNull(
// "DataSourcePropertiesConfiguration valid properties was null after set apollo attribute",
// dataSourcePropertiesConfiguration.getValidDataSourceProperties());
// }
//
// @Test
// public void testMultiAttr() {
// DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration();
// assertEquals("DataSourcePropertiesConfiguration valid field size was wrong", 0,
// dataSourcePropertiesConfiguration.getValidField().size());
// assertNull("DataSourcePropertiesConfiguration valid properties was not null",
// dataSourcePropertiesConfiguration.getValidDataSourceProperties());
//
// FileDataSourceProperties fileDataSourceProperties = buildFileProperties();
// NacosDataSourceProperties nacosDataSourceProperties = buildNacosProperties();
//
// dataSourcePropertiesConfiguration.setFile(fileDataSourceProperties);
// dataSourcePropertiesConfiguration.setNacos(nacosDataSourceProperties);
//
// assertEquals(
// "DataSourcePropertiesConfiguration valid field size was wrong after set file and nacos attribute",
// 2, dataSourcePropertiesConfiguration.getValidField().size());
// assertNull(
// "DataSourcePropertiesConfiguration valid properties was not null after set file and nacos attribute",
// dataSourcePropertiesConfiguration.getValidDataSourceProperties());
// }
//
// @Test
// public void testFileConstructor() {
// DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration(
// buildFileProperties());
// assertEquals(
// "DataSourcePropertiesConfiguration file constructor valid field size was wrong",
// 1, dataSourcePropertiesConfiguration.getValidField().size());
// assertNotNull(
// "DataSourcePropertiesConfiguration file constructor valid properties was null",
// dataSourcePropertiesConfiguration.getValidDataSourceProperties());
// }
//
// @Test
// public void testNacosConstructor() {
// DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration(
// buildNacosProperties());
// assertEquals(
// "DataSourcePropertiesConfiguration nacos constructor valid field size was wrong",
// 1, dataSourcePropertiesConfiguration.getValidField().size());
// assertNotNull(
// "DataSourcePropertiesConfiguration nacos constructor valid properties was null",
// dataSourcePropertiesConfiguration.getValidDataSourceProperties());
// }
//
// @Test
// public void testApolloConstructor() {
// DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration(
// buildApolloProperties());
// assertEquals(
// "DataSourcePropertiesConfiguration apollo constructor valid field size was wrong",
// 1, dataSourcePropertiesConfiguration.getValidField().size());
// assertNotNull(
// "DataSourcePropertiesConfiguration apollo constructor valid properties was null",
// dataSourcePropertiesConfiguration.getValidDataSourceProperties());
// }
//
// @Test
// public void testZKConstructor() {
// DataSourcePropertiesConfiguration dataSourcePropertiesConfiguration = new DataSourcePropertiesConfiguration(
// buildZKProperties());
// assertEquals(
// "DataSourcePropertiesConfiguration zk constructor valid field size was wrong",
// 1, dataSourcePropertiesConfiguration.getValidField().size());
// assertNotNull(
// "DataSourcePropertiesConfiguration zk constructor valid properties was null",
// dataSourcePropertiesConfiguration.getValidDataSourceProperties());
// }
//
// private FileDataSourceProperties buildFileProperties() {
// FileDataSourceProperties fileDataSourceProperties = new FileDataSourceProperties();
//
// fileDataSourceProperties.setFile("/tmp/test.json");
// fileDataSourceProperties.setBufSize(1024);
// fileDataSourceProperties.setRecommendRefreshMs(2000);
// return fileDataSourceProperties;
// }
//
// private NacosDataSourceProperties buildNacosProperties() {
// NacosDataSourceProperties nacosDataSourceProperties = new NacosDataSourceProperties();
// nacosDataSourceProperties.setServerAddr("127.0.0.1:8848");
// nacosDataSourceProperties.setDataId("sentinel");
// nacosDataSourceProperties.setGroupId("custom-group");
// return nacosDataSourceProperties;
// }
//
// private ApolloDataSourceProperties buildApolloProperties() {
// ApolloDataSourceProperties apolloDataSourceProperties = new ApolloDataSourceProperties();
// apolloDataSourceProperties.setFlowRulesKey("test-key");
// apolloDataSourceProperties.setDefaultFlowRuleValue("dft-val");
// apolloDataSourceProperties.setNamespaceName("namespace");
// return apolloDataSourceProperties;
// }
//
// private ZookeeperDataSourceProperties buildZKProperties() {
// ZookeeperDataSourceProperties zookeeperDataSourceProperties = new ZookeeperDataSourceProperties();
//
// zookeeperDataSourceProperties.setServerAddr("localhost:2181");
// zookeeperDataSourceProperties.setPath("/path");
// return zookeeperDataSourceProperties;
// }
//
//}