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
服务限流,触发降级操作
服务限流,触发降级操作
服务限流,触发降级操作
。。。

sentinel 使用示例相关推荐

  1. python连接redis哨兵_Python redis.sentinel方法代码示例

    本文整理汇总了Python中redis.sentinel方法的典型用法代码示例.如果您正苦于以下问题:Python redis.sentinel方法的具体用法?Python redis.sentine ...

  2. 微服务限流Sentinel讲解(五)

    授权规则可以对请求方来源做判断和控制. 4.1.授权规则 4.1.1.基本规则 授权规则可以对调用方的来源做控制,有白名单和黑名单两种方式. 白名单:来源(origin)在白名单内的调用者允许访问 黑 ...

  3. Redis 哨兵Sentinel 文档

    Redis 的 Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务: 监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服 ...

  4. Sentinel(十九)之主流框架的适配

    转载自  主流框架的适配 注:适配模块仅提供相应适配功能,若希望接入 Sentinel 控制台,请务必参考 Sentinel 控制台文档. 云原生微服务体系 Spring Cloud Spring C ...

  5. Redis Sentinel

    2019独角兽企业重金招聘Python工程师标准>>> Redis Sentinel Documentation Redis 的 Sentinel 系统用于管理多个 Redis 服务 ...

  6. Redis 的 Sentinel哨兵介绍与源码分析(1):初始化部分

    http://www.redis.cn/topics/sentinel.html redis-6.0.8 本文是在官方中文文档的基础上进行的源码分析,其中包含完整的原文,并在此基础上,添加源码介绍. ...

  7. redis 之sentinel配置

    Redis 命令参考 » Sentinel 本文档翻译自: http://redis.io/topics/sentinel . Redis 的 Sentinel 系统用于管理多个 Redis 服务器( ...

  8. Sentinel 为 RocketMQ 服务保驾护航

    在 Apache RocketMQ 中,当消费者去消费消息的时候,无论是通过 pull 的方式还是 push 的方式,都可能会出现大批量的消息突刺.如果此时要处理所有消息,很可能会导致系统负载过高,影 ...

  9. Sentinel微服务流量控制熔断降级及稳定性监控IO框架

    目录 Sentinel 介绍 Sentinel 的历史 Sentinel 基本概念 资源 规则 Sentinel 功能和设计理念 流量控制 熔断降级 系统负载保护 Sentinel 是如何工作的 快速 ...

最新文章

  1. 知识图谱如何让“人工智能”更智能?
  2. 使用Excel调用ABAP系统的函数
  3. Visual Studio项目引用出现感叹号怎么办?
  4. js原生后代选择器_CSS 后代选择器
  5. html jade文件,Jade模板
  6. JeecgBoot轻松解决ERP项目复杂布局需求,JVXETable高性能行表格效果和项目案例
  7. 【bzoj1532】[POI2005]Kos-Dicing 二分+网络流最大流
  8. for 循环 and while 循环(二)
  9. oracle----globle temp table
  10. ubuntu zip文件解压失败
  11. 电子档案管理系统java,电子档案管理系统单点登陆示例
  12. python 取整求余函数
  13. 别做正常的傻瓜-读后感
  14. 数字图像处理——隐形眼镜缺陷检测算法
  15. 【OJ每日一练】1049 - 矩阵对角线元素之和 v1.0
  16. 2 机器学习入门——逻辑回归之kaggle泰坦尼克号竞赛
  17. Python在线办公系统毕业设计源码071116
  18. 【原创】年轻人接受指点,但不接受指指点点
  19. qq拼音纯净版下载QQPinyin_Setup_1.3.1265.400
  20. Mysql清除字段中的中文,只保留数字、字母等非中文符号

热门文章

  1. 嵌入式实时操作系统的设计与开发New(三)
  2. EveryonePiano:永久免费的电脑键盘钢琴模拟软件
  3. mysql 表里restrict_数据库的三种状态RESTRICT、QUIESCE和SUSPEND(一)
  4. onKeyDown方法笔记
  5. [问题解决]更新订单BOM中的Qty Var-Sz Item字段失败
  6. 如何用python批量下载数据_利用python脚本,批量自动下载欧洲中心的气象数据
  7. 【赵强老师】开发第一个Java程序
  8. Google最强模型BERT出炉,NLP还有哪些值得期待的发展?
  9. MyBatis从使用到源码(上部:使用)
  10. 15.23数据库(23):MySQL事务