forked from alibaba/Sentinel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ZooKeeper data source for Sentinel (alibaba#33)
- Loading branch information
Showing
8 changed files
with
425 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,73 @@ | ||
<?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>0.1.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>sentinel-demo-zookeeper-datasource</artifactId> | ||
|
||
<properties> | ||
<zookeeper.version>3.4.13</zookeeper.version> | ||
<curator.version>4.0.1</curator.version> | ||
<curator-test.version>2.12.0</curator-test.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-datasource-extension</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-datasource-zookeeper</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>fastjson</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.zookeeper</groupId> | ||
<artifactId>zookeeper</artifactId> | ||
<version>${zookeeper.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.curator</groupId> | ||
<artifactId>curator-test</artifactId> | ||
<version>${curator-test.version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.apache.zookeeper</groupId> | ||
<artifactId>zookeeper</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</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> |
75 changes: 75 additions & 0 deletions
75
...c/main/java/com/alibaba/csp/sentinel/demo/datasource/zookeeper/ZookeeperConfigSender.java
This file contains 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,75 @@ | ||
package com.alibaba.csp.sentinel.demo.datasource.zookeeper; | ||
|
||
import org.apache.curator.framework.CuratorFramework; | ||
import org.apache.curator.framework.CuratorFrameworkFactory; | ||
import org.apache.curator.retry.ExponentialBackoffRetry; | ||
import org.apache.curator.test.TestingServer; | ||
import org.apache.zookeeper.CreateMode; | ||
import org.apache.zookeeper.data.Stat; | ||
|
||
/** | ||
* Zookeeper config sender for demo | ||
* | ||
* @author guonanjun | ||
*/ | ||
public class ZookeeperConfigSender { | ||
|
||
private static final int RETRY_TIMES = 3; | ||
private static final int SLEEP_TIME = 1000; | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
// 启动Zookeeper服务 | ||
TestingServer server = new TestingServer(2181); | ||
|
||
final String remoteAddress = server.getConnectString(); | ||
final String groupId = "Sentinel-Demo"; | ||
final String dataId = "SYSTEM-CODE-DEMO-FLOW"; | ||
final String rule = "[\n" | ||
+ " {\n" | ||
+ " \"resource\": \"TestResource\",\n" | ||
+ " \"controlBehavior\": 0,\n" | ||
+ " \"count\": 10.0,\n" | ||
+ " \"grade\": 1,\n" | ||
+ " \"limitApp\": \"default\",\n" | ||
+ " \"strategy\": 0\n" | ||
+ " }\n" | ||
+ "]"; | ||
|
||
CuratorFramework zkClient = CuratorFrameworkFactory.newClient(remoteAddress, new ExponentialBackoffRetry(SLEEP_TIME, RETRY_TIMES)); | ||
zkClient.start(); | ||
String path = getPath(groupId, dataId); | ||
Stat stat = zkClient.checkExists().forPath(path); | ||
if (stat == null) { | ||
zkClient.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, null); | ||
} | ||
zkClient.setData().forPath(path, rule.getBytes()); | ||
// zkClient.delete().forPath(path); | ||
|
||
try { | ||
Thread.sleep(30000L); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
zkClient.close(); | ||
|
||
//停止zookeeper服务 | ||
server.stop(); | ||
} | ||
|
||
private static String getPath(String groupId, String dataId) { | ||
String path = ""; | ||
if (groupId.startsWith("/")) { | ||
path += groupId; | ||
} else { | ||
path += "/" + groupId; | ||
} | ||
if (dataId.startsWith("/")) { | ||
path += dataId; | ||
} else { | ||
path += "/" + dataId; | ||
} | ||
return path; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...main/java/com/alibaba/csp/sentinel/demo/datasource/zookeeper/ZookeeperDataSourceDemo.java
This file contains 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,65 @@ | ||
package com.alibaba.csp.sentinel.demo.datasource.zookeeper; | ||
|
||
import java.util.List; | ||
|
||
import com.alibaba.csp.sentinel.datasource.DataSource; | ||
import com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; | ||
import com.alibaba.fastjson.JSON; | ||
import com.alibaba.fastjson.TypeReference; | ||
|
||
/** | ||
* Zookeeper DataSource Demo | ||
* | ||
* @author guonanjun | ||
*/ | ||
public class ZookeeperDataSourceDemo { | ||
|
||
public static void main(String[] args) { | ||
// 使用zookeeper的场景 | ||
loadRules(); | ||
|
||
// 方便扩展的场景 | ||
//loadRules2(); | ||
} | ||
|
||
private static void loadRules() { | ||
|
||
final String remoteAddress = "127.0.0.1:2181"; | ||
final String path = "/Sentinel-Demo/SYSTEM-CODE-DEMO-FLOW"; | ||
|
||
DataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, path, | ||
source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})); | ||
FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); | ||
|
||
|
||
} | ||
|
||
private static void loadRules2() { | ||
|
||
final String remoteAddress = "127.0.0.1:2181"; | ||
// 引入groupId和dataId的概念,是为了方便和Nacos进行切换 | ||
final String groupId = "Sentinel-Demo"; | ||
final String flowDataId = "SYSTEM-CODE-DEMO-FLOW"; | ||
// final String degradeDataId = "SYSTEM-CODE-DEMO-DEGRADE"; | ||
// final String systemDataId = "SYSTEM-CODE-DEMO-SYSTEM"; | ||
|
||
|
||
// 规则会持久化到zk的/groupId/flowDataId节点 | ||
// groupId和和flowDataId可以用/开头也可以不用 | ||
// 建议不用以/开头,目的是为了如果从Zookeeper切换到Nacos的话,只需要改数据源类名就可以 | ||
DataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, flowDataId, | ||
source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})); | ||
FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); | ||
|
||
// DataSource<String, List<DegradeRule>> degradeRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, degradeDataId, | ||
// source -> JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {})); | ||
// DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty()); | ||
// | ||
// DataSource<String, List<SystemRule>> systemRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, systemDataId, | ||
// source -> JSON.parseObject(source, new TypeReference<List<SystemRule>>() {})); | ||
// SystemRuleManager.register2Property(systemRuleDataSource.getProperty()); | ||
|
||
} | ||
} |
This file contains 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 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,43 @@ | ||
<?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>0.1.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>sentinel-datasource-zookeeper</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<zookeeper.version>3.4.13</zookeeper.version> | ||
<curator.version>4.0.1</curator.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-datasource-extension</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.zookeeper</groupId> | ||
<artifactId>zookeeper</artifactId> | ||
<version>${zookeeper.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.curator</groupId> | ||
<artifactId>curator-recipes</artifactId> | ||
<version>${curator.version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.apache.zookeeper</groupId> | ||
<artifactId>zookeeper</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
</project> |
Oops, something went wrong.