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:
parent
530a36fc06
commit
14d11b7517
@ -15,6 +15,7 @@ import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
|
|||||||
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
|
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract class Using by {@link DataSourcePropertiesConfiguration}
|
* Abstract class Using by {@link DataSourcePropertiesConfiguration}
|
||||||
@ -30,6 +31,8 @@ public class AbstractDataSourceProperties {
|
|||||||
private String converterClass;
|
private String converterClass;
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
private final String factoryBeanName;
|
private final String factoryBeanName;
|
||||||
|
@JsonIgnore
|
||||||
|
private Environment env;
|
||||||
|
|
||||||
public AbstractDataSourceProperties(String factoryBeanName) {
|
public AbstractDataSourceProperties(String factoryBeanName) {
|
||||||
this.factoryBeanName = factoryBeanName;
|
this.factoryBeanName = factoryBeanName;
|
||||||
@ -63,6 +66,14 @@ public class AbstractDataSourceProperties {
|
|||||||
return factoryBeanName;
|
return factoryBeanName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Environment getEnv() {
|
||||||
|
return env;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnv(Environment env) {
|
||||||
|
this.env = env;
|
||||||
|
}
|
||||||
|
|
||||||
public void preCheck(String dataSourceName) {
|
public void preCheck(String dataSourceName) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,6 @@ package org.springframework.cloud.alibaba.sentinel.datasource.config;
|
|||||||
|
|
||||||
import javax.validation.constraints.NotEmpty;
|
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.cloud.alibaba.sentinel.datasource.factorybean.NacosDataSourceFactoryBean;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
@ -34,9 +32,13 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void preCheck(String dataSourceName) {
|
public void preCheck(String dataSourceName) {
|
||||||
if (StringUtils.isEmpty(serverAddr) && acmPropertiesInvalid()) {
|
if (StringUtils.isEmpty(serverAddr)) {
|
||||||
throw new IllegalArgumentException(
|
serverAddr = this.getEnv().getProperty(
|
||||||
"NacosDataSource properties value not correct. serverAddr is empty but there is empty value in accessKey, secretKey, endpoint, namespace property");
|
"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;
|
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;
|
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;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Zookeeper Properties class Using by {@link DataSourcePropertiesConfiguration} and
|
* Zookeeper Properties class Using by {@link DataSourcePropertiesConfiguration} and
|
||||||
@ -16,7 +15,6 @@ 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;
|
||||||
@ -25,6 +23,18 @@ public class ZookeeperDataSourceProperties extends AbstractDataSourceProperties
|
|||||||
|
|
||||||
private String dataId;
|
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() {
|
public String getServerAddr() {
|
||||||
return serverAddr;
|
return serverAddr;
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@ import org.springframework.cloud.alibaba.sentinel.datasource.converter.XmlConver
|
|||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
|
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
|
||||||
@ -170,8 +171,8 @@ public class SentinelAutoConfiguration {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SentinelDataSourceHandler sentinelDataSourceHandler(
|
public SentinelDataSourceHandler sentinelDataSourceHandler(
|
||||||
DefaultListableBeanFactory beanFactory) {
|
DefaultListableBeanFactory beanFactory, SentinelProperties sentinelProperties, Environment env) {
|
||||||
return new SentinelDataSourceHandler(beanFactory);
|
return new SentinelDataSourceHandler(beanFactory, sentinelProperties, env);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ConditionalOnClass(ObjectMapper.class)
|
@ConditionalOnClass(ObjectMapper.class)
|
||||||
|
@ -12,13 +12,13 @@ import java.util.Set;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.SmartInitializingSingleton;
|
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.BeanDefinitionBuilder;
|
||||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||||
import org.springframework.cloud.alibaba.sentinel.SentinelProperties;
|
import org.springframework.cloud.alibaba.sentinel.SentinelProperties;
|
||||||
import org.springframework.cloud.alibaba.sentinel.datasource.config.AbstractDataSourceProperties;
|
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.JsonConverter;
|
||||||
import org.springframework.cloud.alibaba.sentinel.datasource.converter.XmlConverter;
|
import org.springframework.cloud.alibaba.sentinel.datasource.converter.XmlConverter;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
@ -48,12 +48,16 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
|
|||||||
|
|
||||||
private final DefaultListableBeanFactory beanFactory;
|
private final DefaultListableBeanFactory beanFactory;
|
||||||
|
|
||||||
public SentinelDataSourceHandler(DefaultListableBeanFactory beanFactory) {
|
private final SentinelProperties sentinelProperties;
|
||||||
this.beanFactory = beanFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Autowired
|
private final Environment env;
|
||||||
private SentinelProperties sentinelProperties;
|
|
||||||
|
public SentinelDataSourceHandler(DefaultListableBeanFactory beanFactory,
|
||||||
|
SentinelProperties sentinelProperties, Environment env) {
|
||||||
|
this.beanFactory = beanFactory;
|
||||||
|
this.sentinelProperties = sentinelProperties;
|
||||||
|
this.env = env;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterSingletonsInstantiated() {
|
public void afterSingletonsInstantiated() {
|
||||||
@ -69,6 +73,7 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
|
|||||||
}
|
}
|
||||||
AbstractDataSourceProperties abstractDataSourceProperties = dataSourceProperties
|
AbstractDataSourceProperties abstractDataSourceProperties = dataSourceProperties
|
||||||
.getValidDataSourceProperties();
|
.getValidDataSourceProperties();
|
||||||
|
abstractDataSourceProperties.setEnv(env);
|
||||||
abstractDataSourceProperties.preCheck(dataSourceName);
|
abstractDataSourceProperties.preCheck(dataSourceName);
|
||||||
registerBean(abstractDataSourceProperties, dataSourceName
|
registerBean(abstractDataSourceProperties, dataSourceName
|
||||||
+ "-sentinel-" + validFields.get(0) + "-datasource");
|
+ "-sentinel-" + validFields.get(0) + "-datasource");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user