From bbe93b0bf30544320b053bc704c17c82173be5c0 Mon Sep 17 00:00:00 2001 From: mshe <666666666@666666666.666666666> Date: Thu, 20 Nov 2025 20:24:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(sentinel):=20=E9=85=8D=E7=BD=AEnacos?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E5=B9=B6=E4=BC=98=E5=8C=96=E6=B5=81=E6=8E=A7?= =?UTF-8?q?=E8=A7=84=E5=88=99=E6=8E=A8=E9=80=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改nacos配置中心地址为172.16.8.70:8848 - 注入FlowRuleNacosPublisher实现流控规则发布功能 - 重构publishRules方法增加异常处理和日志记录 - 调整流控规则dataId格式从后缀改为前缀方式 - 更新Nacos配置发布方法参数支持JSON类型 - 修复代码缩进和格式问题提升可读性 --- .../controller/FlowControllerV1.java | 24 ++++++++++++++----- .../rule/nacos/FlowRuleNacosProvider.java | 5 ++-- .../rule/nacos/FlowRuleNacosPublisher.java | 4 ++-- .../dashboard/rule/nacos/NacosConfigUtil.java | 5 ++-- .../src/main/resources/application.properties | 3 ++- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/FlowControllerV1.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/FlowControllerV1.java index 3806fd76..a91064ae 100755 --- a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/FlowControllerV1.java +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/FlowControllerV1.java @@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit; import com.alibaba.csp.sentinel.dashboard.auth.AuthAction; import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType; import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement; +import com.alibaba.csp.sentinel.dashboard.rule.nacos.FlowRuleNacosPublisher; import com.alibaba.csp.sentinel.util.StringUtil; import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient; @@ -64,6 +65,10 @@ public class FlowControllerV1 { @Autowired private SentinelApiClient sentinelApiClient; + // 注入发布者 + @Autowired + private FlowRuleNacosPublisher publisher; + @GetMapping("/rules") @AuthAction(PrivilegeType.READ_RULE) public Result> apiQueryMachineRules(@RequestParam String app, @@ -169,10 +174,10 @@ public class FlowControllerV1 { @PutMapping("/save.json") @AuthAction(PrivilegeType.WRITE_RULE) public Result apiUpdateFlowRule(Long id, String app, - String limitApp, String resource, Integer grade, - Double count, Integer strategy, String refResource, - Integer controlBehavior, Integer warmUpPeriodSec, - Integer maxQueueingTimeMs) { + String limitApp, String resource, Integer grade, + Double count, Integer strategy, String refResource, + Integer controlBehavior, Integer warmUpPeriodSec, + Integer maxQueueingTimeMs) { if (id == null) { return Result.ofFail(-1, "id can't be null"); } @@ -241,7 +246,7 @@ public class FlowControllerV1 { } catch (Throwable t) { Throwable e = t instanceof ExecutionException ? t.getCause() : t; logger.error("Error when updating flow rules, app={}, ip={}, ruleId={}", entity.getApp(), - entity.getIp(), id, e); + entity.getIp(), id, e); return Result.ofFail(-1, e.getMessage()); } } @@ -269,13 +274,20 @@ public class FlowControllerV1 { } catch (Throwable t) { Throwable e = t instanceof ExecutionException ? t.getCause() : t; logger.error("Error when deleting flow rules, app={}, ip={}, id={}", oldEntity.getApp(), - oldEntity.getIp(), id, e); + oldEntity.getIp(), id, e); return Result.ofFail(-1, e.getMessage()); } } private CompletableFuture publishRules(String app, String ip, Integer port) { List rules = repository.findAllByMachine(MachineInfo.of(app, ip, port)); + try { + publisher.publish(app, rules); + logger.info("限流规则推送到nacos完成,app=[{}], ip=[{}]", app, ip); + } catch (Exception e) { + logger.error("限流规则发布失败, app=[{}], ip=[{}]", app, ip, e); + throw new RuntimeException(e); + } return sentinelApiClient.setFlowRuleOfMachineAsync(app, ip, port, rules); } } diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosProvider.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosProvider.java index bf8534b8..712132df 100644 --- a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosProvider.java +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosProvider.java @@ -47,8 +47,9 @@ public class FlowRuleNacosProvider implements DynamicRuleProvider getRules(String appName) throws Exception { - String rules = configService.getConfig(appName + NacosConfigUtil.FLOW_DATA_ID_POSTFIX, - NacosConfigUtil.GROUP_ID, 3000); + String dataId = String.format("%s%s", NacosConfigUtil.FLOW_DATA_ID_PREFIX, appName); + + String rules = configService.getConfig(dataId, NacosConfigUtil.GROUP_ID, 3000); if (StringUtil.isEmpty(rules)) { return new ArrayList<>(); } diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosPublisher.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosPublisher.java index df0f5712..35e32959 100644 --- a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosPublisher.java +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosPublisher.java @@ -44,9 +44,9 @@ public class FlowRuleNacosPublisher implements DynamicRulePublisher