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

using Enum replace of String

This commit is contained in:
fangjian0423 2018-12-28 14:43:56 +08:00
parent 0e75da3b8d
commit 6806cd77ac
3 changed files with 8 additions and 14 deletions

View File

@ -19,7 +19,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
public class AbstractDataSourceProperties { public class AbstractDataSourceProperties {
private String dataType = "json"; private String dataType = "json";
private String ruleType; private RuleType ruleType;
private String converterClass; private String converterClass;
@JsonIgnore @JsonIgnore
protected String factoryBeanName; protected String factoryBeanName;
@ -36,11 +36,11 @@ public class AbstractDataSourceProperties {
this.dataType = dataType; this.dataType = dataType;
} }
public String getRuleType() { public RuleType getRuleType() {
return ruleType; return ruleType;
} }
public void setRuleType(String ruleType) { public void setRuleType(RuleType ruleType) {
this.ruleType = ruleType; this.ruleType = ruleType;
} }
@ -61,15 +61,10 @@ public class AbstractDataSourceProperties {
} }
public void preCheck(String dataSourceName) { public void preCheck(String dataSourceName) {
if (!RuleType.getByName(this.getRuleType()).isPresent()) {
throw new IllegalArgumentException(
"[Sentinel Starter] DataSource " + dataSourceName
+ " get error ruleType [" + this.getRuleType() + "]");
}
} }
public void postRegister(AbstractDataSource dataSource) { public void postRegister(AbstractDataSource dataSource) {
switch (RuleType.getByName(this.getRuleType()).get()) { switch (this.getRuleType()) {
case FLOW: case FLOW:
FlowRuleManager.register2Property(dataSource.getProperty()); FlowRuleManager.register2Property(dataSource.getProperty());
break; break;

View File

@ -128,10 +128,10 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties {
.getProperty(SentinelDataSourceConstants.PROJECT_NAME) + "-" + type); .getProperty(SentinelDataSourceConstants.PROJECT_NAME) + "-" + type);
result.setGroupId("nacos-sentinel"); result.setGroupId("nacos-sentinel");
if (type.equals(RuleType.FLOW.getName())) { if (type.equals(RuleType.FLOW.getName())) {
result.setRuleType(RuleType.FLOW.getName()); result.setRuleType(RuleType.FLOW);
} }
else { else {
result.setRuleType(RuleType.DEGRADE.getName()); result.setRuleType(RuleType.DEGRADE);
} }
return result; return result;
} }

View File

@ -18,7 +18,6 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.cloud.alibaba.sentinel.SentinelConstants; import org.springframework.cloud.alibaba.sentinel.SentinelConstants;
import org.springframework.cloud.alibaba.sentinel.SentinelProperties; import org.springframework.cloud.alibaba.sentinel.SentinelProperties;
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.config.AbstractDataSourceProperties; import org.springframework.cloud.alibaba.sentinel.datasource.config.AbstractDataSourceProperties;
import org.springframework.cloud.alibaba.sentinel.datasource.config.DataSourcePropertiesConfiguration; import org.springframework.cloud.alibaba.sentinel.datasource.config.DataSourcePropertiesConfiguration;
@ -199,7 +198,7 @@ public class SentinelDataSourceHandler {
// 'sentinel-{converterType}-{ruleType}-converter' // 'sentinel-{converterType}-{ruleType}-converter'
builder.addPropertyReference("converter", builder.addPropertyReference("converter",
"sentinel-" + propertyValue.toString() + "-" "sentinel-" + propertyValue.toString() + "-"
+ dataSourceProperties.getRuleType() + dataSourceProperties.getRuleType().getName()
+ "-converter"); + "-converter");
} }
} }
@ -220,7 +219,7 @@ public class SentinelDataSourceHandler {
.getBean(dataSourceName); .getBean(dataSourceName);
logAndCheckRuleType(newDataSource, dataSourceName, logAndCheckRuleType(newDataSource, dataSourceName,
RuleType.getByName(dataSourceProperties.getRuleType()).get().getClazz()); dataSourceProperties.getRuleType().getClazz());
// register property in RuleManager // register property in RuleManager
dataSourceProperties.postRegister(newDataSource); dataSourceProperties.postRegister(newDataSource);