代码
import cn.hutool.core.util.StrUtil;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
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;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.zookeeper.ZookeeperProperties;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
@Component
public class ZkDataSourceConfig {
@Value("${spring.application.name}")
private String appname;
@Value("${spring.profiles.active}")
private String profile;
@Resource
private ZookeeperProperties zookeeperProperties;
@PostConstruct
public void init() {
loadRules(zookeeperProperties.getConnectString());
}
private void loadRules(String remoteAddress) {
String groupId = StrUtil.format("sentinel/{}:{}", appname, profile);
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, "FlowRule",
source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
ReadableDataSource<String, List<DegradeRule>> degradeRuleDataSource = new ZookeeperDataSource<>(remoteAddress, groupId, "DegradeRule",
source -> JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {
}));
DegradeRuleManager.register2Property(degradeRuleDataSource.getProperty());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
ZK配置参考
/sentinel/应用名:环境=FlowRule=[{"resource": "资源1", "limitApp": "default", "grade": 1, "count": 40.0, "strategy": 0, "controlBehavior": 2, "maxQueueingTimeMs": 100000 }, {"resource": "资源2", "limitApp": "default", "grade": 1, "count": 40.0, "strategy": 0, "controlBehavior": 2, "maxQueueingTimeMs": 100000 }, {"resource": "资源3", "limitApp": "default", "grade": 1, "count": 10.0, "strategy": 0, "controlBehavior": 2, "maxQueueingTimeMs": 100000 } ]
使用
public class demo {
public static void main(String[] args) {
initFlowRules();
while (true) {
try (Entry entry = SphU.entry("myResource")) {
System.out.println("业务资源访问成功!");
} catch (BlockException ex) {
System.out.println("资源访问失败!!!");
} finally {
if (entry != null) {
entry.exit();
}
}
}
while (true) {
if(SphO.entry("myResource")){
try{
System.out.println("业务资源访问成功!");
} catch (BlockException ex) {
System.out.println("资源访问失败!!!");
}finally{
SphO.exit();
}
}
}
while(true){
getUserById("111");
}
}
private static void initFlowRules(){
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("myResource");
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(10);
rule.setLimitApp("default");
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
@SentinelResource(value="myResource",blockHandler="handlerException")
public User getUserById(String id){
return new User("数据库用户");
}
public User handlerException(String id,BlockException exception){
return new User("流控用户");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
Entry entry = null;
try {
entry = SphU.entry("myResource");
...
} catch (BlockException e1) {
...
} finally {
if (entry != null) {
entry.exit();
}
}