mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
optimize the usage of sentinel datasource
This commit is contained in:
@@ -15,6 +15,7 @@ import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
|
||||
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
/**
|
||||
* Abstract class Using by {@link DataSourcePropertiesConfiguration}
|
||||
@@ -30,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;
|
||||
@@ -63,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) {
|
||||
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,8 +1,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
|
||||
@@ -16,7 +15,6 @@ public class ZookeeperDataSourceProperties extends AbstractDataSourceProperties
|
||||
super(ZookeeperDataSourceFactoryBean.class.getName());
|
||||
}
|
||||
|
||||
@NotEmpty
|
||||
private String serverAddr;
|
||||
|
||||
private String path;
|
||||
@@ -25,6 +23,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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user