-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Support Etcd datasource #1018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Support Etcd datasource #1018
Changes from 35 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
11dba5e
com.alibaba.csp.sentinel.node.StatisticNode#curThreadNum
362f326
reformat the code according to Alibaba Java Coding Guideline
50d2a5a
reformat the code according to Alibaba Java Coding Guideline
509ecf2
Merge pull request #1 from alibaba/master
linlinisme 01bda6b
Remove the one redundant space....
ce717e9
fix issue 62 : Support customized log directory and configuration pro…
b8e66b8
use System.getProperty to get the property
5c6600d
Merge pull request #2 from alibaba/master
linlinisme 9999154
merge master latest code
8f9339b
using SentinelConfigLocator to load sentinel-core config, LogConfigLo…
2f485b4
Merge pull request #3 from alibaba/master
linlinisme c6dd24d
merge remote master
810d40a
fix typo
3f98b62
add null judge
23244e9
Reorganize the code
bd8a481
Reorganize the code
e6bbf79
add config test cases
c91b963
ConfigUtil.loadPropertiesFromFile when file not exist return null
d401c2f
rename classname and method name
85173a5
rename classname and method name
4510234
using File.separator to replace "/"
c068edd
support retrieve properties from classpath file
9af06c9
support retrieve properties from classpath file
014cda3
add feature support : retrieve properties from relative file
a899dc6
reformat code
419d97f
Merge pull request #4 from alibaba/master
linlinisme 80f283e
Merge remote-tracking branch 'remotes/origin/master' into 20190531-fi…
14a1d5b
Revert "rename classname and method name"
ea18020
add copy right doc
c562474
reset commit
c71759f
remove unused code
d387b47
merge alibaba master
56d5df0
Merge pull request #6 from alibaba/master
linlinisme 22cafd2
Merge pull request #7 from alibaba/master
linlinisme 29519e9
Sentinel DataSource Etcd provides integration with Etcd
55d0b08
etcd source demo add linked url
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>sentinel-demo</artifactId> | ||
<groupId>com.alibaba.csp</groupId> | ||
<version>1.7.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>sentinel-demo-etcd-datasource</artifactId> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-core</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-datasource-etcd</artifactId> | ||
<version>1.7.0-SNAPSHOT</version> | ||
</dependency> | ||
|
||
|
||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>fastjson</artifactId> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>${maven.compiler.version}</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
<encoding>${java.encoding}</encoding> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
58 changes: 58 additions & 0 deletions
58
...asource/src/main/java/com/alibaba/csp/sentinel/demo/datasource/etcd/EtcdConfigSender.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* 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 | ||
* | ||
* http://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.csp.sentinel.demo.datasource.etcd; | ||
|
||
|
||
import io.etcd.jetcd.ByteSequence; | ||
import io.etcd.jetcd.Client; | ||
|
||
/** | ||
* Etcd config sender for demo. | ||
* | ||
* @author lianglin | ||
* @since 1.7.0 | ||
*/ | ||
public class EtcdConfigSender { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
|
||
|
||
String rule_key = "sentinel_demo_rule_key"; | ||
|
||
Client client = Client.builder() | ||
.endpoints("http://127.0.0.1:2379") | ||
.user(ByteSequence.from("root".getBytes())) | ||
.password(ByteSequence.from("12345".getBytes())) | ||
.build(); | ||
final String rule = "[\n" | ||
+ " {\n" | ||
+ " \"resource\": \"TestResource\",\n" | ||
+ " \"controlBehavior\": 0,\n" | ||
+ " \"count\": 5.0,\n" | ||
+ " \"grade\": 1,\n" | ||
+ " \"limitApp\": \"default\",\n" | ||
+ " \"strategy\": 0\n" | ||
+ " }\n" | ||
+ "]"; | ||
client.getKVClient() | ||
.put(ByteSequence.from(rule_key.getBytes()), ByteSequence.from(rule.getBytes())); | ||
|
||
System.out.println("setting rule success"); | ||
Thread.sleep(10000); | ||
|
||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...ource/src/main/java/com/alibaba/csp/sentinel/demo/datasource/etcd/EtcdDataSourceDemo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* 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 | ||
* | ||
* http://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.csp.sentinel.demo.datasource.etcd; | ||
|
||
import com.alibaba.csp.sentinel.config.SentinelConfig; | ||
import com.alibaba.csp.sentinel.datasource.ReadableDataSource; | ||
import com.alibaba.csp.sentinel.datasource.etcd.EtcdConfig; | ||
import com.alibaba.csp.sentinel.datasource.etcd.EtcdDataSource; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; | ||
import com.alibaba.fastjson.JSON; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author lianglin | ||
* @since 1.7.0 | ||
*/ | ||
public class EtcdDataSourceDemo { | ||
|
||
public static void main(String[] args) { | ||
|
||
String rule_key = "sentinel_demo_rule_key"; | ||
String yourUserName = "root"; | ||
String yourPassWord = "12345"; | ||
String endPoints = "http://127.0.0.1:2379"; | ||
SentinelConfig.setConfig(EtcdConfig.END_POINTS, endPoints); | ||
SentinelConfig.setConfig(EtcdConfig.USER, yourUserName); | ||
SentinelConfig.setConfig(EtcdConfig.PASSWORD, yourPassWord); | ||
SentinelConfig.setConfig(EtcdConfig.CHARSET, "utf-8"); | ||
SentinelConfig.setConfig(EtcdConfig.AUTH_ENABLE, "true"); | ||
|
||
ReadableDataSource<String, List<FlowRule>> flowRuleEtcdDataSource = new EtcdDataSource<>(rule_key, (rule) -> JSON.parseArray(rule, FlowRule.class)); | ||
FlowRuleManager.register2Property(flowRuleEtcdDataSource.getProperty()); | ||
List<FlowRule> rules = FlowRuleManager.getRules(); | ||
System.out.println(rules); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Sentinel DataSource Etcd | ||
|
||
Sentinel DataSource Etcd provides integration with Etcd so that Etcd | ||
can be the dynamic rule data source of Sentinel. The data source uses push model (watcher). | ||
|
||
To use Sentinel DataSource Etcd, you should add the following dependency: | ||
|
||
```xml | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-datasource-etcd</artifactId> | ||
<version>x.y.z</version> | ||
</dependency> | ||
``` | ||
Configure Etcd Connect Properties By Config File (for example sentinel.properties) | ||
|
||
``` | ||
csp.sentinel.etcd.end.points=http://ip1:port1,http://ip2:port2 | ||
csp.sentinel.etcd.user=your_user | ||
csp.sentinel.etcd.password=your_password | ||
csp.sentinel.etcd.charset=your_charset | ||
csp.sentinel.etcd.auth.enable=true //if ture open user/password or ssl check | ||
csp.sentinel.etcd.authority=authority //ssl | ||
``` | ||
or JVM args(Add -D prefix) | ||
|
||
|
||
Then you can create an `EtcdDataSource` and register to rule managers. | ||
For instance: | ||
|
||
```java | ||
//`rule_key` is the rule config key | ||
ReadableDataSource<String, List<FlowRule>> flowRuleEtcdDataSource = new EtcdDataSource<>(rule_key, (rule) -> JSON.parseArray(rule, FlowRule.class)); | ||
FlowRuleManager.register2Property(flowRuleEtcdDataSource.getProperty()); | ||
``` | ||
|
||
> Note: It needs to update JDK version to JDK8 | ||
|
||
|
||
We've also provided an example: [sentinel-demo-etcd-datasource]. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>sentinel-extension</artifactId> | ||
<groupId>com.alibaba.csp</groupId> | ||
<version>1.7.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>sentinel-datasource-etcd</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<jetcd.version>0.3.0</jetcd.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-datasource-extension</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.etcd</groupId> | ||
<artifactId>jetcd-core</artifactId> | ||
<version>${jetcd.version}</version> | ||
</dependency> | ||
|
||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>fastjson</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>${maven.compiler.source}</source> | ||
<target>${maven.compiler.target}</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
|
||
</project> |
74 changes: 74 additions & 0 deletions
74
...el-datasource-etcd/src/main/java/com/alibaba/csp/sentinel/datasource/etcd/EtcdConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* 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 | ||
* | ||
* http://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.csp.sentinel.datasource.etcd; | ||
|
||
import com.alibaba.csp.sentinel.config.SentinelConfig; | ||
import com.alibaba.csp.sentinel.util.StringUtil; | ||
|
||
/** | ||
* Configure Etcd Connect Properties | ||
* | ||
* @author lianglin | ||
* @since 1.7.0 | ||
*/ | ||
public final class EtcdConfig { | ||
|
||
|
||
public final static String END_POINTS = "csp.sentinel.etcd.end.points"; | ||
public final static String USER = "csp.sentinel.etcd.user"; | ||
public final static String PASSWORD = "csp.sentinel.etcd.password"; | ||
public final static String CHARSET = "csp.sentinel.etcd.charset"; | ||
public final static String AUTH_ENABLE = "csp.sentinel.etcd.auth.enable"; | ||
public final static String AUTHORITY = "csp.sentinel.etcd.authority"; | ||
|
||
private final static String ENABLED = "true"; | ||
|
||
|
||
public static String getEndPoints() { | ||
return SentinelConfig.getConfig(END_POINTS); | ||
} | ||
|
||
public static String getUser() { | ||
return SentinelConfig.getConfig(USER); | ||
} | ||
|
||
public static String getPassword() { | ||
return SentinelConfig.getConfig(PASSWORD); | ||
} | ||
|
||
public static String getCharset() { | ||
String etcdCharSet = SentinelConfig.getConfig(CHARSET); | ||
if (StringUtil.isNotBlank(etcdCharSet)) { | ||
return etcdCharSet; | ||
} | ||
return SentinelConfig.charset(); | ||
} | ||
|
||
public static boolean isAuthEnable() { | ||
if (ENABLED.equalsIgnoreCase(SentinelConfig.getConfig(AUTH_ENABLE))) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static String getAuthority() { | ||
return SentinelConfig.getConfig(AUTHORITY); | ||
} | ||
|
||
private EtcdConfig() { | ||
}; | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add the expected URL for the demo :)