Search in sources :

Example 1 with DegradeRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity in project pig by pig-mesh.

the class DegradeController method apiUpdateRule.

@PutMapping("/rule/{id}")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<DegradeRuleEntity> apiUpdateRule(@PathVariable("id") Long id, @RequestBody DegradeRuleEntity entity) {
    if (id == null || id <= 0) {
        return Result.ofFail(-1, "id can't be null or negative");
    }
    DegradeRuleEntity oldEntity = repository.findById(id);
    if (oldEntity == null) {
        return Result.ofFail(-1, "Degrade rule does not exist, id=" + id);
    }
    entity.setApp(oldEntity.getApp());
    entity.setIp(oldEntity.getIp());
    entity.setPort(oldEntity.getPort());
    entity.setId(oldEntity.getId());
    Result<DegradeRuleEntity> checkResult = checkEntityInternal(entity);
    if (checkResult != null) {
        return checkResult;
    }
    entity.setGmtCreate(oldEntity.getGmtCreate());
    entity.setGmtModified(new Date());
    try {
        entity = repository.save(entity);
    } catch (Throwable t) {
        logger.error("Failed to save degrade rule, id={}, rule={}", id, entity, t);
        return Result.ofThrowable(-1, t);
    }
    if (!publishRules(entity.getApp(), entity.getIp(), entity.getPort())) {
        logger.warn("Publish degrade rules failed, app={}", entity.getApp());
    }
    return Result.ofSuccess(entity);
}
Also used : DegradeRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity) Date(java.util.Date) PutMapping(org.springframework.web.bind.annotation.PutMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 2 with DegradeRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity in project spring-boot-student by wyh-spring-ecosystem-student.

the class DegradeController method add.

@ResponseBody
@RequestMapping("/new.json")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<DegradeRuleEntity> add(String app, String ip, Integer port, String limitApp, String resource, Double count, Integer timeWindow, Integer grade) {
    if (StringUtil.isBlank(app)) {
        return Result.ofFail(-1, "app can't be null or empty");
    }
    if (StringUtil.isBlank(ip)) {
        return Result.ofFail(-1, "ip can't be null or empty");
    }
    if (port == null) {
        return Result.ofFail(-1, "port can't be null");
    }
    if (StringUtil.isBlank(limitApp)) {
        return Result.ofFail(-1, "limitApp can't be null or empty");
    }
    if (StringUtil.isBlank(resource)) {
        return Result.ofFail(-1, "resource can't be null or empty");
    }
    if (count == null) {
        return Result.ofFail(-1, "count can't be null");
    }
    if (timeWindow == null) {
        return Result.ofFail(-1, "timeWindow can't be null");
    }
    if (grade == null) {
        return Result.ofFail(-1, "grade can't be null");
    }
    if (grade < RuleConstant.DEGRADE_GRADE_RT || grade > RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT) {
        return Result.ofFail(-1, "Invalid grade: " + grade);
    }
    DegradeRuleEntity entity = new DegradeRuleEntity();
    entity.setApp(app.trim());
    entity.setIp(ip.trim());
    entity.setPort(port);
    entity.setLimitApp(limitApp.trim());
    entity.setResource(resource.trim());
    entity.setCount(count);
    entity.setTimeWindow(timeWindow);
    entity.setGrade(grade);
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("add error:", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(app, ip, port)) {
        logger.info("publish degrade rules fail after rule add");
    }
    return Result.ofSuccess(entity);
}
Also used : DegradeRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity) Date(java.util.Date) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 3 with DegradeRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity in project XHuiCloud by sindaZeng.

the class DegradeController method updateIfNotNull.

@ResponseBody
@RequestMapping("/save.json")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<DegradeRuleEntity> updateIfNotNull(Long id, String app, String limitApp, String resource, Double count, Integer timeWindow, Integer grade) {
    if (id == null) {
        return Result.ofFail(-1, "id can't be null");
    }
    if (grade != null) {
        if (grade < RuleConstant.DEGRADE_GRADE_RT || grade > RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT) {
            return Result.ofFail(-1, "Invalid grade: " + grade);
        }
    }
    DegradeRuleEntity entity = repository.findById(id);
    if (entity == null) {
        return Result.ofFail(-1, "id " + id + " dose not exist");
    }
    if (StringUtil.isNotBlank(app)) {
        entity.setApp(app.trim());
    }
    if (StringUtil.isNotBlank(limitApp)) {
        entity.setLimitApp(limitApp.trim());
    }
    if (StringUtil.isNotBlank(resource)) {
        entity.setResource(resource.trim());
    }
    if (count != null) {
        entity.setCount(count);
    }
    if (timeWindow != null) {
        entity.setTimeWindow(timeWindow);
    }
    if (grade != null) {
        entity.setGrade(grade);
    }
    Date date = new Date();
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable throwable) {
        logger.error("save error:", throwable);
        return Result.ofThrowable(-1, throwable);
    }
    if (!publishRules(entity.getApp(), entity.getIp(), entity.getPort())) {
        logger.info("publish degrade rules fail after rule update");
    }
    return Result.ofSuccess(entity);
}
Also used : DegradeRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity) Date(java.util.Date) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 4 with DegradeRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity in project RuoYi-Cloud-Plus by JavaLionLi.

the class DegradeController method apiAddRule.

@PostMapping("/rule")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<DegradeRuleEntity> apiAddRule(@RequestBody DegradeRuleEntity entity) {
    Result<DegradeRuleEntity> checkResult = checkEntityInternal(entity);
    if (checkResult != null) {
        return checkResult;
    }
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    try {
        entity = repository.save(entity);
    } catch (Throwable t) {
        logger.error("Failed to add new degrade rule, app={}, ip={}", entity.getApp(), entity.getIp(), t);
        return Result.ofThrowable(-1, t);
    }
    if (!publishRules(entity.getApp(), entity.getIp(), entity.getPort())) {
        logger.warn("Publish degrade rules failed, app={}", entity.getApp());
    }
    return Result.ofSuccess(entity);
}
Also used : DegradeRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity) Date(java.util.Date) PostMapping(org.springframework.web.bind.annotation.PostMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Example 5 with DegradeRuleEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity in project RuoYi-Cloud-Plus by JavaLionLi.

the class DegradeController method apiUpdateRule.

@PutMapping("/rule/{id}")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<DegradeRuleEntity> apiUpdateRule(@PathVariable("id") Long id, @RequestBody DegradeRuleEntity entity) {
    if (id == null || id <= 0) {
        return Result.ofFail(-1, "id can't be null or negative");
    }
    DegradeRuleEntity oldEntity = repository.findById(id);
    if (oldEntity == null) {
        return Result.ofFail(-1, "Degrade rule does not exist, id=" + id);
    }
    entity.setApp(oldEntity.getApp());
    entity.setIp(oldEntity.getIp());
    entity.setPort(oldEntity.getPort());
    entity.setId(oldEntity.getId());
    Result<DegradeRuleEntity> checkResult = checkEntityInternal(entity);
    if (checkResult != null) {
        return checkResult;
    }
    entity.setGmtCreate(oldEntity.getGmtCreate());
    entity.setGmtModified(new Date());
    try {
        entity = repository.save(entity);
    } catch (Throwable t) {
        logger.error("Failed to save degrade rule, id={}, rule={}", id, entity, t);
        return Result.ofThrowable(-1, t);
    }
    if (!publishRules(entity.getApp(), entity.getIp(), entity.getPort())) {
        logger.warn("Publish degrade rules failed, app={}", entity.getApp());
    }
    return Result.ofSuccess(entity);
}
Also used : DegradeRuleEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity) Date(java.util.Date) PutMapping(org.springframework.web.bind.annotation.PutMapping) AuthAction(com.alibaba.csp.sentinel.dashboard.auth.AuthAction)

Aggregations

AuthAction (com.alibaba.csp.sentinel.dashboard.auth.AuthAction)10 DegradeRuleEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity)10 Date (java.util.Date)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)4 PostMapping (org.springframework.web.bind.annotation.PostMapping)3 PutMapping (org.springframework.web.bind.annotation.PutMapping)3