From 7b06f44ee427c96af7ff9da18b4044ea42925405 Mon Sep 17 00:00:00 2001 From: mengjin Date: Thu, 2 Apr 2020 17:12:15 +0800 Subject: [PATCH] :sparkles: Introducing new features. Sentinel Support ConsulDataSource Dynamic Rule Configuration --- spring-cloud-alibaba-dependencies/pom.xml | 6 ++ .../pom.xml | 6 ++ .../config/ConsulDataSourceProperties.java | 98 +++++++++++++++++++ .../DataSourcePropertiesConfiguration.java | 15 +++ .../config/RedisDataSourceProperties.java | 2 +- .../ConsulDataSourceFactoryBean.java | 95 ++++++++++++++++++ .../META-INF/sentinel-datasource.properties | 1 + 7 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/ConsulDataSourceProperties.java create mode 100644 spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/factorybean/ConsulDataSourceFactoryBean.java diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 5c2b55ed8..ac38a4023 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -93,6 +93,12 @@ ${sentinel.version} + + com.alibaba.csp + sentinel-datasource-consul + ${sentinel.version} + + com.alibaba.csp sentinel-web-servlet 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 794dde469..a45ed77b2 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 @@ -83,6 +83,12 @@ true + + com.alibaba.csp + sentinel-datasource-consul + true + + 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/ConsulDataSourceProperties.java b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/ConsulDataSourceProperties.java new file mode 100644 index 000000000..98f064f55 --- /dev/null +++ b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/ConsulDataSourceProperties.java @@ -0,0 +1,98 @@ +/* + * 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 com.alibaba.cloud.sentinel.datasource.factorybean.ConsulDataSourceFactoryBean; +import org.springframework.util.StringUtils; + +/** + * Consul Properties class Using by {@link DataSourcePropertiesConfiguration} and + * {@link ConsulDataSourceFactoryBean}. + * + * @author mengjin + */ +public class ConsulDataSourceProperties extends AbstractDataSourceProperties { + + public ConsulDataSourceProperties(){ + super(ConsulDataSourceFactoryBean.class.getName()); + } + + /** + * consul server host + */ + private String host; + + /** + * consul server port + */ + private int port=8500; + + /** + * data key in Redis. + */ + private String ruleKey; + + /** + * Request of query will hang until timeout (in second) or get updated value. + */ + private int waitTimeoutInSecond = 1; + + @Override + public void preCheck(String dataSourceName) { + super.preCheck(dataSourceName); + if(StringUtils.isEmpty(host)){ + throw new IllegalArgumentException( + "ConsulDataSource server-host is empty"); + } + if(StringUtils.isEmpty(ruleKey)){ + throw new IllegalArgumentException( + "ConsulDataSource ruleKey can not be 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 getRuleKey() { + return ruleKey; + } + + public void setRuleKey(String ruleKey) { + this.ruleKey = ruleKey; + } + + public int getWaitTimeoutInSecond() { + return waitTimeoutInSecond; + } + + public void setWaitTimeoutInSecond(int waitTimeoutInSecond) { + this.waitTimeoutInSecond = waitTimeoutInSecond; + } +} 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 2c4f1d7b0..a0a5526f3 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 @@ -34,6 +34,7 @@ import org.springframework.util.ObjectUtils; * @see ZookeeperDataSourceProperties * @see FileDataSourceProperties * @see RedisDataSourceProperties + * @see ConsulDataSourceProperties */ public class DataSourcePropertiesConfiguration { @@ -47,9 +48,23 @@ public class DataSourcePropertiesConfiguration { private RedisDataSourceProperties redis; + private ConsulDataSourceProperties consul; + public DataSourcePropertiesConfiguration() { } + public DataSourcePropertiesConfiguration(ConsulDataSourceProperties consul) { + this.consul = consul; + } + + public ConsulDataSourceProperties getConsul() { + return consul; + } + + public void setConsul(ConsulDataSourceProperties consul) { + this.consul = consul; + } + public DataSourcePropertiesConfiguration(FileDataSourceProperties file) { this.file = file; } diff --git a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/RedisDataSourceProperties.java b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/RedisDataSourceProperties.java index 984b6e2cd..a5049660b 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/RedisDataSourceProperties.java +++ b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/RedisDataSourceProperties.java @@ -24,7 +24,7 @@ import com.alibaba.cloud.sentinel.datasource.factorybean.RedisDataSourceFactoryB import org.springframework.util.StringUtils; /** - * Zookeeper Properties class Using by {@link DataSourcePropertiesConfiguration} and + * Redis Properties class Using by {@link DataSourcePropertiesConfiguration} and * {@link RedisDataSourceFactoryBean}. * * @author lengleng diff --git a/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/factorybean/ConsulDataSourceFactoryBean.java b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/factorybean/ConsulDataSourceFactoryBean.java new file mode 100644 index 000000000..ad132357f --- /dev/null +++ b/spring-cloud-alibaba-starters/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/factorybean/ConsulDataSourceFactoryBean.java @@ -0,0 +1,95 @@ +/* + * 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 com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.datasource.consul.ConsulDataSource; +import org.springframework.beans.factory.FactoryBean; + +/** + * A {@link FactoryBean} for creating {@link ConsulDataSource} instance. + * + * @author mengjin + * @see ConsulDataSource + */ +public class ConsulDataSourceFactoryBean implements FactoryBean { + + private String host; + + private int port; + + private String ruleKey; + + private int waitTimeoutInSecond; + + private Converter converter; + + @Override + public ConsulDataSource getObject() throws Exception { + return new ConsulDataSource( + host, + port, + ruleKey, + waitTimeoutInSecond, + converter); + } + + @Override + public Class getObjectType() { + return ConsulDataSource.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 getRuleKey() { + return ruleKey; + } + + public void setRuleKey(String ruleKey) { + this.ruleKey = ruleKey; + } + + public int getWaitTimeoutInSecond() { + return waitTimeoutInSecond; + } + + public void setWaitTimeoutInSecond(int waitTimeoutInSecond) { + this.waitTimeoutInSecond = waitTimeoutInSecond; + } + + 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 074751fbd..db4df37f7 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 @@ -3,3 +3,4 @@ file =com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource 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 \ No newline at end of file