Sentinel-Api(二)

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(二)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章