package com.example.demo;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.TimeUnit;
@Slf4j
public class SentinelTest {
static {
SentinelUtils2.load();
}
public static void main(String[] args) throws Exception {
testQps();
}
private static void testQps() throws Exception {
while (true) {
Entry entry = null;
try {
TimeUnit.MILLISECONDS.sleep(40);
entry = SphU.entry("testQps");
log.info("testQps");
} catch (BlockException e1) {
log.info("block!");
} finally {
if (entry != null) {
entry.exit();
}
}
}
}
private static void testFail() throws Exception {
SentinelUtils.initDegradeRule("testFail");
Entry entry = null;
while (true) {
try {
TimeUnit.MILLISECONDS.sleep(40);
entry = SphU.entry("testFail");
new Thread(SentinelTest::mockRpcInvokeFail).start();
} catch (BlockException e) {
log.info("block");
} finally {
if (null != entry) {
entry.exit();
}
}
}
}
private static void mockRpcInvokeFail() {
long count = Thread.currentThread().getId() % 2;
if (count == 0) {
throw new RuntimeException("fail");
} else {
log.info("failMethod");
}
}
private static void testTimeFail() throws Exception {
long startTime = System.currentTimeMillis();
SentinelUtils.initDegradeRule("testTimeFail");
log.info("init runTime: {}", System.currentTimeMillis() - startTime);
Entry entry = null;
while (true) {
TimeUnit.MILLISECONDS.sleep(40);
try {
entry = SphU.entry("testTimeFail");
new Thread(SentinelTest::mockRpcTimeout).start();
} catch (BlockException e) {
log.info("block");
} finally {
if (null != entry) {
entry.exit();
}
}
}
}
private static void mockRpcTimeout() {
long count = Thread.currentThread().getId() % 4;
log.info("mockTimeFail");
try {
TimeUnit.SECONDS.sleep(count);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.example.demo;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
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 lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
public class SentinelUtils {
private static final int TIME_WINDOW = 5;
private static final Set<String> KEY_SET = ConcurrentHashMap.newKeySet();
public static void initDegradeRule(String key) {
if (KEY_SET.contains(key)) {
return;
}
synchronized (SentinelUtils.class) {
if (KEY_SET.contains(key)) {
return;
}
List<DegradeRule> rules = DegradeRuleManager.getRules();
DegradeRule degradeRuleTime = buildDegradeRule(key, RuleConstant.DEGRADE_GRADE_RT, 1000D);
rules.add(degradeRuleTime);
DegradeRule degradeRuleRadio = buildDegradeRule(key, RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO, 0.3);
rules.add(degradeRuleRadio);
List<FlowRule> flowRules = new ArrayList<>();
FlowRule flowRule = buildFlowRule(key);
flowRules.add(flowRule);
DegradeRuleManager.loadRules(rules);
FlowRuleManager.loadRules(flowRules);
KEY_SET.add(key);
}
}
private static FlowRule buildFlowRule(String key) {
FlowRule rule = new FlowRule();
rule.setResource(key);
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(20);
return rule;
}
private static DegradeRule buildDegradeRule(String key, int degradeGradeRt, Double timeCount) {
DegradeRule degradeRuleTime = new DegradeRule();
degradeRuleTime.setResource(key);
degradeRuleTime.setGrade(degradeGradeRt);
degradeRuleTime.setCount(timeCount);
degradeRuleTime.setTimeWindow(TIME_WINDOW);
return degradeRuleTime;
}
}