diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties index e9c0859369..eb6fcdc887 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties @@ -38,3 +38,4 @@ spring.cloud.sentinel.datasource.ds4.file.rule-type=system spring.cloud.sentinel.datasource.ds5.file.file=classpath: param-flow.json spring.cloud.sentinel.datasource.ds5.file.rule-type=param_flow +spring.cloud.sentinel.datasource.ds5.opensergo.rule-type=param_flow diff --git a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/pom.xml b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/pom.xml index eb43b98edd..75c60afb61 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/pom.xml +++ b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/pom.xml @@ -14,6 +14,10 @@ spring-cloud-alibaba-sentinel-datasource Spring Cloud Alibaba Sentinel DataSource + + 0.1.0-beta + + com.alibaba.cloud @@ -81,6 +85,12 @@ true + + com.alibaba.csp + sentinel-datasource-opensergo + ${opensergo-datasource.version} + + com.fasterxml.jackson.core jackson-databind diff --git a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/DataSourcePropertiesConfiguration.java b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/DataSourcePropertiesConfiguration.java index d72d15ab7a..c465d35234 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/DataSourcePropertiesConfiguration.java +++ b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/DataSourcePropertiesConfiguration.java @@ -50,6 +50,8 @@ public class DataSourcePropertiesConfiguration { private ConsulDataSourceProperties consul; + private OpenSergoDataSourceProperties opensergo; + public DataSourcePropertiesConfiguration() { } @@ -125,6 +127,14 @@ public void setRedis(RedisDataSourceProperties redis) { this.redis = redis; } + public OpenSergoDataSourceProperties getOpensergo() { + return opensergo; + } + + public void setOpensergo(OpenSergoDataSourceProperties opensergo) { + this.opensergo = opensergo; + } + @JsonIgnore public List getValidField() { return Arrays.stream(this.getClass().getDeclaredFields()) diff --git a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/OpenSergoDataSourceProperties.java b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/OpenSergoDataSourceProperties.java new file mode 100644 index 0000000000..98b3b96099 --- /dev/null +++ b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/OpenSergoDataSourceProperties.java @@ -0,0 +1,117 @@ +/* + * Copyright 2013-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.cloud.sentinel.datasource.config; + +import java.util.HashSet; +import java.util.Set; + +import com.alibaba.cloud.commons.lang.StringUtils; +import com.alibaba.cloud.sentinel.datasource.RuleType; +import com.alibaba.cloud.sentinel.datasource.factorybean.OpenSergoDataSourceFactoryBean; +import com.alibaba.csp.sentinel.datasource.OpenSergoDataSourceGroup; +import com.alibaba.csp.sentinel.datasource.OpenSergoSentinelConstants; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; +import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; +import com.alibaba.csp.sentinel.util.AppNameUtil; + +import org.springframework.util.CollectionUtils; + +/** + * OpenSergo Properties class Using by {@link DataSourcePropertiesConfiguration} and + * {@link OpenSergoDataSourceFactoryBean}. + * + * @author musi + * @author + */ +public class OpenSergoDataSourceProperties extends AbstractDataSourceProperties { + private static final String FLOW = "flow"; + private static final String DEGRADE = "degrade"; + private String host = "127.0.0.1"; + + private int port = 10246; + + private String namespace = "default"; + + private String app = AppNameUtil.getAppName(); + + private Set enabledRules = new HashSet<>(); + + public OpenSergoDataSourceProperties() { + super(OpenSergoDataSourceFactoryBean.class.getName()); + } + + public void postRegister(OpenSergoDataSourceGroup dataSourceGroup) { + // TODO: SystemRule and ParamFlowRule + if (enabledRules.contains(FLOW)) { + FlowRuleManager.register2Property(dataSourceGroup.subscribeFlowRules()); + } + if (enabledRules.contains(DEGRADE)) { + DegradeRuleManager.register2Property(dataSourceGroup.subscribeDegradeRules()); + } + // When there is no enabled-rules, try ruleType + RuleType ruleType = getRuleType(); + switch (ruleType) { + case FLOW: + FlowRuleManager.register2Property(dataSourceGroup.subscribeFlowRules()); + break; + case DEGRADE: + DegradeRuleManager.register2Property(dataSourceGroup.subscribeDegradeRules()); + break; + } + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + public String getApp() { + return app; + } + + public void setApp(String app) { + this.app = app; + } + + public Set getEnabledRules() { + return enabledRules; + } + + public void setEnabledRules(Set enabledRules) { + this.enabledRules = enabledRules; + } + +} diff --git a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/factorybean/OpenSergoDataSourceFactoryBean.java b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/factorybean/OpenSergoDataSourceFactoryBean.java new file mode 100644 index 0000000000..19609e0f0b --- /dev/null +++ b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/factorybean/OpenSergoDataSourceFactoryBean.java @@ -0,0 +1,106 @@ +/* + * Copyright 2013-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.cloud.sentinel.datasource.factorybean; + +import java.util.Set; + +import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.datasource.OpenSergoDataSourceGroup; + +import org.springframework.beans.factory.FactoryBean; + +/** + * A {@link FactoryBean} for creating {@link OpenSergoDataSourceGroup} instance. + * + * @author musi + * @author + * @see OpenSergoDataSourceGroup + */ +public class OpenSergoDataSourceFactoryBean + implements FactoryBean { + + private String host; + + private int port; + + private String namespace; + + private String app; + + private Set enabledRules; + + private Converter converter; + + @Override + public OpenSergoDataSourceGroup getObject() throws Exception { + return new OpenSergoDataSourceGroup(host, port, namespace, app); + } + + @Override + public Class getObjectType() { + return OpenSergoDataSourceGroup.class; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + public String getApp() { + return app; + } + + public void setApp(String app) { + this.app = app; + } + + public Set getEnabledRules() { + return enabledRules; + } + + public void setEnabledRules(Set enabledRules) { + this.enabledRules = enabledRules; + } + + public Converter getConverter() { + return converter; + } + + public void setConverter(Converter converter) { + this.converter = converter; + } + +} diff --git a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/resources/META-INF/sentinel-datasource.properties b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/resources/META-INF/sentinel-datasource.properties index e67a3b9b58..c4a1da0381 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/resources/META-INF/sentinel-datasource.properties +++ b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/resources/META-INF/sentinel-datasource.properties @@ -4,3 +4,4 @@ apollo = com.alibaba.csp.sentinel.datasource.apollo.ApolloDataSource zk = com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource redis = com.alibaba.csp.sentinel.datasource.redis.RedisDataSource consul = com.alibaba.csp.sentinel.datasource.consul.ConsulDataSource +opensergo = com.alibaba.csp.sentinel.datasource.OpenSergoDataSourceGroup diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/custom/SentinelDataSourceHandler.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/custom/SentinelDataSourceHandler.java index 517a0d6f6b..1652da0173 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/custom/SentinelDataSourceHandler.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/custom/SentinelDataSourceHandler.java @@ -25,9 +25,11 @@ import com.alibaba.cloud.sentinel.SentinelProperties; import com.alibaba.cloud.sentinel.datasource.config.AbstractDataSourceProperties; +import com.alibaba.cloud.sentinel.datasource.config.OpenSergoDataSourceProperties; import com.alibaba.cloud.sentinel.datasource.converter.JsonConverter; import com.alibaba.cloud.sentinel.datasource.converter.XmlConverter; import com.alibaba.csp.sentinel.datasource.AbstractDataSource; +import com.alibaba.csp.sentinel.datasource.OpenSergoDataSourceGroup; import com.alibaba.csp.sentinel.datasource.ReadableDataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -206,11 +208,26 @@ private void registerBean(final AbstractDataSourceProperties dataSourcePropertie this.beanFactory.registerBeanDefinition(dataSourceName, builder.getBeanDefinition()); // init in Spring - AbstractDataSource newDataSource = (AbstractDataSource) this.beanFactory - .getBean(dataSourceName); - - // register property in RuleManager - dataSourceProperties.postRegister(newDataSource); + Object newDataSource = this.beanFactory.getBean(dataSourceName); + if (newDataSource instanceof AbstractDataSource) { + // register property in RuleManager + dataSourceProperties.postRegister((AbstractDataSource) newDataSource); + } + if (newDataSource instanceof OpenSergoDataSourceGroup) { + // Properties must be OpenSergoDataSourceProperties + if (!(dataSourceProperties instanceof OpenSergoDataSourceProperties)) { + return; + } + OpenSergoDataSourceProperties openSergoDataSourceProperties = (OpenSergoDataSourceProperties) dataSourceProperties; + try { + OpenSergoDataSourceGroup dataSourceGroup = (OpenSergoDataSourceGroup) newDataSource; + dataSourceGroup.start(); + openSergoDataSourceProperties.postRegister(dataSourceGroup); + } + catch (Exception e) { + log.error("Error on register to OpenSergo data source", e); + } + } } }