-
Notifications
You must be signed in to change notification settings - Fork 8.2k
[dashboard/1.8] feat: 新增基于Nacos的规则持久化支持 #3535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 1.8
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,33 +15,26 @@ | |||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| package com.alibaba.csp.sentinel.dashboard.controller; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import java.util.Date; | ||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.auth.AuthAction; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.slots.block.RuleConstant; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.util.StringUtil; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.domain.Result; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.repository.rule.RuleRepository; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.slots.block.RuleConstant; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.util.StringUtil; | ||||||||||||||||||||||||
| import org.slf4j.Logger; | ||||||||||||||||||||||||
| import org.slf4j.LoggerFactory; | ||||||||||||||||||||||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.GetMapping; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.PathVariable; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.PostMapping; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.PutMapping; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.RequestBody; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.RequestMapping; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.RequestParam; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.RestController; | ||||||||||||||||||||||||
| import org.springframework.beans.factory.annotation.Qualifier; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.*; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import java.util.Date; | ||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * @author Eric Zhao | ||||||||||||||||||||||||
|
|
@@ -53,6 +46,16 @@ public class AuthorityRuleController { | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private final Logger logger = LoggerFactory.getLogger(AuthorityRuleController.class); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // === 新增:注入 Nacos Provider/Publisher === | ||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This type of comment seems unnecessary - we can safely remove it to keep the code concise. (the same applies to other similar comments below) |
||||||||||||||||||||||||
| @Autowired | ||||||||||||||||||||||||
| @Qualifier("authorityRuleNacosProvider") | ||||||||||||||||||||||||
| private DynamicRuleProvider<List<AuthorityRuleEntity>> ruleProvider; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @Autowired | ||||||||||||||||||||||||
| @Qualifier("authorityRuleNacosPublisher") | ||||||||||||||||||||||||
| private DynamicRulePublisher<List<AuthorityRuleEntity>> rulePublisher; | ||||||||||||||||||||||||
| // === 新增结束 === | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @Autowired | ||||||||||||||||||||||||
| private SentinelApiClient sentinelApiClient; | ||||||||||||||||||||||||
| @Autowired | ||||||||||||||||||||||||
|
|
@@ -78,7 +81,8 @@ public Result<List<AuthorityRuleEntity>> apiQueryAllRulesForMachine(@RequestPara | |||||||||||||||||||||||
| return Result.ofFail(-1, "given ip does not belong to given app"); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| List<AuthorityRuleEntity> rules = sentinelApiClient.fetchAuthorityRulesOfMachine(app, ip, port); | ||||||||||||||||||||||||
| // === 修改:从 Nacos 获取规则 === | ||||||||||||||||||||||||
| List<AuthorityRuleEntity> rules = ruleProvider.getRules(app); | ||||||||||||||||||||||||
| rules = repository.saveAll(rules); | ||||||||||||||||||||||||
| return Result.ofSuccess(rules); | ||||||||||||||||||||||||
| } catch (Throwable throwable) { | ||||||||||||||||||||||||
|
|
@@ -129,6 +133,11 @@ public Result<AuthorityRuleEntity> apiAddAuthorityRule(@RequestBody AuthorityRul | |||||||||||||||||||||||
| entity.setGmtModified(date); | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| entity = repository.save(entity); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // === 新增:发布到 Nacos === | ||||||||||||||||||||||||
| publishRulesToNacos(entity.getApp()); | ||||||||||||||||||||||||
| // === 新增结束 === | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| } catch (Throwable throwable) { | ||||||||||||||||||||||||
| logger.error("Failed to add authority rule", throwable); | ||||||||||||||||||||||||
| return Result.ofThrowable(-1, throwable); | ||||||||||||||||||||||||
|
|
@@ -159,6 +168,11 @@ public Result<AuthorityRuleEntity> apiUpdateParamFlowRule(@PathVariable("id") Lo | |||||||||||||||||||||||
| if (entity == null) { | ||||||||||||||||||||||||
| return Result.ofFail(-1, "Failed to save authority rule"); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // === 新增:发布到 Nacos === | ||||||||||||||||||||||||
| publishRulesToNacos(entity.getApp()); | ||||||||||||||||||||||||
| // === 新增结束 === | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| } catch (Throwable throwable) { | ||||||||||||||||||||||||
| logger.error("Failed to save authority rule", throwable); | ||||||||||||||||||||||||
| return Result.ofThrowable(-1, throwable); | ||||||||||||||||||||||||
|
|
@@ -181,6 +195,11 @@ public Result<Long> apiDeleteRule(@PathVariable("id") Long id) { | |||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| repository.delete(id); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // === 新增:发布到 Nacos === | ||||||||||||||||||||||||
| publishRulesToNacos(oldEntity.getApp()); | ||||||||||||||||||||||||
| // === 新增结束 === | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| } catch (Exception e) { | ||||||||||||||||||||||||
| return Result.ofFail(-1, e.getMessage()); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
@@ -194,4 +213,11 @@ private boolean publishRules(String app, String ip, Integer port) { | |||||||||||||||||||||||
| List<AuthorityRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port)); | ||||||||||||||||||||||||
| return sentinelApiClient.setAuthorityRuleOfMachine(app, ip, port, rules); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // === 新增:发布规则到 Nacos 的方法 === | ||||||||||||||||||||||||
| private void publishRulesToNacos(String app) throws Exception { | ||||||||||||||||||||||||
| List<AuthorityRuleEntity> rules = repository.findAllByApp(app); | ||||||||||||||||||||||||
| rulePublisher.publish(app, rules); | ||||||||||||||||||||||||
|
Comment on lines
+218
to
+220
|
||||||||||||||||||||||||
| private void publishRulesToNacos(String app) throws Exception { | |
| List<AuthorityRuleEntity> rules = repository.findAllByApp(app); | |
| rulePublisher.publish(app, rules); | |
| private void publishRulesToNacos(String app) { | |
| try { | |
| List<AuthorityRuleEntity> rules = repository.findAllByApp(app); | |
| rulePublisher.publish(app, rules); | |
| } catch (Exception e) { | |
| logger.error("Failed to publish authority rules to Nacos for app: {}", app, e); | |
| throw new RuntimeException("Failed to publish authority rules to Nacos for app: " + app, e); | |
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,33 +15,27 @@ | |||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| package com.alibaba.csp.sentinel.dashboard.controller; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import java.util.Date; | ||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.auth.AuthAction; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.domain.Result; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.repository.rule.RuleRepository; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.slots.block.RuleConstant; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.CircuitBreakerStrategy; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.util.StringUtil; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity; | ||||||||||||||||||||||||
| import com.alibaba.csp.sentinel.dashboard.domain.Result; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import org.slf4j.Logger; | ||||||||||||||||||||||||
| import org.slf4j.LoggerFactory; | ||||||||||||||||||||||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.GetMapping; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.PathVariable; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.PostMapping; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.PutMapping; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.RequestBody; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.RequestMapping; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.RestController; | ||||||||||||||||||||||||
| import org.springframework.beans.factory.annotation.Qualifier; | ||||||||||||||||||||||||
| import org.springframework.web.bind.annotation.*; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import java.util.Date; | ||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Controller regarding APIs of degrade rules. Refactored since 1.8.0. | ||||||||||||||||||||||||
|
|
@@ -55,6 +49,14 @@ public class DegradeController { | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private final Logger logger = LoggerFactory.getLogger(DegradeController.class); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @Autowired | ||||||||||||||||||||||||
| @Qualifier("degradeRuleNacosProvider") | ||||||||||||||||||||||||
| private DynamicRuleProvider<List<DegradeRuleEntity>> ruleProvider; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @Autowired | ||||||||||||||||||||||||
| @Qualifier("degradeRuleNacosPublisher") | ||||||||||||||||||||||||
| private DynamicRulePublisher<List<DegradeRuleEntity>> rulePublisher; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @Autowired | ||||||||||||||||||||||||
| private RuleRepository<DegradeRuleEntity, Long> repository; | ||||||||||||||||||||||||
| @Autowired | ||||||||||||||||||||||||
|
|
@@ -78,7 +80,7 @@ public Result<List<DegradeRuleEntity>> apiQueryMachineRules(String app, String i | |||||||||||||||||||||||
| return Result.ofFail(-1, "given ip does not belong to given app"); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| List<DegradeRuleEntity> rules = sentinelApiClient.fetchDegradeRuleOfMachine(app, ip, port); | ||||||||||||||||||||||||
| List<DegradeRuleEntity> rules = ruleProvider.getRules(app); | ||||||||||||||||||||||||
| rules = repository.saveAll(rules); | ||||||||||||||||||||||||
| return Result.ofSuccess(rules); | ||||||||||||||||||||||||
| } catch (Throwable throwable) { | ||||||||||||||||||||||||
|
|
@@ -99,13 +101,11 @@ public Result<DegradeRuleEntity> apiAddRule(@RequestBody DegradeRuleEntity entit | |||||||||||||||||||||||
| entity.setGmtModified(date); | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| entity = repository.save(entity); | ||||||||||||||||||||||||
| publishRulesToNacos(entity.getApp()); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| } 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); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
@@ -133,13 +133,11 @@ public Result<DegradeRuleEntity> apiUpdateRule(@PathVariable("id") Long id, | |||||||||||||||||||||||
| entity.setGmtModified(new Date()); | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| entity = repository.save(entity); | ||||||||||||||||||||||||
| publishRulesToNacos(entity.getApp()); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| } 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); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
@@ -157,13 +155,11 @@ public Result<Long> delete(@PathVariable("id") Long id) { | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| repository.delete(id); | ||||||||||||||||||||||||
| publishRulesToNacos(oldEntity.getApp()); | ||||||||||||||||||||||||
| } 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); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
@@ -172,6 +168,11 @@ private boolean publishRules(String app, String ip, Integer port) { | |||||||||||||||||||||||
| return sentinelApiClient.setDegradeRuleOfMachine(app, ip, port, rules); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| private void publishRulesToNacos(String app) throws Exception { | ||||||||||||||||||||||||
| List<DegradeRuleEntity> rules = repository.findAllByApp(app); | ||||||||||||||||||||||||
| rulePublisher.publish(app, rules); | ||||||||||||||||||||||||
|
Comment on lines
+171
to
+173
|
||||||||||||||||||||||||
| private void publishRulesToNacos(String app) throws Exception { | |
| List<DegradeRuleEntity> rules = repository.findAllByApp(app); | |
| rulePublisher.publish(app, rules); | |
| private void publishRulesToNacos(String app) { | |
| try { | |
| List<DegradeRuleEntity> rules = repository.findAllByApp(app); | |
| rulePublisher.publish(app, rules); | |
| } catch (Exception e) { | |
| logger.error("Failed to publish degrade rules to Nacos for app: {}", app, e); | |
| throw new RuntimeException("Failed to publish degrade rules to Nacos for app: " + app, e); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we consider add nacos-discovery?