From 35dbe68c4d19052ac7d9979864bc91734fb44a32 Mon Sep 17 00:00:00 2001 From: mshe <666666666@666666666.666666666> Date: Thu, 20 Nov 2025 23:07:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(sentinel):=20=E5=AE=9E=E7=8E=B0Nacos?= =?UTF-8?q?=E8=A7=84=E5=88=99=E6=8E=A8=E9=80=81=E5=8A=9F=E8=83=BD=E5=B9=B6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8C=85=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增AuthorityRule、DegradeRule、ParamFlowRule和SystemRule的Nacos推送实现 - 重构FlowRule相关类到独立的flow包下 - 统一Nacos配置工具类,支持多种规则类型的dataId生成 - 移除对nacos-api的直接依赖声明 - 为各类规则增加Nacos配置的编解码器 - 在控制器中集成Nacos推送逻辑,增强规则发布的可靠性 --- sentinel-dashboard/pom.xml | 10 +-- .../controller/AuthorityRuleController.java | 11 ++++ .../controller/DegradeController.java | 12 +++- .../controller/FlowControllerV1.java | 2 +- .../controller/ParamFlowRuleController.java | 12 +++- .../controller/SystemController.java | 12 +++- .../rule/auth/AuthorityRuleApiProvider.java | 55 +++++++++++++++++ .../rule/auth/AuthorityRuleApiPublisher.java | 61 +++++++++++++++++++ .../rule/auth/AuthorityRuleNacosProvider.java | 58 ++++++++++++++++++ .../auth/AuthorityRuleNacosPublisher.java | 53 ++++++++++++++++ .../rule/degrade/DegradeRuleApiProvider.java | 56 +++++++++++++++++ .../rule/degrade/DegradeRuleApiPublisher.java | 60 ++++++++++++++++++ .../degrade/DegradeRuleNacosProvider.java | 58 ++++++++++++++++++ .../degrade/DegradeRuleNacosPublisher.java | 52 ++++++++++++++++ .../rule/{ => flow}/FlowRuleApiProvider.java | 3 +- .../rule/{ => flow}/FlowRuleApiPublisher.java | 3 +- .../FlowRuleNacosProvider.java | 7 ++- .../FlowRuleNacosPublisher.java | 10 +-- .../dashboard/rule/nacos/NacosConfig.java | 57 +++++++++++++++-- .../dashboard/rule/nacos/NacosConfigUtil.java | 31 +++++++++- .../rule/param/ParamRuleApiProvider.java | 60 ++++++++++++++++++ .../rule/param/ParamRuleApiPublisher.java | 60 ++++++++++++++++++ .../rule/param/ParamRuleNacosProvider.java | 58 ++++++++++++++++++ .../rule/param/ParamRuleNacosPublisher.java | 52 ++++++++++++++++ .../rule/system/SystemRuleApiProvider.java | 55 +++++++++++++++++ .../rule/system/SystemRuleApiPublisher.java | 60 ++++++++++++++++++ .../rule/system/SystemRuleNacosProvider.java | 58 ++++++++++++++++++ .../rule/system/SystemRuleNacosPublisher.java | 52 ++++++++++++++++ 28 files changed, 1052 insertions(+), 26 deletions(-) create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/auth/AuthorityRuleApiProvider.java create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/auth/AuthorityRuleApiPublisher.java create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/auth/AuthorityRuleNacosProvider.java create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/auth/AuthorityRuleNacosPublisher.java create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/degrade/DegradeRuleApiProvider.java create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/degrade/DegradeRuleApiPublisher.java create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/degrade/DegradeRuleNacosProvider.java create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/degrade/DegradeRuleNacosPublisher.java rename sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/{ => flow}/FlowRuleApiProvider.java (94%) rename sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/{ => flow}/FlowRuleApiPublisher.java (94%) rename sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/{nacos => flow}/FlowRuleNacosProvider.java (89%) rename sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/{nacos => flow}/FlowRuleNacosPublisher.java (81%) create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/param/ParamRuleApiProvider.java create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/param/ParamRuleApiPublisher.java create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/param/ParamRuleNacosProvider.java create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/param/ParamRuleNacosPublisher.java create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/system/SystemRuleApiProvider.java create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/system/SystemRuleApiPublisher.java create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/system/SystemRuleNacosProvider.java create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/system/SystemRuleNacosPublisher.java diff --git a/sentinel-dashboard/pom.xml b/sentinel-dashboard/pom.xml index 8ee444f6..aa22073e 100644 --- a/sentinel-dashboard/pom.xml +++ b/sentinel-dashboard/pom.xml @@ -105,11 +105,11 @@ com.alibaba.csp sentinel-datasource-nacos - - com.alibaba.nacos - nacos-api - 2.5.1 - + + + + + com.ctrip.framework.apollo diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/AuthorityRuleController.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/AuthorityRuleController.java index df6c90e5..ff31be1a 100644 --- a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/AuthorityRuleController.java +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/AuthorityRuleController.java @@ -23,6 +23,8 @@ import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient; 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.rule.auth.AuthorityRuleNacosPublisher; +import com.alibaba.csp.sentinel.dashboard.rule.flow.FlowRuleNacosPublisher; import com.alibaba.csp.sentinel.slots.block.RuleConstant; import com.alibaba.csp.sentinel.util.StringUtil; @@ -59,6 +61,8 @@ public class AuthorityRuleController { private RuleRepository repository; @Autowired private AppManagement appManagement; + @Autowired + private AuthorityRuleNacosPublisher publisher; @GetMapping("/rules") @AuthAction(PrivilegeType.READ_RULE) @@ -192,6 +196,13 @@ public class AuthorityRuleController { private boolean 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.setAuthorityRuleOfMachine(app, ip, port, rules); } } diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/DegradeController.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/DegradeController.java index 4668f55c..164855fa 100755 --- a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/DegradeController.java +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/DegradeController.java @@ -24,6 +24,8 @@ 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.repository.rule.RuleRepository; +import com.alibaba.csp.sentinel.dashboard.rule.auth.AuthorityRuleNacosPublisher; +import com.alibaba.csp.sentinel.dashboard.rule.degrade.DegradeRuleNacosPublisher; 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; @@ -61,7 +63,8 @@ public class DegradeController { private SentinelApiClient sentinelApiClient; @Autowired private AppManagement appManagement; - + @Autowired + private DegradeRuleNacosPublisher publisher; @GetMapping("/rules.json") @AuthAction(PrivilegeType.READ_RULE) public Result> apiQueryMachineRules(String app, String ip, Integer port) { @@ -169,6 +172,13 @@ public class DegradeController { private boolean 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.setDegradeRuleOfMachine(app, ip, port, rules); } 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 a91064ae..c930252b 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,7 +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.dashboard.rule.flow.FlowRuleNacosPublisher; import com.alibaba.csp.sentinel.util.StringUtil; import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient; diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/ParamFlowRuleController.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/ParamFlowRuleController.java index 513b44dd..c67b8e8e 100644 --- a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/ParamFlowRuleController.java +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/ParamFlowRuleController.java @@ -28,6 +28,8 @@ import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement; import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo; import com.alibaba.csp.sentinel.dashboard.auth.AuthService; import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType; +import com.alibaba.csp.sentinel.dashboard.rule.auth.AuthorityRuleNacosPublisher; +import com.alibaba.csp.sentinel.dashboard.rule.param.ParamRuleNacosPublisher; import com.alibaba.csp.sentinel.slots.block.RuleConstant; import com.alibaba.csp.sentinel.util.StringUtil; import com.alibaba.csp.sentinel.dashboard.datasource.entity.SentinelVersion; @@ -65,7 +67,8 @@ public class ParamFlowRuleController { private AppManagement appManagement; @Autowired private RuleRepository repository; - + @Autowired + private ParamRuleNacosPublisher publisher; private boolean checkIfSupported(String app, String ip, int port) { try { return Optional.ofNullable(appManagement.getDetailApp(app)) @@ -260,6 +263,13 @@ public class ParamFlowRuleController { 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.setParamFlowRuleOfMachine(app, ip, port, rules); } diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/SystemController.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/SystemController.java index b3f3577e..25ce2c9d 100755 --- a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/SystemController.java +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/SystemController.java @@ -22,6 +22,8 @@ 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.repository.rule.RuleRepository; +import com.alibaba.csp.sentinel.dashboard.rule.auth.AuthorityRuleNacosPublisher; +import com.alibaba.csp.sentinel.dashboard.rule.system.SystemRuleNacosPublisher; import com.alibaba.csp.sentinel.util.StringUtil; import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity; @@ -51,7 +53,8 @@ public class SystemController { private SentinelApiClient sentinelApiClient; @Autowired private AppManagement appManagement; - + @Autowired + private SystemRuleNacosPublisher publisher; private Result checkBasicParams(String app, String ip, Integer port) { if (StringUtil.isEmpty(app)) { return Result.ofFail(-1, "app can't be null or empty"); @@ -251,6 +254,13 @@ public class SystemController { private boolean 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.setSystemRuleOfMachine(app, ip, port, rules); } } diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/auth/AuthorityRuleApiProvider.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/auth/AuthorityRuleApiProvider.java new file mode 100644 index 00000000..d80225b5 --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/auth/AuthorityRuleApiProvider.java @@ -0,0 +1,55 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.auth; + +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.rule.DynamicRuleProvider; +import com.alibaba.csp.sentinel.util.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +@Component("authorityRuleDefaultProvider") +public class AuthorityRuleApiProvider implements DynamicRuleProvider> { + + @Autowired + private SentinelApiClient sentinelApiClient; + @Autowired + private AppManagement appManagement; + + @Override + public List getRules(String appName) throws Exception { + if (StringUtil.isBlank(appName)) { + return new ArrayList<>(); + } + List list = appManagement.getDetailApp(appName).getMachines() + .stream() + .filter(MachineInfo::isHealthy) + .sorted((e1, e2) -> Long.compare(e2.getLastHeartbeat(), e1.getLastHeartbeat())).collect(Collectors.toList()); + if (list.isEmpty()) { + return new ArrayList<>(); + } else { + MachineInfo machine = list.get(0); + return sentinelApiClient.fetchAuthorityRulesOfMachine(machine.getApp(), machine.getIp(), machine.getPort()); + } + } +} diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/auth/AuthorityRuleApiPublisher.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/auth/AuthorityRuleApiPublisher.java new file mode 100644 index 00000000..c3894bfb --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/auth/AuthorityRuleApiPublisher.java @@ -0,0 +1,61 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.auth; + +import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient; +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity; +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.rule.DynamicRulePublisher; +import com.alibaba.csp.sentinel.util.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Set; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +@Component("authorityRuleDefaultPublisher") +public class AuthorityRuleApiPublisher implements DynamicRulePublisher> { + + @Autowired + private SentinelApiClient sentinelApiClient; + @Autowired + private AppManagement appManagement; + + @Override + public void publish(String app, List rules) throws Exception { + if (StringUtil.isBlank(app)) { + return; + } + if (rules == null) { + return; + } + Set set = appManagement.getDetailApp(app).getMachines(); + + for (MachineInfo machine : set) { + if (!machine.isHealthy()) { + continue; + } + // TODO: parse the results + sentinelApiClient.setAuthorityRuleOfMachine(app, machine.getIp(), machine.getPort(), rules); + } + } +} diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/auth/AuthorityRuleNacosProvider.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/auth/AuthorityRuleNacosProvider.java new file mode 100644 index 00000000..abd04a64 --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/auth/AuthorityRuleNacosProvider.java @@ -0,0 +1,58 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.auth; + +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity; +import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider; +import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil; +import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.util.StringUtil; +import com.alibaba.nacos.api.config.ConfigService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +@Component("authorityRuleNacosProvider") +public class AuthorityRuleNacosProvider implements DynamicRuleProvider> { + + @Autowired + private ConfigService configService; + @Autowired + private Converter> converter; + + /** + * 获取规则 + * + * @degrade appName 应用名称 + * @return 规则列表 + * @throws Exception 异常 + */ + @Override + public List getRules(String appName) throws Exception { + String dataId = NacosConfigUtil.getAuthorityRuleDataId(appName); + String rules = configService.getConfig(dataId, NacosConfigUtil.GROUP_ID, 3000); + if (StringUtil.isEmpty(rules)) { + return new ArrayList<>(); + } + return converter.convert(rules); + } +} diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/auth/AuthorityRuleNacosPublisher.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/auth/AuthorityRuleNacosPublisher.java new file mode 100644 index 00000000..2d809373 --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/auth/AuthorityRuleNacosPublisher.java @@ -0,0 +1,53 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.auth; + +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity; +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity; +import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher; +import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil; +import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.util.AssertUtil; +import com.alibaba.nacos.api.config.ConfigService; +import com.alibaba.nacos.api.config.ConfigType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +@Component("authorityRuleNacosPublisher") +public class AuthorityRuleNacosPublisher implements DynamicRulePublisher> { + + @Autowired + private ConfigService configService; + @Autowired + private Converter, String> converter; + + @Override + public void publish(String app, List rules) throws Exception { + AssertUtil.notEmpty(app, "app name cannot be empty"); + if (rules == null) { + return; + } + String dataId = NacosConfigUtil.getAuthorityRuleDataId(app); + // 发布配置到nacos + configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules), ConfigType.JSON.getType()); + } +} diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/degrade/DegradeRuleApiProvider.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/degrade/DegradeRuleApiProvider.java new file mode 100644 index 00000000..78989e24 --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/degrade/DegradeRuleApiProvider.java @@ -0,0 +1,56 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.degrade; + +import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient; +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity; +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity; +import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement; +import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo; +import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider; +import com.alibaba.csp.sentinel.util.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +@Component("degradeRuleDefaultProvider") +public class DegradeRuleApiProvider implements DynamicRuleProvider> { + + @Autowired + private SentinelApiClient sentinelApiClient; + @Autowired + private AppManagement appManagement; + + @Override + public List getRules(String appName) throws Exception { + if (StringUtil.isBlank(appName)) { + return new ArrayList<>(); + } + List list = appManagement.getDetailApp(appName).getMachines() + .stream() + .filter(MachineInfo::isHealthy) + .sorted((e1, e2) -> Long.compare(e2.getLastHeartbeat(), e1.getLastHeartbeat())).collect(Collectors.toList()); + if (list.isEmpty()) { + return new ArrayList<>(); + } else { + MachineInfo machine = list.get(0); + return sentinelApiClient.fetchDegradeRuleOfMachine(machine.getApp(), machine.getIp(), machine.getPort()); + } + } +} diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/degrade/DegradeRuleApiPublisher.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/degrade/DegradeRuleApiPublisher.java new file mode 100644 index 00000000..2bd1db70 --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/degrade/DegradeRuleApiPublisher.java @@ -0,0 +1,60 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.degrade; + +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.rule.DynamicRulePublisher; +import com.alibaba.csp.sentinel.util.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Set; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +@Component("degradeRuleDefaultPublisher") +public class DegradeRuleApiPublisher implements DynamicRulePublisher> { + + @Autowired + private SentinelApiClient sentinelApiClient; + @Autowired + private AppManagement appManagement; + + @Override + public void publish(String app, List rules) throws Exception { + if (StringUtil.isBlank(app)) { + return; + } + if (rules == null) { + return; + } + Set set = appManagement.getDetailApp(app).getMachines(); + + for (MachineInfo machine : set) { + if (!machine.isHealthy()) { + continue; + } + // TODO: parse the results + sentinelApiClient.setDegradeRuleOfMachine(app, machine.getIp(), machine.getPort(), rules); + } + } +} diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/degrade/DegradeRuleNacosProvider.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/degrade/DegradeRuleNacosProvider.java new file mode 100644 index 00000000..ee80cf97 --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/degrade/DegradeRuleNacosProvider.java @@ -0,0 +1,58 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.degrade; + +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity; +import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider; +import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil; +import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.util.StringUtil; +import com.alibaba.nacos.api.config.ConfigService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +@Component("degradeRuleNacosProvider") +public class DegradeRuleNacosProvider implements DynamicRuleProvider> { + + @Autowired + private ConfigService configService; + @Autowired + private Converter> converter; + + /** + * 获取规则 + * + * @degrade appName 应用名称 + * @return 规则列表 + * @throws Exception 异常 + */ + @Override + public List getRules(String appName) throws Exception { + String dataId = NacosConfigUtil.getDegradeRuleDataId(appName); + String rules = configService.getConfig(dataId, NacosConfigUtil.GROUP_ID, 3000); + if (StringUtil.isEmpty(rules)) { + return new ArrayList<>(); + } + return converter.convert(rules); + } +} diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/degrade/DegradeRuleNacosPublisher.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/degrade/DegradeRuleNacosPublisher.java new file mode 100644 index 00000000..93cf9039 --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/degrade/DegradeRuleNacosPublisher.java @@ -0,0 +1,52 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.degrade; + +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity; +import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher; +import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil; +import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.util.AssertUtil; +import com.alibaba.nacos.api.config.ConfigService; +import com.alibaba.nacos.api.config.ConfigType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +@Component("degradeRuleNacosPublisher") +public class DegradeRuleNacosPublisher implements DynamicRulePublisher> { + + @Autowired + private ConfigService configService; + @Autowired + private Converter, String> converter; + + @Override + public void publish(String app, List rules) throws Exception { + AssertUtil.notEmpty(app, "app name cannot be empty"); + if (rules == null) { + return; + } + String dataId = NacosConfigUtil.getDegradeRuleDataId(app); + // 发布配置到nacos + configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules), ConfigType.JSON.getType()); + } +} diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/FlowRuleApiProvider.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/flow/FlowRuleApiProvider.java similarity index 94% rename from sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/FlowRuleApiProvider.java rename to sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/flow/FlowRuleApiProvider.java index 9f9ce5cc..36e50593 100644 --- a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/FlowRuleApiProvider.java +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/flow/FlowRuleApiProvider.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.dashboard.rule; +package com.alibaba.csp.sentinel.dashboard.rule.flow; import java.util.ArrayList; import java.util.List; @@ -22,6 +22,7 @@ import java.util.stream.Collectors; import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient; import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement; import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo; +import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider; import com.alibaba.csp.sentinel.util.StringUtil; import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity; diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/FlowRuleApiPublisher.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/flow/FlowRuleApiPublisher.java similarity index 94% rename from sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/FlowRuleApiPublisher.java rename to sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/flow/FlowRuleApiPublisher.java index 9afbcf34..eae1e989 100644 --- a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/FlowRuleApiPublisher.java +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/flow/FlowRuleApiPublisher.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.dashboard.rule; +package com.alibaba.csp.sentinel.dashboard.rule.flow; import java.util.List; import java.util.Set; @@ -21,6 +21,7 @@ import java.util.Set; import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient; import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement; import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo; +import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher; import com.alibaba.csp.sentinel.util.StringUtil; import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity; 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/flow/FlowRuleNacosProvider.java similarity index 89% rename from sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosProvider.java rename to sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/flow/FlowRuleNacosProvider.java index 712132df..79562d73 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/flow/FlowRuleNacosProvider.java @@ -13,16 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.dashboard.rule.nacos; +package com.alibaba.csp.sentinel.dashboard.rule.flow; import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity; import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider; +import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil; import com.alibaba.csp.sentinel.datasource.Converter; import com.alibaba.csp.sentinel.util.StringUtil; import com.alibaba.nacos.api.config.ConfigService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; @@ -47,8 +49,7 @@ public class FlowRuleNacosProvider implements DynamicRuleProvider getRules(String appName) throws Exception { - String dataId = String.format("%s%s", NacosConfigUtil.FLOW_DATA_ID_PREFIX, appName); - + String dataId = NacosConfigUtil.getFlowRuleDataId(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/flow/FlowRuleNacosPublisher.java similarity index 81% rename from sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosPublisher.java rename to sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/flow/FlowRuleNacosPublisher.java index 35e32959..f2508961 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/flow/FlowRuleNacosPublisher.java @@ -13,10 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.csp.sentinel.dashboard.rule.nacos; +package com.alibaba.csp.sentinel.dashboard.rule.flow; import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity; import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher; +import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil; import com.alibaba.csp.sentinel.datasource.Converter; import com.alibaba.csp.sentinel.util.AssertUtil; import com.alibaba.nacos.api.config.ConfigService; @@ -44,9 +45,10 @@ public class FlowRuleNacosPublisher implements DynamicRulePublisher, String> authorityRuleEntityEncoder() { + return JSON::toJSONString; + } + @Bean + public Converter> authorityRuleEntityDecoder() { + return s -> JSON.parseArray(s, AuthorityRuleEntity.class); + } + /***************** System End ******************/ + + + /***************** System Start ******************/ + @Bean + public Converter, String> degradeRuleEntityEncoder() { + return JSON::toJSONString; + } + @Bean + public Converter> degradeRuleEntityDecoder() { + return s -> JSON.parseArray(s, DegradeRuleEntity.class); + } + /***************** System End ******************/ + + + /***************** System Start ******************/ + @Bean + public Converter, String> systemRuleEntityEncoder() { + return JSON::toJSONString; + } + @Bean + public Converter> systemRuleEntityDecoder() { + return s -> JSON.parseArray(s, SystemRuleEntity.class); + } + /***************** System End ******************/ + + + /***************** Param Start ******************/ + @Bean + public Converter, String> paramRuleEntityEncoder() { + return JSON::toJSONString; + } + @Bean + public Converter> paramRuleEntityDecoder() { + return s -> JSON.parseArray(s, ParamFlowRuleEntity.class); + } + /***************** Param End ******************/ + + + /***************** Flow Start ******************/ @Bean public Converter, String> flowRuleEntityEncoder() { return JSON::toJSONString; } - @Bean public Converter> flowRuleEntityDecoder() { return s -> JSON.parseArray(s, FlowRuleEntity.class); } + /***************** Flow End ******************/ + @Bean public ConfigService nacosConfigService() throws Exception { diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/NacosConfigUtil.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/NacosConfigUtil.java index ee25fc6f..057f7d59 100644 --- a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/NacosConfigUtil.java +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/NacosConfigUtil.java @@ -22,10 +22,14 @@ package com.alibaba.csp.sentinel.dashboard.rule.nacos; public final class NacosConfigUtil { public static final String GROUP_ID = "SENTINEL_GROUP"; + public static final String GLOBAL_DATA_ID_POSTFIX = ".json"; -// public static final String FLOW_DATA_ID_POSTFIX = "-flow-rules"; public static final String FLOW_DATA_ID_PREFIX = "flow-rules-"; - public static final String PARAM_FLOW_DATA_ID_POSTFIX = "-param-rules"; + public static final String PARAM_FLOW_DATA_ID_PREFIX = "param-rules-"; + public static final String DEGRADE_DATA_ID_PREFIX = "degrade-rules-"; + public static final String AUTHORITY_DATA_ID_PREFIX = "auth-rules-"; + public static final String SYSTEM_DATA_ID_PREFIX = "system-rules-"; + public static final String CLUSTER_MAP_DATA_ID_POSTFIX = "-cluster-map"; /** @@ -39,5 +43,26 @@ public final class NacosConfigUtil { public static final String SERVER_FLOW_CONFIG_DATA_ID_POSTFIX = "-cs-flow-config"; public static final String SERVER_NAMESPACE_SET_DATA_ID_POSTFIX = "-cs-namespace-set"; - private NacosConfigUtil() {} + private NacosConfigUtil() { + } + + public static String getSystemRuleDataId(String appName) { + return String.format("%s%s%s", NacosConfigUtil.SYSTEM_DATA_ID_PREFIX, appName, NacosConfigUtil.GLOBAL_DATA_ID_POSTFIX); + } + + public static String getAuthorityRuleDataId(String appName) { + return String.format("%s%s%s", NacosConfigUtil.AUTHORITY_DATA_ID_PREFIX, appName, NacosConfigUtil.GLOBAL_DATA_ID_POSTFIX); + } + + public static String getDegradeRuleDataId(String appName) { + return String.format("%s%s%s", NacosConfigUtil.DEGRADE_DATA_ID_PREFIX, appName, NacosConfigUtil.GLOBAL_DATA_ID_POSTFIX); + } + + public static String getParamRuleDataId(String appName) { + return String.format("%s%s%s", NacosConfigUtil.PARAM_FLOW_DATA_ID_PREFIX, appName, NacosConfigUtil.GLOBAL_DATA_ID_POSTFIX); + } + + public static String getFlowRuleDataId(String appName) { + return String.format("%s%s%s", NacosConfigUtil.FLOW_DATA_ID_PREFIX, appName, NacosConfigUtil.GLOBAL_DATA_ID_POSTFIX); + } } diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/param/ParamRuleApiProvider.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/param/ParamRuleApiProvider.java new file mode 100644 index 00000000..262e9174 --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/param/ParamRuleApiProvider.java @@ -0,0 +1,60 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.param; + +import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient; +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity; +import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement; +import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo; +import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider; +import com.alibaba.csp.sentinel.util.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +@Component("paramRuleDefaultProvider") +public class ParamRuleApiProvider implements DynamicRuleProvider> { + + @Autowired + private SentinelApiClient sentinelApiClient; + @Autowired + private AppManagement appManagement; + + @Override + public List getRules(String appName) throws Exception { + if (StringUtil.isBlank(appName)) { + return new ArrayList<>(); + } + List list = appManagement.getDetailApp(appName).getMachines() + .stream() + .filter(MachineInfo::isHealthy) + .sorted((e1, e2) -> Long.compare(e2.getLastHeartbeat(), e1.getLastHeartbeat())).collect(Collectors.toList()); + if (list.isEmpty()) { + return new ArrayList<>(); + } else { + MachineInfo machine = list.get(0); +// return sentinelApiClient.fetchFlowRuleOfMachine(machine.getApp(), machine.getIp(), machine.getPort()); + return sentinelApiClient.fetchParamFlowRulesOfMachine(machine.getApp(), machine.getIp(), machine.getPort()) +// .thenApply(repository::saveAll) +// .thenApply(Result::ofSuccess) +// .get() + .get(); + } + } +} diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/param/ParamRuleApiPublisher.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/param/ParamRuleApiPublisher.java new file mode 100644 index 00000000..a2f581b1 --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/param/ParamRuleApiPublisher.java @@ -0,0 +1,60 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.param; + +import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient; +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity; +import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement; +import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo; +import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher; +import com.alibaba.csp.sentinel.util.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Set; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +@Component("paramRuleDefaultPublisher") +public class ParamRuleApiPublisher implements DynamicRulePublisher> { + + @Autowired + private SentinelApiClient sentinelApiClient; + @Autowired + private AppManagement appManagement; + + @Override + public void publish(String app, List rules) throws Exception { + if (StringUtil.isBlank(app)) { + return; + } + if (rules == null) { + return; + } + Set set = appManagement.getDetailApp(app).getMachines(); + + for (MachineInfo machine : set) { + if (!machine.isHealthy()) { + continue; + } + // TODO: parse the results + sentinelApiClient.setParamFlowRuleOfMachine(app, machine.getIp(), machine.getPort(), rules); + } + } +} diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/param/ParamRuleNacosProvider.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/param/ParamRuleNacosProvider.java new file mode 100644 index 00000000..c20e1086 --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/param/ParamRuleNacosProvider.java @@ -0,0 +1,58 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.param; + +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity; +import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider; +import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil; +import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.util.StringUtil; +import com.alibaba.nacos.api.config.ConfigService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +@Component("paramRuleNacosProvider") +public class ParamRuleNacosProvider implements DynamicRuleProvider> { + + @Autowired + private ConfigService configService; + @Autowired + private Converter> converter; + + /** + * 获取规则 + * + * @param appName 应用名称 + * @return 规则列表 + * @throws Exception 异常 + */ + @Override + public List getRules(String appName) throws Exception { + String dataId = NacosConfigUtil.getParamRuleDataId(appName); + String rules = configService.getConfig(dataId, NacosConfigUtil.GROUP_ID, 3000); + if (StringUtil.isEmpty(rules)) { + return new ArrayList<>(); + } + return converter.convert(rules); + } +} diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/param/ParamRuleNacosPublisher.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/param/ParamRuleNacosPublisher.java new file mode 100644 index 00000000..4183442a --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/param/ParamRuleNacosPublisher.java @@ -0,0 +1,52 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.param; + +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity; +import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher; +import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil; +import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.util.AssertUtil; +import com.alibaba.nacos.api.config.ConfigService; +import com.alibaba.nacos.api.config.ConfigType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +@Component("paramRuleNacosPublisher") +public class ParamRuleNacosPublisher implements DynamicRulePublisher> { + + @Autowired + private ConfigService configService; + @Autowired + private Converter, String> converter; + + @Override + public void publish(String app, List rules) throws Exception { + AssertUtil.notEmpty(app, "app name cannot be empty"); + if (rules == null) { + return; + } + String dataId = NacosConfigUtil.getParamRuleDataId(app); + // 发布配置到nacos + configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules),ConfigType.JSON.getType()); + } +} diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/system/SystemRuleApiProvider.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/system/SystemRuleApiProvider.java new file mode 100644 index 00000000..5fc56fac --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/system/SystemRuleApiProvider.java @@ -0,0 +1,55 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.system; + +import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient; +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity; +import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement; +import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo; +import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider; +import com.alibaba.csp.sentinel.util.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +@Component("systemRuleDefaultProvider") +public class SystemRuleApiProvider implements DynamicRuleProvider> { + + @Autowired + private SentinelApiClient sentinelApiClient; + @Autowired + private AppManagement appManagement; + + @Override + public List getRules(String appName) throws Exception { + if (StringUtil.isBlank(appName)) { + return new ArrayList<>(); + } + List list = appManagement.getDetailApp(appName).getMachines() + .stream() + .filter(MachineInfo::isHealthy) + .sorted((e1, e2) -> Long.compare(e2.getLastHeartbeat(), e1.getLastHeartbeat())).collect(Collectors.toList()); + if (list.isEmpty()) { + return new ArrayList<>(); + } else { + MachineInfo machine = list.get(0); + return sentinelApiClient.fetchSystemRuleOfMachine(machine.getApp(), machine.getIp(), machine.getPort()); + } + } +} diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/system/SystemRuleApiPublisher.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/system/SystemRuleApiPublisher.java new file mode 100644 index 00000000..82123168 --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/system/SystemRuleApiPublisher.java @@ -0,0 +1,60 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.system; + +import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient; +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity; +import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement; +import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo; +import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher; +import com.alibaba.csp.sentinel.util.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Set; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +@Component("systemRuleDefaultPublisher") +public class SystemRuleApiPublisher implements DynamicRulePublisher> { + + @Autowired + private SentinelApiClient sentinelApiClient; + @Autowired + private AppManagement appManagement; + + @Override + public void publish(String app, List rules) throws Exception { + if (StringUtil.isBlank(app)) { + return; + } + if (rules == null) { + return; + } + Set set = appManagement.getDetailApp(app).getMachines(); + + for (MachineInfo machine : set) { + if (!machine.isHealthy()) { + continue; + } + // TODO: parse the results + sentinelApiClient.setSystemRuleOfMachine(app, machine.getIp(), machine.getPort(), rules); + } + } +} diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/system/SystemRuleNacosProvider.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/system/SystemRuleNacosProvider.java new file mode 100644 index 00000000..bca00f06 --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/system/SystemRuleNacosProvider.java @@ -0,0 +1,58 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.system; + +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity; +import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider; +import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil; +import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.util.StringUtil; +import com.alibaba.nacos.api.config.ConfigService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +@Component("systemRuleNacosProvider") +public class SystemRuleNacosProvider implements DynamicRuleProvider> { + + @Autowired + private ConfigService configService; + @Autowired + private Converter> converter; + + /** + * 获取规则 + * + * @degrade appName 应用名称 + * @return 规则列表 + * @throws Exception 异常 + */ + @Override + public List getRules(String appName) throws Exception { + String dataId = NacosConfigUtil.getSystemRuleDataId(appName); + String rules = configService.getConfig(dataId, NacosConfigUtil.GROUP_ID, 3000); + if (StringUtil.isEmpty(rules)) { + return new ArrayList<>(); + } + return converter.convert(rules); + } +} diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/system/SystemRuleNacosPublisher.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/system/SystemRuleNacosPublisher.java new file mode 100644 index 00000000..9b3c61bc --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/system/SystemRuleNacosPublisher.java @@ -0,0 +1,52 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.system; + +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity; +import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher; +import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil; +import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.util.AssertUtil; +import com.alibaba.nacos.api.config.ConfigService; +import com.alibaba.nacos.api.config.ConfigType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @author Eric Zhao + * @since 1.4.0 + */ +@Component("systemRuleNacosPublisher") +public class SystemRuleNacosPublisher implements DynamicRulePublisher> { + + @Autowired + private ConfigService configService; + @Autowired + private Converter, String> converter; + + @Override + public void publish(String app, List rules) throws Exception { + AssertUtil.notEmpty(app, "app name cannot be empty"); + if (rules == null) { + return; + } + String dataId = NacosConfigUtil.getSystemRuleDataId(app); + // 发布配置到nacos + configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules), ConfigType.JSON.getType()); + } +}