From b22fcb444fd1b415b2be313420803944b974e3ad Mon Sep 17 00:00:00 2001 From: musi Date: Sat, 4 Feb 2023 16:14:30 +0800 Subject: [PATCH 1/5] feat: Add support for sentinel opensergo datasource --- .../pom.xml | 10 ++ .../DataSourcePropertiesConfiguration.java | 10 ++ .../config/OpenSergoDataSourceProperties.java | 125 ++++++++++++++++++ .../OpenSergoDataSourceFactoryBean.java | 106 +++++++++++++++ .../META-INF/sentinel-datasource.properties | 1 + .../custom/SentinelDataSourceHandler.java | 27 +++- 6 files changed, 274 insertions(+), 5 deletions(-) create mode 100644 spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/OpenSergoDataSourceProperties.java create mode 100644 spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/factorybean/OpenSergoDataSourceFactoryBean.java 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..ac0e43329a 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 + + 2.0.0-SNAPSHOT + + 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..0f387bca0c --- /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,125 @@ +/* + * 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.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 org.springframework.util.CollectionUtils; + +/** + * OpenSergo Properties class Using by {@link DataSourcePropertiesConfiguration} and + * {@link OpenSergoDataSourceFactoryBean}. + * + * @author musi + * @author + */ +public class OpenSergoDataSourceProperties extends AbstractDataSourceProperties { + + private String host = "127.0.0.1"; + + private int port = 10246; + + private String namespace = "default"; + + private String app; + + private Set enabledRules; + + public OpenSergoDataSourceProperties() { + super(OpenSergoDataSourceFactoryBean.class.getName()); + } + + public void postRegister(OpenSergoDataSourceGroup dataSourceGroup) { + // TODO: SystemRule and ParamFlowRule + if (enabledRules.contains(OpenSergoSentinelConstants.KIND_PARAM_FLOW_RULE)) { + FlowRuleManager.register2Property(dataSourceGroup.subscribeFlowRules()); + } + if (enabledRules.contains(OpenSergoSentinelConstants.KIND_CIRCUIT_BREAKER_RULE)) { + 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; + } + } + + @Override + public void preCheck(String dataSourceName) { + if (StringUtils.isEmpty(app)) { + throw new IllegalArgumentException("OpenSergoDataSource app is empty"); + } + if (CollectionUtils.isEmpty(enabledRules)) { + throw new IllegalArgumentException( + "OpenSergoDataSource enabled-rules is empty"); + } + } + + 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); + } + } } } From db4d4910b1dce96d53cb9e3f7ac1d75e1b87f469 Mon Sep 17 00:00:00 2001 From: musi Date: Mon, 6 Feb 2023 13:50:12 +0800 Subject: [PATCH 2/5] fix: Change OpenSergo DataSource version to 0.1.0-beta --- .../spring-cloud-alibaba-sentinel-datasource/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ac0e43329a..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 @@ -15,7 +15,7 @@ Spring Cloud Alibaba Sentinel DataSource - 2.0.0-SNAPSHOT + 0.1.0-beta From 5ceef78171ce50813dde4c06653dba7a84fa9ad8 Mon Sep 17 00:00:00 2001 From: musi Date: Mon, 6 Feb 2023 14:17:57 +0800 Subject: [PATCH 3/5] fix: Use AppNameUtil to get app name --- .../config/OpenSergoDataSourceProperties.java | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) 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 index 0f387bca0c..2dffd517c5 100644 --- 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 @@ -25,6 +25,7 @@ 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; @@ -43,7 +44,7 @@ public class OpenSergoDataSourceProperties extends AbstractDataSourceProperties private String namespace = "default"; - private String app; + private String app = AppNameUtil.getAppName(); private Set enabledRules; @@ -71,17 +72,6 @@ public void postRegister(OpenSergoDataSourceGroup dataSourceGroup) { } } - @Override - public void preCheck(String dataSourceName) { - if (StringUtils.isEmpty(app)) { - throw new IllegalArgumentException("OpenSergoDataSource app is empty"); - } - if (CollectionUtils.isEmpty(enabledRules)) { - throw new IllegalArgumentException( - "OpenSergoDataSource enabled-rules is empty"); - } - } - public String getHost() { return host; } From 5c4593fc8000c71fb6af50ef7ae0ff63792b7c4c Mon Sep 17 00:00:00 2001 From: musi Date: Mon, 6 Feb 2023 14:19:14 +0800 Subject: [PATCH 4/5] fix: Add default value for enabledRules --- .../datasource/config/OpenSergoDataSourceProperties.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index 2dffd517c5..90c4aae678 100644 --- 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 @@ -16,6 +16,7 @@ package com.alibaba.cloud.sentinel.datasource.config; +import java.util.HashSet; import java.util.Set; import com.alibaba.cloud.commons.lang.StringUtils; @@ -46,7 +47,7 @@ public class OpenSergoDataSourceProperties extends AbstractDataSourceProperties private String app = AppNameUtil.getAppName(); - private Set enabledRules; + private Set enabledRules = new HashSet<>(); public OpenSergoDataSourceProperties() { super(OpenSergoDataSourceFactoryBean.class.getName()); From ed194d52c4fb4364cf20b2693d19f16c28993c21 Mon Sep 17 00:00:00 2001 From: musi Date: Mon, 6 Feb 2023 16:48:14 +0800 Subject: [PATCH 5/5] fix: Unify flow rule --- .../src/main/resources/application.properties | 1 + .../datasource/config/OpenSergoDataSourceProperties.java | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) 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/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 index 90c4aae678..98b3b96099 100644 --- 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 @@ -38,7 +38,8 @@ * @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; @@ -55,10 +56,10 @@ public OpenSergoDataSourceProperties() { public void postRegister(OpenSergoDataSourceGroup dataSourceGroup) { // TODO: SystemRule and ParamFlowRule - if (enabledRules.contains(OpenSergoSentinelConstants.KIND_PARAM_FLOW_RULE)) { + if (enabledRules.contains(FLOW)) { FlowRuleManager.register2Property(dataSourceGroup.subscribeFlowRules()); } - if (enabledRules.contains(OpenSergoSentinelConstants.KIND_CIRCUIT_BREAKER_RULE)) { + if (enabledRules.contains(DEGRADE)) { DegradeRuleManager.register2Property(dataSourceGroup.subscribeDegradeRules()); } // When there is no enabled-rules, try ruleType