1
0
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:
fangjian0423 2019-05-06 17:27:33 +08:00
parent 530a36fc06
commit 14d11b7517
5 changed files with 45 additions and 21 deletions

View File

@ -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) {
}

View File

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

View File

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

View File

@ -37,6 +37,7 @@ import org.springframework.cloud.alibaba.sentinel.datasource.converter.XmlConver
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
@ -170,8 +171,8 @@ public class SentinelAutoConfiguration {
@Bean
public SentinelDataSourceHandler sentinelDataSourceHandler(
DefaultListableBeanFactory beanFactory) {
return new SentinelDataSourceHandler(beanFactory);
DefaultListableBeanFactory beanFactory, SentinelProperties sentinelProperties, Environment env) {
return new SentinelDataSourceHandler(beanFactory, sentinelProperties, env);
}
@ConditionalOnClass(ObjectMapper.class)

View File

@ -12,13 +12,13 @@ import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.SentinelProperties;
import org.springframework.cloud.alibaba.sentinel.datasource.config.AbstractDataSourceProperties;
import org.springframework.cloud.alibaba.sentinel.datasource.converter.JsonConverter;
import org.springframework.cloud.alibaba.sentinel.datasource.converter.XmlConverter;
import org.springframework.core.env.Environment;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
@ -48,12 +48,16 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
private final DefaultListableBeanFactory beanFactory;
public SentinelDataSourceHandler(DefaultListableBeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
private final SentinelProperties sentinelProperties;
@Autowired
private SentinelProperties sentinelProperties;
private final Environment env;
public SentinelDataSourceHandler(DefaultListableBeanFactory beanFactory,
SentinelProperties sentinelProperties, Environment env) {
this.beanFactory = beanFactory;
this.sentinelProperties = sentinelProperties;
this.env = env;
}
@Override
public void afterSingletonsInstantiated() {
@ -69,6 +73,7 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
}
AbstractDataSourceProperties abstractDataSourceProperties = dataSourceProperties
.getValidDataSourceProperties();
abstractDataSourceProperties.setEnv(env);
abstractDataSourceProperties.preCheck(dataSourceName);
registerBean(abstractDataSourceProperties, dataSourceName
+ "-sentinel-" + validFields.get(0) + "-datasource");