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

add jsr 303 validation for SentinelProperties

This commit is contained in:
fangjian0423 2019-01-04 12:32:05 +08:00
parent 98993ba705
commit 1db45e85c4
7 changed files with 32 additions and 0 deletions

View File

@ -83,6 +83,13 @@
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>

View File

@ -1,6 +1,10 @@
package org.springframework.cloud.alibaba.sentinel.datasource.config; package org.springframework.cloud.alibaba.sentinel.datasource.config;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import org.springframework.cloud.alibaba.sentinel.datasource.RuleType; import org.springframework.cloud.alibaba.sentinel.datasource.RuleType;
import org.springframework.util.Assert;
import com.alibaba.csp.sentinel.datasource.AbstractDataSource; import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager; import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
@ -18,7 +22,9 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
*/ */
public class AbstractDataSourceProperties { public class AbstractDataSourceProperties {
@NotEmpty
private String dataType = "json"; private String dataType = "json";
@NotNull
private RuleType ruleType; private RuleType ruleType;
private String converterClass; private String converterClass;
@JsonIgnore @JsonIgnore
@ -61,6 +67,7 @@ public class AbstractDataSourceProperties {
} }
public void preCheck(String dataSourceName) { public void preCheck(String dataSourceName) {
} }
public void postRegister(AbstractDataSource dataSource) { public void postRegister(AbstractDataSource dataSource) {

View File

@ -1,5 +1,7 @@
package org.springframework.cloud.alibaba.sentinel.datasource.config; package org.springframework.cloud.alibaba.sentinel.datasource.config;
import javax.validation.constraints.NotEmpty;
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.ApolloDataSourceFactoryBean; import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.ApolloDataSourceFactoryBean;
/** /**
@ -10,7 +12,9 @@ import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.ApolloD
*/ */
public class ApolloDataSourceProperties extends AbstractDataSourceProperties { public class ApolloDataSourceProperties extends AbstractDataSourceProperties {
@NotEmpty
private String namespaceName; private String namespaceName;
@NotEmpty
private String flowRulesKey; private String flowRulesKey;
private String defaultFlowRuleValue; private String defaultFlowRuleValue;

View File

@ -2,6 +2,8 @@ package org.springframework.cloud.alibaba.sentinel.datasource.config;
import java.io.IOException; import java.io.IOException;
import javax.validation.constraints.NotEmpty;
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.FileRefreshableDataSourceFactoryBean; import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.FileRefreshableDataSourceFactoryBean;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -14,6 +16,7 @@ import org.springframework.util.StringUtils;
*/ */
public class FileDataSourceProperties extends AbstractDataSourceProperties { public class FileDataSourceProperties extends AbstractDataSourceProperties {
@NotEmpty
private String file; private String file;
private String charset = "utf-8"; private String charset = "utf-8";
private long recommendRefreshMs = 3000L; private long recommendRefreshMs = 3000L;

View File

@ -1,5 +1,7 @@
package org.springframework.cloud.alibaba.sentinel.datasource.config; 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.RuleType;
import org.springframework.cloud.alibaba.sentinel.datasource.SentinelDataSourceConstants; import org.springframework.cloud.alibaba.sentinel.datasource.SentinelDataSourceConstants;
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.NacosDataSourceFactoryBean; import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.NacosDataSourceFactoryBean;
@ -15,7 +17,11 @@ import org.springframework.util.StringUtils;
public class NacosDataSourceProperties extends AbstractDataSourceProperties { public class NacosDataSourceProperties extends AbstractDataSourceProperties {
private String serverAddr; private String serverAddr;
@NotEmpty
private String groupId; private String groupId;
@NotEmpty
private String dataId; private String dataId;
// commercialized usage // commercialized usage

View File

@ -1,5 +1,7 @@
package org.springframework.cloud.alibaba.sentinel.datasource.config; 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.cloud.alibaba.sentinel.datasource.factorybean.ZookeeperDataSourceFactoryBean;
/** /**
@ -14,6 +16,7 @@ public class ZookeeperDataSourceProperties extends AbstractDataSourceProperties
super(ZookeeperDataSourceFactoryBean.class.getName()); super(ZookeeperDataSourceFactoryBean.class.getName());
} }
@NotEmpty
private String serverAddr; private String serverAddr;
private String path; private String path;

View File

@ -24,6 +24,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty; import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.cloud.alibaba.sentinel.datasource.config.DataSourcePropertiesConfiguration; import org.springframework.cloud.alibaba.sentinel.datasource.config.DataSourcePropertiesConfiguration;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.validation.annotation.Validated;
import com.alibaba.csp.sentinel.config.SentinelConfig; import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.log.LogBase; import com.alibaba.csp.sentinel.log.LogBase;
@ -36,6 +37,7 @@ import com.alibaba.csp.sentinel.transport.config.TransportConfig;
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a> * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/ */
@ConfigurationProperties(prefix = SentinelConstants.PROPERTY_PREFIX) @ConfigurationProperties(prefix = SentinelConstants.PROPERTY_PREFIX)
@Validated
public class SentinelProperties { public class SentinelProperties {
/** /**