大家好,欢迎来到IT知识分享网。
sentinel 使用示例
官网:https://sentinelguard.io/zh-cn/docs/quick-start.html
github:https://github.com/alibaba/Sentinel/wiki/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8
相关类与接口
Sphu
public class SphU {
private static final Object[] OBJECTS0 = new Object[0];
private SphU() {
}
public static Entry entry(String name) throws BlockException {
public static Entry entry(String name, int batchCount) throws BlockException {
public static Entry entry(String name, EntryType trafficType) throws BlockException {
public static Entry entry(String name, EntryType trafficType, int batchCount) throws BlockException {
public static Entry entry(String name, EntryType trafficType, int batchCount, Object... args) throws BlockException {
public static Entry entry(String name, int resourceType, EntryType trafficType) throws BlockException {
public static Entry entry(String name, int resourceType, EntryType trafficType, Object[] args) throws BlockException {
public static Entry entry(Method method) throws BlockException {
public static Entry entry(Method method, int batchCount) throws BlockException {
public static Entry entry(Method method, EntryType trafficType) throws BlockException {
public static Entry entry(Method method, EntryType trafficType, int batchCount) throws BlockException {
public static Entry entry(Method method, EntryType trafficType, int batchCount, Object... args) throws BlockException {
public static Entry entryWithPriority(String name) throws BlockException {
public static Entry entryWithPriority(String name, EntryType trafficType) throws BlockException {
public static AsyncEntry asyncEntry(String name) throws BlockException {
public static AsyncEntry asyncEntry(String name, EntryType trafficType) throws BlockException {
public static AsyncEntry asyncEntry(String name, EntryType trafficType, int batchCount, Object... args) throws BlockException {
public static AsyncEntry asyncEntry(String name, int resourceType, EntryType trafficType) throws BlockException {
public static AsyncEntry asyncEntry(String name, int resourceType, EntryType trafficType, Object[] args) throws BlockException {
public static AsyncEntry asyncEntry(String name, int resourceType, EntryType trafficType, int batchCount, Object[] args) throws BlockException {
SphO
public class SphO {
private static final Object[] OBJECTS0 = new Object[0];
public SphO() {
}
public static boolean entry(String name) {
public static boolean entry(String name, int batchCount) {
public static boolean entry(String name, EntryType type) {
public static boolean entry(String name, EntryType type, int count) {
public static boolean entry(String name, EntryType trafficType, int batchCount, Object... args) {
public static boolean entry(Method method) {
public static boolean entry(Method method, int batchCount) {
public static boolean entry(Method method, EntryType type) {
public static boolean entry(Method method, EntryType type, int count) {
public static boolean entry(Method method, EntryType trafficType, int batchCount, Object... args) {
public static void exit(int count, Object... args) {
public static void exit(int count) {
public static void exit() {
使用示例
Test
public class Test {
public static void fun(int index){ //受保护的资源
System.out.println("fun方法执行:"+index);
}
public static void initFlowRule(){ //初始化限流规则
List<FlowRule> flowRules = new ArrayList<>();
FlowRule flowRule = new FlowRule();
flowRule.setResource("hello"); //定义受保护的资源名称,需与下方entry中名称一致
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
flowRule.setCount(20);
flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
flowRules.add(flowRule);
FlowRuleManager.loadRules(flowRules);
}
public static void main(String[] args) {
initFlowRule();
Entry entry;
for (int i=0;i<100;i++){
try {
entry = SphU.entry("hello"); //受保护的资源名称
fun(i); //调用受保护的方法
} catch (Exception e){
e.printStackTrace();
}finally {
if (entry != null){
entry.exit();
}
}
}
System.exit(0);
}
}
点击运行,控制台输出:
INFO: Sentinel log output type is: file
INFO: Sentinel log charset is: utf-8
INFO: Sentinel log base directory is: /Users/huli/logs/csp/
INFO: Sentinel log name use pid is: false
fun方法执行:0
fun方法执行:1
fun方法执行:2
fun方法执行:3
fun方法执行:4
fun方法执行:5
fun方法执行:6
fun方法执行:7
fun方法执行:8
fun方法执行:9
fun方法执行:10
fun方法执行:11
fun方法执行:12
fun方法执行:13
fun方法执行:14
fun方法执行:15
fun方法执行:16
fun方法执行:17
fun方法执行:18
fun方法执行:19
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
。。。
输出的日志
1648360038000|2022-03-27 13:47:18|hello|20|80|0|0|0|0|0|0
时间戳:1648360038000
对应日期时间:2022-03-27 13:47:18
hello:资源名称
20:通过(pass)的请求数
80:阻塞(block)的请求数
使用示例 2
Test2
public class Test2 {
public static void fun(int index){
System.out.println("fun方法执行:"+index);
}
public static void initFlowRule(){
List<FlowRule> flowRules = new ArrayList<>();
FlowRule flowRule = new FlowRule();
flowRule.setResource("hello2");
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
flowRule.setCount(20);
flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
flowRules.add(flowRule);
FlowRuleManager.loadRules(flowRules);
}
public static void main(String[] args) {
initFlowRule();
for (int i=0;i<100;i++){
if (SphO.entry("hello2")){
try {
fun(i);
}catch (Exception e){
System.out.println("业务异常,触发降级操作");
} finally{
SphO.exit(); //需与SphO.entry成对出现,否则会导致调用链记录异常,
//抛出ErrorEntryFreeException` 异常
}
}else {
System.out.println("服务限流,触发降级操作");
}
}
System.exit(0);
}
}
点击运行,控制台输出:
INFO: Sentinel log output type is: file
INFO: Sentinel log charset is: utf-8
INFO: Sentinel log base directory is: /Users/huli/logs/csp/
INFO: Sentinel log name use pid is: false
fun方法执行:0
fun方法执行:1
fun方法执行:2
fun方法执行:3
fun方法执行:4
fun方法执行:5
fun方法执行:6
fun方法执行:7
fun方法执行:8
fun方法执行:9
fun方法执行:10
fun方法执行:11
fun方法执行:12
fun方法执行:13
fun方法执行:14
fun方法执行:15
fun方法执行:16
fun方法执行:17
fun方法执行:18
fun方法执行:19
服务限流,触发降级操作
服务限流,触发降级操作
服务限流,触发降级操作
。。。
使用示例 3
Test3
public class Test3 {
public static void fun(int index){
System.out.println("fun方法执行:"+index);
}
public static void initFlowRule(){
List<FlowRule> flowRules = new ArrayList<>();
FlowRule flowRule = new FlowRule();
flowRule.setResource("hello3");
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
flowRule.setCount(20);
flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
flowRules.add(flowRule);
FlowRuleManager.loadRules(flowRules);
}
public static void main(String[] args) {
initFlowRule();
for (int i=0;i<100;i++){
try {
AsyncEntry entry = SphU.asyncEntry("hello3");
int finalI = i;
CompletableFuture.runAsync(()->{
ContextUtil.runOnContext(entry.getAsyncContext(),()->{
try {
fun(finalI);
}catch (Exception e){
e.printStackTrace();
}finally {
entry.exit();
}
});
});
}catch (Exception e){
System.out.println("服务限流,触发降级操作");
}
}
System.exit(0);
}
}
点击运行,控制台输出:
INFO: Sentinel log output type is: file
INFO: Sentinel log charset is: utf-8
INFO: Sentinel log base directory is: /Users/huli/logs/csp/
INFO: Sentinel log name use pid is: false
fun方法执行:5
fun方法执行:6
fun方法执行:2
fun方法执行:0
fun方法执行:8
fun方法执行:1
fun方法执行:3
fun方法执行:10
fun方法执行:9
fun方法执行:7
fun方法执行:15
fun方法执行:4
fun方法执行:16
fun方法执行:14
fun方法执行:13
fun方法执行:12
fun方法执行:11
fun方法执行:19
fun方法执行:18
fun方法执行:17
服务限流,触发降级操作
服务限流,触发降级操作
服务限流,触发降级操作
。。。
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/22174.html