首页 > Windows开发 > 详细

Sentinel-Api(二)

时间:2019-11-21 09:43:06      阅读:240      评论:0      收藏:0      [点我收藏+]
sentinel-api 与 sentinel-dashboard设置限流
1.sentinel-api: 定义资源,出现异常后累加异常次数
2.sentinel-dashboard: 对上面定义的资源进行限流操作

1.sentinel-api

// 控制器方法
public String test(@RequestParam(required = false) String a){
        // 定义sentinel资源名
        String resourceName="test-service";
        // 模拟设置【来源-微服务】
        ContextUtil.enter(resourceName,"member-center");

        Entry entry=null;
        try {
            entry= SphU.entry(resourceName);
            if (StringUtils.isBlank(a)){
                throw new IllegalArgumentException("参数 a 不能为空");
            }
        } catch (BlockException e) {
            e.printStackTrace();
            return "限流,降级了";
        } catch (IllegalArgumentException e){
            // 增加 IllegalArgumentException 错误次数
            Tracer.trace(e);
            return e.getMessage();
        }finally {
            // 释放资源
            if (entry!=null){
                entry.exit();
            }
            ContextUtil.exit();
        }
        return "success";
    }

sentinel dashboard操作:

  • 浏览器访问一次对应api
  • 在 sentinel dashboard 设置对应资源的限制
    技术分享图片

Sentinel-Api(二)

原文:https://blog.51cto.com/3168834/2451941

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!