Describe what happened (or what feature you want)
找了很多地方,没有找到关联流量控制的Demo,这里补充下。
这里先需要说明一下,关联流量控制生效的时间点为:B>threashold的时候A就会被限流。
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import java.util.Arrays;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
/**
* <br/>
* 钉钉:1xd_zqcim4jtj5
*
* @author westonlv
* @since 2019/2/21 11:05
*/
public class FlowQpsRelateDemo {
static String node_read = "read";
static String node_write = "write";
public static void main(String[] args) {
FlowRule rule = new FlowRule();
rule.setResource(node_read);
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setStrategy(RuleConstant.STRATEGY_RELATE);
rule.setCount(10);
rule.setRefResource(node_write);
FlowRuleManager.loadRules(Arrays.asList(rule));
CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
Thread thread1 = new Thread(() -> {
try {
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
for (int i = 0; i < 20; i++) {
ContextUtil.enter("read");
Entry entry = null;
try {
entry = SphU.entry(node_read);
Thread.sleep(100);
System.out.println("read pass..");
} catch (BlockException e) {
System.out.println("block read:" + (i - 10));
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (null != entry)
entry.exit();
ContextUtil.exit();
}
}
System.out.println("read over..");
});
thread1.start();
Thread thread2 = new Thread(() -> {
try {
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
for (int i = 0; i < 20; i++) {
ContextUtil.enter("write");
Entry entry = null;
try {
entry = SphU.entry(node_write);
Thread.sleep(1);
System.out.println("write pass..");
} catch (BlockException e) {
System.out.println("block write:" + (i - 10));
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (null != entry)
entry.exit();
ContextUtil.exit();
}
}
System.out.println("write over..");
});
thread2.start();
}
}
===========================下方为测试结果============================
可以控制read资源和write资源sleep的时间产生不同的结果
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write over..
read pass..
block read:-9
block read:-8
block read:-7
block read:-6
block read:-5
block read:-4
block read:-3
block read:-2
block read:-1
block read:0
block read:1
block read:2
block read:3
block read:4
block read:5
block read:6
block read:7
block read:8
block read:9
read over..
Describe what happened (or what feature you want)
找了很多地方,没有找到关联流量控制的Demo,这里补充下。
这里先需要说明一下,关联流量控制生效的时间点为:B>threashold的时候A就会被限流。
===========================下方为测试结果============================
可以控制read资源和write资源sleep的时间产生不同的结果