|
|
@@ -3,6 +3,7 @@ package com.alibaba.csp.sentinel.datasource.zookeeper; |
|
|
|
import com.alibaba.csp.sentinel.datasource.Converter; |
|
|
|
import com.alibaba.csp.sentinel.datasource.ReadableDataSource; |
|
|
|
import com.alibaba.csp.sentinel.slots.block.RuleConstant; |
|
|
|
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; |
|
|
|
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; |
|
|
|
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
@@ -18,6 +19,7 @@ import org.apache.zookeeper.data.ACL; |
|
|
|
import org.apache.zookeeper.data.Id; |
|
|
|
import org.apache.zookeeper.data.Stat; |
|
|
|
import org.apache.zookeeper.server.auth.DigestAuthenticationProvider; |
|
|
|
import org.junit.Assert; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
@@ -43,16 +45,17 @@ public class ZookeeperDataSourceTest { |
|
|
|
final String path = "/sentinel-zk-ds-demo/flow-HK"; |
|
|
|
|
|
|
|
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<List<FlowRule>>(remoteAddress, path, |
|
|
|
new Converter<String, List<FlowRule>>() { |
|
|
|
@Override |
|
|
|
public List<FlowRule> convert(String source) { |
|
|
|
return JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}); |
|
|
|
} |
|
|
|
}); |
|
|
|
new Converter<String, List<FlowRule>>() { |
|
|
|
@Override |
|
|
|
public List<FlowRule> convert(String source) { |
|
|
|
return JSON.parseObject(source, new TypeReference<List<FlowRule>>() { |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
FlowRuleManager.register2Property(flowRuleDataSource.getProperty()); |
|
|
|
|
|
|
|
CuratorFramework zkClient = CuratorFrameworkFactory.newClient(remoteAddress, |
|
|
|
new ExponentialBackoffRetry(3, 1000)); |
|
|
|
new ExponentialBackoffRetry(3, 1000)); |
|
|
|
zkClient.start(); |
|
|
|
Stat stat = zkClient.checkExists().forPath(path); |
|
|
|
if (stat == null) { |
|
|
@@ -67,6 +70,9 @@ public class ZookeeperDataSourceTest { |
|
|
|
server.stop(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
public void testZooKeeperDataSourceAuthorization() throws Exception { |
|
|
|
TestingServer server = new TestingServer(21812); |
|
|
@@ -116,21 +122,21 @@ public class ZookeeperDataSourceTest { |
|
|
|
|
|
|
|
private void publishThenTestFor(CuratorFramework zkClient, String path, String resourceName, long count) throws Exception { |
|
|
|
FlowRule rule = new FlowRule().setResource(resourceName) |
|
|
|
.setLimitApp("default") |
|
|
|
.as(FlowRule.class) |
|
|
|
.setCount(count) |
|
|
|
.setGrade(RuleConstant.FLOW_GRADE_QPS); |
|
|
|
.setLimitApp("default") |
|
|
|
.as(FlowRule.class) |
|
|
|
.setCount(count) |
|
|
|
.setGrade(RuleConstant.FLOW_GRADE_QPS); |
|
|
|
String ruleString = JSON.toJSONString(Collections.singletonList(rule)); |
|
|
|
zkClient.setData().forPath(path, ruleString.getBytes()); |
|
|
|
|
|
|
|
await().timeout(5, TimeUnit.SECONDS) |
|
|
|
.until(new Callable<Boolean>() { |
|
|
|
@Override |
|
|
|
public Boolean call() throws Exception { |
|
|
|
List<FlowRule> rules = FlowRuleManager.getRules(); |
|
|
|
return rules != null && !rules.isEmpty(); |
|
|
|
} |
|
|
|
}); |
|
|
|
.until(new Callable<Boolean>() { |
|
|
|
@Override |
|
|
|
public Boolean call() throws Exception { |
|
|
|
List<FlowRule> rules = FlowRuleManager.getRules(); |
|
|
|
return rules != null && !rules.isEmpty(); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
List<FlowRule> rules = FlowRuleManager.getRules(); |
|
|
|
boolean exists = false; |
|
|
@@ -142,4 +148,77 @@ public class ZookeeperDataSourceTest { |
|
|
|
} |
|
|
|
assertTrue(exists); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Test whether different dataSources can share the same zkClient when the connection parameters are the same. |
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
@Test |
|
|
|
public void testZooKeeperDataSourceSameZkClient() throws Exception { |
|
|
|
TestingServer server = new TestingServer(21813); |
|
|
|
server.start(); |
|
|
|
|
|
|
|
final String remoteAddress = server.getConnectString(); |
|
|
|
final String flowPath = "/sentinel-zk-ds-demo/flow-HK"; |
|
|
|
final String degradePath = "/sentinel-zk-ds-demo/degrade-HK"; |
|
|
|
|
|
|
|
|
|
|
|
ZookeeperDataSource<List<FlowRule>> flowRuleZkDataSource = new ZookeeperDataSource<>(remoteAddress, flowPath, |
|
|
|
new Converter<String, List<FlowRule>>() { |
|
|
|
@Override |
|
|
|
public List<FlowRule> convert(String source) { |
|
|
|
return JSON.parseObject(source, new TypeReference<List<FlowRule>>() { |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
ZookeeperDataSource<List<DegradeRule>> degradeRuleZkDataSource = new ZookeeperDataSource<>(remoteAddress, degradePath, |
|
|
|
new Converter<String, List<DegradeRule>>() { |
|
|
|
@Override |
|
|
|
public List<DegradeRule> convert(String source) { |
|
|
|
return JSON.parseObject(source, new TypeReference<List<DegradeRule>>() { |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
Assert.assertTrue(flowRuleZkDataSource.getZkClient() == degradeRuleZkDataSource.getZkClient()); |
|
|
|
|
|
|
|
|
|
|
|
final String groupId = "sentinel-zk-ds-demo"; |
|
|
|
final String flowDataId = "flow-HK"; |
|
|
|
final String degradeDataId = "degrade-HK"; |
|
|
|
final String scheme = "digest"; |
|
|
|
final String auth = "root:123456"; |
|
|
|
AuthInfo authInfo = new AuthInfo(scheme, auth.getBytes()); |
|
|
|
List<AuthInfo> authInfoList = Collections.singletonList(authInfo); |
|
|
|
|
|
|
|
|
|
|
|
ZookeeperDataSource<List<FlowRule>> flowRuleZkAutoDataSource = new ZookeeperDataSource<List<FlowRule>>(remoteAddress, |
|
|
|
authInfoList, groupId, flowDataId, |
|
|
|
new Converter<String, List<FlowRule>>() { |
|
|
|
@Override |
|
|
|
public List<FlowRule> convert(String source) { |
|
|
|
return JSON.parseObject(source, new TypeReference<List<FlowRule>>() { |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
ZookeeperDataSource<List<DegradeRule>> degradeRuleZkAutoDataSource = new ZookeeperDataSource<List<DegradeRule>>(remoteAddress, |
|
|
|
authInfoList, groupId, degradeDataId, |
|
|
|
new Converter<String, List<DegradeRule>>() { |
|
|
|
@Override |
|
|
|
public List<DegradeRule> convert(String source) { |
|
|
|
return JSON.parseObject(source, new TypeReference<List<DegradeRule>>() { |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
Assert.assertTrue(flowRuleZkAutoDataSource.getZkClient() == degradeRuleZkAutoDataSource.getZkClient()); |
|
|
|
|
|
|
|
server.stop(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |