|
@@ -104,49 +104,10 @@ public class DegradeController {
|
|
|
|
|
|
@ResponseBody
|
|
|
@RequestMapping("/rule")
|
|
|
- public Result<DegradeRuleEntity> add(HttpServletRequest request, @RequestBody DegradeRuleEntity entity
|
|
|
-// String app, String ip, Integer port, String limitApp, String resource,
|
|
|
-// Double count, Integer timeWindow, Integer grade
|
|
|
- ) {
|
|
|
+ public Result<DegradeRuleEntity> add(HttpServletRequest request, @RequestBody DegradeRuleEntity entity) {
|
|
|
AuthUser authUser = authService.getAuthUser(request);
|
|
|
authUser.authTarget(entity.getApp(), PrivilegeType.WRITE_RULE);
|
|
|
|
|
|
-// 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);
|
|
@@ -163,9 +124,16 @@ public class DegradeController {
|
|
|
@ResponseBody
|
|
|
@RequestMapping("/save.json")
|
|
|
public Result<DegradeRuleEntity> updateIfNotNull(HttpServletRequest request,
|
|
|
- Long id, String app, String limitApp, String resource,
|
|
|
- Double count, Integer timeWindow, Integer grade) {
|
|
|
+ @RequestBody DegradeRuleEntity param) {
|
|
|
AuthUser authUser = authService.getAuthUser(request);
|
|
|
+ Long id = param.getId();
|
|
|
+ Integer grade = param.getGrade();
|
|
|
+ String app = param.getApp();
|
|
|
+ String limitApp = param.getLimitApp();
|
|
|
+ String resource = param.getResource();
|
|
|
+ Double count = param.getCount();
|
|
|
+ Integer timeWindow = param.getTimeWindow();
|
|
|
+ Integer minRequestAmount = param.getMinRequestAmount();
|
|
|
if (id == null) {
|
|
|
return Result.ofFail(-1, "id can't be null");
|
|
|
}
|
|
@@ -198,6 +166,9 @@ public class DegradeController {
|
|
|
if (grade != null) {
|
|
|
entity.setGrade(grade);
|
|
|
}
|
|
|
+ if(minRequestAmount != null){
|
|
|
+ entity.setMinRequestAmount(minRequestAmount);
|
|
|
+ }
|
|
|
Date date = new Date();
|
|
|
entity.setGmtModified(date);
|
|
|
try {
|
|
@@ -238,163 +209,4 @@ public class DegradeController {
|
|
|
rulePublisher.publish(app, rules);
|
|
|
}
|
|
|
|
|
|
-// @GetMapping("/rules.json")
|
|
|
-// @AuthAction(PrivilegeType.READ_RULE)
|
|
|
-// public Result<List<DegradeRuleEntity>> apiQueryMachineRules(String app, String ip, Integer port) {
|
|
|
-// if (StringUtil.isEmpty(app)) {
|
|
|
-// return Result.ofFail(-1, "app can't be null or empty");
|
|
|
-// }
|
|
|
-// if (StringUtil.isEmpty(ip)) {
|
|
|
-// return Result.ofFail(-1, "ip can't be null or empty");
|
|
|
-// }
|
|
|
-// if (port == null) {
|
|
|
-// return Result.ofFail(-1, "port can't be null");
|
|
|
-// }
|
|
|
-// try {
|
|
|
-// List<DegradeRuleEntity> rules = sentinelApiClient.fetchDegradeRuleOfMachine(app, ip, port);
|
|
|
-// rules = repository.saveAll(rules);
|
|
|
-// return Result.ofSuccess(rules);
|
|
|
-// } catch (Throwable throwable) {
|
|
|
-// logger.error("queryApps error:", throwable);
|
|
|
-// return Result.ofThrowable(-1, throwable);
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// @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);
|
|
|
-// }
|
|
|
-//
|
|
|
-// @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);
|
|
|
-// }
|
|
|
-//
|
|
|
-// @DeleteMapping("/rule/{id}")
|
|
|
-// @AuthAction(PrivilegeType.DELETE_RULE)
|
|
|
-// public Result<Long> delete(@PathVariable("id") Long id) {
|
|
|
-// if (id == null) {
|
|
|
-// return Result.ofFail(-1, "id can't be null");
|
|
|
-// }
|
|
|
-//
|
|
|
-// DegradeRuleEntity oldEntity = repository.findById(id);
|
|
|
-// if (oldEntity == null) {
|
|
|
-// return Result.ofSuccess(null);
|
|
|
-// }
|
|
|
-//
|
|
|
-// try {
|
|
|
-// repository.delete(id);
|
|
|
-// } catch (Throwable throwable) {
|
|
|
-// logger.error("Failed to delete degrade rule, id={}", id, throwable);
|
|
|
-// return Result.ofThrowable(-1, throwable);
|
|
|
-// }
|
|
|
-// if (!publishRules(oldEntity.getApp(), oldEntity.getIp(), oldEntity.getPort())) {
|
|
|
-// logger.warn("Publish degrade rules failed, app={}", oldEntity.getApp());
|
|
|
-// }
|
|
|
-// return Result.ofSuccess(id);
|
|
|
-// }
|
|
|
-//
|
|
|
-// private boolean publishRules(String app, String ip, Integer port) {
|
|
|
-// List<DegradeRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
|
|
|
-// return sentinelApiClient.setDegradeRuleOfMachine(app, ip, port, rules);
|
|
|
-// }
|
|
|
-//
|
|
|
-// private <R> Result<R> checkEntityInternal(DegradeRuleEntity entity) {
|
|
|
-// if (StringUtil.isBlank(entity.getApp())) {
|
|
|
-// return Result.ofFail(-1, "app can't be blank");
|
|
|
-// }
|
|
|
-// if (StringUtil.isBlank(entity.getIp())) {
|
|
|
-// return Result.ofFail(-1, "ip can't be null or empty");
|
|
|
-// }
|
|
|
-// if (entity.getPort() == null || entity.getPort() <= 0) {
|
|
|
-// return Result.ofFail(-1, "invalid port: " + entity.getPort());
|
|
|
-// }
|
|
|
-// if (StringUtil.isBlank(entity.getLimitApp())) {
|
|
|
-// return Result.ofFail(-1, "limitApp can't be null or empty");
|
|
|
-// }
|
|
|
-// if (StringUtil.isBlank(entity.getResource())) {
|
|
|
-// return Result.ofFail(-1, "resource can't be null or empty");
|
|
|
-// }
|
|
|
-// Double threshold = entity.getCount();
|
|
|
-// if (threshold == null || threshold < 0) {
|
|
|
-// return Result.ofFail(-1, "invalid threshold: " + threshold);
|
|
|
-// }
|
|
|
-// Integer recoveryTimeoutSec = entity.getTimeWindow();
|
|
|
-// if (recoveryTimeoutSec == null || recoveryTimeoutSec <= 0) {
|
|
|
-// return Result.ofFail(-1, "recoveryTimeout should be positive");
|
|
|
-// }
|
|
|
-// Integer strategy = entity.getGrade();
|
|
|
-// if (strategy == null) {
|
|
|
-// return Result.ofFail(-1, "circuit breaker strategy cannot be null");
|
|
|
-// }
|
|
|
-// if (strategy < CircuitBreakerStrategy.SLOW_REQUEST_RATIO.getType()
|
|
|
-// || strategy > RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT) {
|
|
|
-// return Result.ofFail(-1, "Invalid circuit breaker strategy: " + strategy);
|
|
|
-// }
|
|
|
-// if (entity.getMinRequestAmount() == null || entity.getMinRequestAmount() <= 0) {
|
|
|
-// return Result.ofFail(-1, "Invalid minRequestAmount");
|
|
|
-// }
|
|
|
-// if (entity.getStatIntervalMs() == null || entity.getStatIntervalMs() <= 0) {
|
|
|
-// return Result.ofFail(-1, "Invalid statInterval");
|
|
|
-// }
|
|
|
-// if (strategy == RuleConstant.DEGRADE_GRADE_RT) {
|
|
|
-// Double slowRatio = entity.getSlowRatioThreshold();
|
|
|
-// if (slowRatio == null) {
|
|
|
-// return Result.ofFail(-1, "SlowRatioThreshold is required for slow request ratio strategy");
|
|
|
-// } else if (slowRatio < 0 || slowRatio > 1) {
|
|
|
-// return Result.ofFail(-1, "SlowRatioThreshold should be in range: [0.0, 1.0]");
|
|
|
-// }
|
|
|
-// } else if (strategy == RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO) {
|
|
|
-// if (threshold > 1) {
|
|
|
-// return Result.ofFail(-1, "Ratio threshold should be in range: [0.0, 1.0]");
|
|
|
-// }
|
|
|
-// }
|
|
|
-// return null;
|
|
|
-// }
|
|
|
}
|