refactor(rule): 重构规则发布与配置逻辑
- 将规则发布类统一移至 publisher 包下,优化包结构 - 更新 Nacos 配置项命名及分组策略,增强可读性与一致性 - 重写规则转换逻辑,确保推送数据结构准确无误 - 添加推送结果日志记录,提升调试与监控能力 - 修复控制器中规则类型描述不准确的问题 - 补充 API 定义相关 Provider 和 Publisher 实现 - 清理冗余代码与注释,提高代码整洁度 - 调整格式与导入顺序,符合编码规范 - 移除不必要的 converter bean 定义,简化配置类 - 修改应用配置文件中的 Nacos 用户名密码为空,适应新部署需求
This commit is contained in:
parent
c48e742041
commit
04d1e82b8c
|
|
@ -23,8 +23,7 @@ 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.dashboard.rule.publisher.AuthorityRuleNacosPublisher;
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
|
||||
|
|
@ -198,9 +197,9 @@ public class AuthorityRuleController {
|
|||
List<AuthorityRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
|
||||
try {
|
||||
publisher.publish(app, rules);
|
||||
logger.info("限流规则推送到nacos完成,app=[{}], ip=[{}]", app, ip);
|
||||
logger.info("[{}]规则推送到nacos完成,app=[{}], ip=[{}]","授权", app, ip);
|
||||
} catch (Exception e) {
|
||||
logger.error("限流规则发布失败, app=[{}], ip=[{}]", app, ip, e);
|
||||
logger.error("[{}]规则发布失败, app=[{}], ip=[{}]", "授权",app, ip, e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return sentinelApiClient.setAuthorityRuleOfMachine(app, ip, port, rules);
|
||||
|
|
|
|||
|
|
@ -24,8 +24,7 @@ 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.dashboard.rule.publisher.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;
|
||||
|
|
@ -65,6 +64,7 @@ public class DegradeController {
|
|||
private AppManagement appManagement;
|
||||
@Autowired
|
||||
private DegradeRuleNacosPublisher publisher;
|
||||
|
||||
@GetMapping("/rules.json")
|
||||
@AuthAction(PrivilegeType.READ_RULE)
|
||||
public Result<List<DegradeRuleEntity>> apiQueryMachineRules(String app, String ip, Integer port) {
|
||||
|
|
@ -115,7 +115,7 @@ public class DegradeController {
|
|||
@PutMapping("/rule/{id}")
|
||||
@AuthAction(PrivilegeType.WRITE_RULE)
|
||||
public Result<DegradeRuleEntity> apiUpdateRule(@PathVariable("id") Long id,
|
||||
@RequestBody DegradeRuleEntity entity) {
|
||||
@RequestBody DegradeRuleEntity entity) {
|
||||
if (id == null || id <= 0) {
|
||||
return Result.ofFail(-1, "id can't be null or negative");
|
||||
}
|
||||
|
|
@ -174,9 +174,9 @@ public class DegradeController {
|
|||
List<DegradeRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
|
||||
try {
|
||||
publisher.publish(app, rules);
|
||||
logger.info("限流规则推送到nacos完成,app=[{}], ip=[{}]", app, ip);
|
||||
logger.info("[{}]规则推送到nacos完成,app=[{}], ip=[{}]", "熔断", app, ip);
|
||||
} catch (Exception e) {
|
||||
logger.error("限流规则发布失败, app=[{}], ip=[{}]", app, ip, e);
|
||||
logger.error("[{}]规则发布失败, app=[{}], ip=[{}]", "熔断", app, ip, e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return sentinelApiClient.setDegradeRuleOfMachine(app, ip, port, rules);
|
||||
|
|
@ -214,10 +214,10 @@ public class DegradeController {
|
|||
return Result.ofFail(-1, "circuit breaker strategy cannot be null");
|
||||
}
|
||||
if (strategy < CircuitBreakerStrategy.SLOW_REQUEST_RATIO.getType()
|
||||
|| strategy > RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT) {
|
||||
|| strategy > RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT) {
|
||||
return Result.ofFail(-1, "Invalid circuit breaker strategy: " + strategy);
|
||||
}
|
||||
if (entity.getMinRequestAmount() == null || entity.getMinRequestAmount() <= 0) {
|
||||
if (entity.getMinRequestAmount() == null || entity.getMinRequestAmount() <= 0) {
|
||||
return Result.ofFail(-1, "Invalid minRequestAmount");
|
||||
}
|
||||
if (entity.getStatIntervalMs() == null || entity.getStatIntervalMs() <= 0) {
|
||||
|
|
|
|||
|
|
@ -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.flow.FlowRuleNacosPublisher;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.publisher.FlowRuleNacosPublisher;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
|
||||
|
|
@ -283,9 +283,9 @@ public class FlowControllerV1 {
|
|||
List<FlowRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
|
||||
try {
|
||||
publisher.publish(app, rules);
|
||||
logger.info("限流规则推送到nacos完成,app=[{}], ip=[{}]", app, ip);
|
||||
logger.info("[{}]规则推送到nacos完成,app=[{}], ip=[{}]", "流控", app, ip);
|
||||
} catch (Exception e) {
|
||||
logger.error("限流规则发布失败, app=[{}], ip=[{}]", app, ip, e);
|
||||
logger.error("[{}]规则发布失败, app=[{}], ip=[{}]", "流控", app, ip, e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return sentinelApiClient.setFlowRuleOfMachineAsync(app, ip, port, rules);
|
||||
|
|
|
|||
|
|
@ -28,8 +28,7 @@ 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.dashboard.rule.publisher.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;
|
||||
|
|
@ -69,13 +68,14 @@ public class ParamFlowRuleController {
|
|||
private RuleRepository<ParamFlowRuleEntity, Long> repository;
|
||||
@Autowired
|
||||
private ParamRuleNacosPublisher publisher;
|
||||
|
||||
private boolean checkIfSupported(String app, String ip, int port) {
|
||||
try {
|
||||
return Optional.ofNullable(appManagement.getDetailApp(app))
|
||||
.flatMap(e -> e.getMachine(ip, port))
|
||||
.flatMap(m -> VersionUtils.parseVersion(m.getVersion())
|
||||
.map(v -> v.greaterOrEqual(version020)))
|
||||
.orElse(true);
|
||||
.flatMap(e -> e.getMachine(ip, port))
|
||||
.flatMap(m -> VersionUtils.parseVersion(m.getVersion())
|
||||
.map(v -> v.greaterOrEqual(version020)))
|
||||
.orElse(true);
|
||||
// If error occurred or cannot retrieve machine info, return true.
|
||||
} catch (Exception ex) {
|
||||
return true;
|
||||
|
|
@ -104,9 +104,9 @@ public class ParamFlowRuleController {
|
|||
}
|
||||
try {
|
||||
return sentinelApiClient.fetchParamFlowRulesOfMachine(app, ip, port)
|
||||
.thenApply(repository::saveAll)
|
||||
.thenApply(Result::ofSuccess)
|
||||
.get();
|
||||
.thenApply(repository::saveAll)
|
||||
.thenApply(Result::ofSuccess)
|
||||
.get();
|
||||
} catch (ExecutionException ex) {
|
||||
logger.error("Error when querying parameter flow rules", ex.getCause());
|
||||
if (isNotSupported(ex.getCause())) {
|
||||
|
|
@ -265,9 +265,9 @@ public class ParamFlowRuleController {
|
|||
List<ParamFlowRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
|
||||
try {
|
||||
publisher.publish(app, rules);
|
||||
logger.info("限流规则推送到nacos完成,app=[{}], ip=[{}]", app, ip);
|
||||
logger.info("[{}]规则推送到nacos完成,app=[{}], ip=[{}]", "热点参数", app, ip);
|
||||
} catch (Exception e) {
|
||||
logger.error("限流规则发布失败, app=[{}], ip=[{}]", app, ip, e);
|
||||
logger.error("[{}]限流规则发布失败, app=[{}], ip=[{}]", "热点参数", app, ip, e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return sentinelApiClient.setParamFlowRuleOfMachine(app, ip, port, rules);
|
||||
|
|
@ -275,7 +275,7 @@ public class ParamFlowRuleController {
|
|||
|
||||
private <R> Result<R> unsupportedVersion() {
|
||||
return Result.ofFail(4041,
|
||||
"Sentinel client not supported for parameter flow control (unsupported version or dependency absent)");
|
||||
"Sentinel client not supported for parameter flow control (unsupported version or dependency absent)");
|
||||
}
|
||||
|
||||
private final SentinelVersion version020 = new SentinelVersion().setMinorVersion(2);
|
||||
|
|
|
|||
|
|
@ -22,8 +22,7 @@ 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.dashboard.rule.publisher.SystemRuleNacosPublisher;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity;
|
||||
|
|
@ -55,6 +54,7 @@ public class SystemController {
|
|||
private AppManagement appManagement;
|
||||
@Autowired
|
||||
private SystemRuleNacosPublisher publisher;
|
||||
|
||||
private <R> Result<R> checkBasicParams(String app, String ip, Integer port) {
|
||||
if (StringUtil.isEmpty(app)) {
|
||||
return Result.ofFail(-1, "app can't be null or empty");
|
||||
|
|
@ -116,7 +116,7 @@ public class SystemController {
|
|||
int notNullCount = countNotNullAndNotNegative(highestSystemLoad, avgRt, maxThread, qps, highestCpuUsage);
|
||||
if (notNullCount != 1) {
|
||||
return Result.ofFail(-1, "only one of [highestSystemLoad, avgRt, maxThread, qps,highestCpuUsage] "
|
||||
+ "value must be set > 0, but " + notNullCount + " values get");
|
||||
+ "value must be set > 0, but " + notNullCount + " values get");
|
||||
}
|
||||
if (null != highestCpuUsage && highestCpuUsage > 1) {
|
||||
return Result.ofFail(-1, "highestCpuUsage must between [0.0, 1.0]");
|
||||
|
|
@ -171,7 +171,7 @@ public class SystemController {
|
|||
@GetMapping("/save.json")
|
||||
@AuthAction(PrivilegeType.WRITE_RULE)
|
||||
public Result<SystemRuleEntity> apiUpdateIfNotNull(Long id, String app, Double highestSystemLoad,
|
||||
Double highestCpuUsage, Long avgRt, Long maxThread, Double qps) {
|
||||
Double highestCpuUsage, Long avgRt, Long maxThread, Double qps) {
|
||||
if (id == null) {
|
||||
return Result.ofFail(-1, "id can't be null");
|
||||
}
|
||||
|
|
@ -256,9 +256,9 @@ public class SystemController {
|
|||
List<SystemRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
|
||||
try {
|
||||
publisher.publish(app, rules);
|
||||
logger.info("限流规则推送到nacos完成,app=[{}], ip=[{}]", app, ip);
|
||||
logger.info("[{}]规则推送到nacos完成,app=[{}], ip=[{}]", "系统", app, ip);
|
||||
} catch (Exception e) {
|
||||
logger.error("限流规则发布失败, app=[{}], ip=[{}]", app, ip, e);
|
||||
logger.error("[{}]规则发布失败, app=[{}], ip=[{}]", "系统", app, ip, e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return sentinelApiClient.setSystemRuleOfMachine(app, ip, port, rules);
|
||||
|
|
|
|||
|
|
@ -26,6 +26,8 @@ import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.AddApiReqVo;
|
|||
import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.ApiPredicateItemVo;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.UpdateApiReqVo;
|
||||
import com.alibaba.csp.sentinel.dashboard.repository.gateway.InMemApiDefinitionStore;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.publisher.ApiDefinitionNacosPublisher;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.publisher.AuthorityRuleNacosPublisher;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
|
@ -56,6 +58,9 @@ public class GatewayApiController {
|
|||
@Autowired
|
||||
private SentinelApiClient sentinelApiClient;
|
||||
|
||||
@Autowired
|
||||
private ApiDefinitionNacosPublisher publisher;
|
||||
|
||||
@GetMapping("/list.json")
|
||||
@AuthAction(AuthService.PrivilegeType.READ_RULE)
|
||||
public Result<List<ApiDefinitionEntity>> queryApis(String app, String ip, Integer port) {
|
||||
|
|
@ -255,6 +260,13 @@ public class GatewayApiController {
|
|||
|
||||
private boolean publishApis(String app, String ip, Integer port) {
|
||||
List<ApiDefinitionEntity> apis = repository.findAllByMachine(MachineInfo.of(app, ip, port));
|
||||
try {
|
||||
publisher.publish(app, apis);
|
||||
logger.info("[{}]规则推送到nacos完成,app=[{}], ip=[{}]", "API定义", app, ip);
|
||||
} catch (Exception e) {
|
||||
logger.error("[{}]限流规则发布失败, app=[{}], ip=[{}]", "API定义", app, ip, e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return sentinelApiClient.modifyApis(app, ip, port, apis);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,6 +27,8 @@ import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.AddFlowRuleReqV
|
|||
import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.GatewayParamFlowItemVo;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.UpdateFlowRuleReqVo;
|
||||
import com.alibaba.csp.sentinel.dashboard.repository.gateway.InMemGatewayFlowRuleStore;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.publisher.ApiDefinitionNacosPublisher;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.publisher.GatewayRuleNacosPublisher;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
|
@ -59,6 +61,9 @@ public class GatewayFlowRuleController {
|
|||
@Autowired
|
||||
private SentinelApiClient sentinelApiClient;
|
||||
|
||||
@Autowired
|
||||
private GatewayRuleNacosPublisher publisher;
|
||||
|
||||
@GetMapping("/list.json")
|
||||
@AuthAction(AuthService.PrivilegeType.READ_RULE)
|
||||
public Result<List<GatewayFlowRuleEntity>> queryFlowRules(String app, String ip, Integer port) {
|
||||
|
|
@ -426,6 +431,13 @@ public class GatewayFlowRuleController {
|
|||
|
||||
private boolean publishRules(String app, String ip, Integer port) {
|
||||
List<GatewayFlowRuleEntity> 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.modifyGatewayFlowRules(app, ip, port, rules);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,105 @@
|
|||
package com.alibaba.csp.sentinel.dashboard.rule;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.*;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class Converters {
|
||||
|
||||
/***************** API Start ******************/
|
||||
@Bean
|
||||
public Converter<List<ApiDefinitionEntity>, String> apiRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<ApiDefinitionEntity>> apiEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, ApiDefinitionEntity.class);
|
||||
}
|
||||
/***************** API End ******************/
|
||||
|
||||
/***************** Gateway Start ******************/
|
||||
@Bean
|
||||
public Converter<List<GatewayFlowRuleEntity>, String> gatewayRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<GatewayFlowRuleEntity>> gatewayRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, GatewayFlowRuleEntity.class);
|
||||
}
|
||||
/***************** Gateway End ******************/
|
||||
|
||||
/***************** System Start ******************/
|
||||
@Bean
|
||||
public Converter<List<AuthorityRuleEntity>, String> authorityRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<AuthorityRuleEntity>> authorityRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, AuthorityRuleEntity.class);
|
||||
}
|
||||
/***************** System End ******************/
|
||||
|
||||
|
||||
/***************** System Start ******************/
|
||||
@Bean
|
||||
public Converter<List<DegradeRuleEntity>, String> degradeRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<DegradeRuleEntity>> degradeRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, DegradeRuleEntity.class);
|
||||
}
|
||||
/***************** System End ******************/
|
||||
|
||||
|
||||
/***************** System Start ******************/
|
||||
@Bean
|
||||
public Converter<List<SystemRuleEntity>, String> systemRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<SystemRuleEntity>> systemRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, SystemRuleEntity.class);
|
||||
}
|
||||
/***************** System End ******************/
|
||||
|
||||
|
||||
/***************** Param Start ******************/
|
||||
@Bean
|
||||
public Converter<List<ParamFlowRuleEntity>, String> paramRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<ParamFlowRuleEntity>> paramRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, ParamFlowRuleEntity.class);
|
||||
}
|
||||
/***************** Param End ******************/
|
||||
|
||||
|
||||
/***************** Flow Start ******************/
|
||||
@Bean
|
||||
public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, FlowRuleEntity.class);
|
||||
}
|
||||
|
||||
/***************** Flow End ******************/
|
||||
|
||||
}
|
||||
|
|
@ -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.api;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
|
||||
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.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
|
||||
public class ApiDefinitionApiProvider implements DynamicRuleProvider<List<ApiDefinitionEntity>> {
|
||||
|
||||
@Autowired
|
||||
private SentinelApiClient sentinelApiClient;
|
||||
@Autowired
|
||||
private AppManagement appManagement;
|
||||
|
||||
@Override
|
||||
public List<ApiDefinitionEntity> getRules(String appName) throws Exception {
|
||||
if (StringUtil.isBlank(appName)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
List<MachineInfo> 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.fetchApis(machine.getApp(), machine.getIp(), machine.getPort()).get();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* 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.api;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
|
||||
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;
|
||||
|
||||
@Component
|
||||
public class ApiDefinitionApiPublisher implements DynamicRulePublisher<List<ApiDefinitionEntity>> {
|
||||
|
||||
@Autowired
|
||||
private SentinelApiClient sentinelApiClient;
|
||||
@Autowired
|
||||
private AppManagement appManagement;
|
||||
|
||||
@Override
|
||||
public void publish(String app, List<ApiDefinitionEntity> rules) throws Exception {
|
||||
if (StringUtil.isBlank(app)) {
|
||||
return;
|
||||
}
|
||||
if (rules == null) {
|
||||
return;
|
||||
}
|
||||
Set<MachineInfo> set = appManagement.getDetailApp(app).getMachines();
|
||||
|
||||
for (MachineInfo machine : set) {
|
||||
if (!machine.isHealthy()) {
|
||||
continue;
|
||||
}
|
||||
// TODO: parse the results
|
||||
sentinelApiClient.modifyApis(app, machine.getIp(), machine.getPort(), rules);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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.api;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
|
||||
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;
|
||||
|
||||
@Component
|
||||
public class ApiDefinitionNacosProvider implements DynamicRuleProvider<List<ApiDefinitionEntity>> {
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<String, List<ApiDefinitionEntity>> converter;
|
||||
|
||||
/**
|
||||
* 获取规则
|
||||
*
|
||||
* @return 规则列表
|
||||
* @throws Exception 异常
|
||||
* @degrade appName 应用名称
|
||||
*/
|
||||
@Override
|
||||
public List<ApiDefinitionEntity> getRules(String appName) throws Exception {
|
||||
String dataId = NacosConfigUtil.getApiDefinitionDataId(appName);
|
||||
String rules = configService.getConfig(dataId, NacosConfigUtil.GROUP_ID, 3000);
|
||||
if (StringUtil.isEmpty(rules)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return converter.convert(rules);
|
||||
}
|
||||
}
|
||||
|
|
@ -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.gateway;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity;
|
||||
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.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("gatewayRuleDefaultProvider")
|
||||
public class GatewayRuleApiProvider implements DynamicRuleProvider<List<GatewayFlowRuleEntity>> {
|
||||
|
||||
@Autowired
|
||||
private SentinelApiClient sentinelApiClient;
|
||||
@Autowired
|
||||
private AppManagement appManagement;
|
||||
|
||||
@Override
|
||||
public List<GatewayFlowRuleEntity> getRules(String appName) throws Exception {
|
||||
if (StringUtil.isBlank(appName)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
List<MachineInfo> 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.fetchGatewayFlowRules(machine.getApp(), machine.getIp(), machine.getPort()).get();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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.gateway;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity;
|
||||
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("gatewayRuleDefaultPublisher")
|
||||
public class GatewayRuleApiPublisher implements DynamicRulePublisher<List<GatewayFlowRuleEntity>> {
|
||||
|
||||
@Autowired
|
||||
private SentinelApiClient sentinelApiClient;
|
||||
@Autowired
|
||||
private AppManagement appManagement;
|
||||
|
||||
@Override
|
||||
public void publish(String app, List<GatewayFlowRuleEntity> rules) throws Exception {
|
||||
if (StringUtil.isBlank(app)) {
|
||||
return;
|
||||
}
|
||||
if (rules == null) {
|
||||
return;
|
||||
}
|
||||
Set<MachineInfo> set = appManagement.getDetailApp(app).getMachines();
|
||||
|
||||
for (MachineInfo machine : set) {
|
||||
if (!machine.isHealthy()) {
|
||||
continue;
|
||||
}
|
||||
// TODO: parse the results
|
||||
sentinelApiClient.modifyGatewayFlowRules(app, machine.getIp(), machine.getPort(), rules);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* 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.gateway;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity;
|
||||
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("gatewayRuleNacosProvider")
|
||||
public class GatewayRuleNacosProvider implements DynamicRuleProvider<List<GatewayFlowRuleEntity>> {
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<String, List<GatewayFlowRuleEntity>> converter;
|
||||
|
||||
/**
|
||||
* 获取规则
|
||||
*
|
||||
* @degrade appName 应用名称
|
||||
* @return 规则列表
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
@Override
|
||||
public List<GatewayFlowRuleEntity> 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -15,6 +15,8 @@
|
|||
*/
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.*;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
|
@ -38,72 +40,6 @@ public class NacosConfig {
|
|||
@Autowired
|
||||
private NacosServerConfig config;
|
||||
|
||||
/***************** System Start ******************/
|
||||
@Bean
|
||||
public Converter<List<AuthorityRuleEntity>, String> authorityRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<AuthorityRuleEntity>> authorityRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, AuthorityRuleEntity.class);
|
||||
}
|
||||
/***************** System End ******************/
|
||||
|
||||
|
||||
/***************** System Start ******************/
|
||||
@Bean
|
||||
public Converter<List<DegradeRuleEntity>, String> degradeRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<DegradeRuleEntity>> degradeRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, DegradeRuleEntity.class);
|
||||
}
|
||||
/***************** System End ******************/
|
||||
|
||||
|
||||
/***************** System Start ******************/
|
||||
@Bean
|
||||
public Converter<List<SystemRuleEntity>, String> systemRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<SystemRuleEntity>> systemRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, SystemRuleEntity.class);
|
||||
}
|
||||
/***************** System End ******************/
|
||||
|
||||
|
||||
/***************** Param Start ******************/
|
||||
@Bean
|
||||
public Converter<List<ParamFlowRuleEntity>, String> paramRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<ParamFlowRuleEntity>> paramRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, ParamFlowRuleEntity.class);
|
||||
}
|
||||
/***************** Param End ******************/
|
||||
|
||||
|
||||
/***************** Flow Start ******************/
|
||||
@Bean
|
||||
public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, FlowRuleEntity.class);
|
||||
}
|
||||
|
||||
/***************** Flow End ******************/
|
||||
|
||||
|
||||
@Bean
|
||||
public ConfigService nacosConfigService() throws Exception {
|
||||
// 创建Nacos配置服务
|
||||
|
|
|
|||
|
|
@ -16,29 +16,31 @@
|
|||
package com.alibaba.csp.sentinel.dashboard.rule.nacos;
|
||||
|
||||
/**
|
||||
* @author Eric Zhao
|
||||
* @since 1.4.0
|
||||
* @author Ms.He
|
||||
* 2025-11-21 07:37
|
||||
* e-mail crabapples.cn@gmail.com
|
||||
* qq 294046317
|
||||
* pc-name mshe
|
||||
*/
|
||||
public final class NacosConfigUtil {
|
||||
|
||||
// public static final String GROUP_ID = "SENTINEL_GROUP";
|
||||
public static final String GROUP_ID = "DEFAULT_GROUP";
|
||||
public static final String GLOBAL_DATA_ID_POSTFIX = ".json";
|
||||
|
||||
// 流控规则
|
||||
public static final String FLOW_DATA_ID_PREFIX = "flow-";
|
||||
public static final String FLOW_DATA_ID_POSTFIX = "-FLOW";
|
||||
// 热点参数规则
|
||||
public static final String PARAM_FLOW_DATA_ID_PREFIX = "param-";
|
||||
public static final String PARAM_FLOW_DATA_ID_POSTFIX = "-PARAM_FLOW";
|
||||
// 熔断规则
|
||||
public static final String DEGRADE_DATA_ID_PREFIX = "degrade-";
|
||||
public static final String DEGRADE_DATA_ID_POSTFIX = "-DEGRADE";
|
||||
// 授权规则
|
||||
public static final String AUTHORITY_DATA_ID_PREFIX = "auth-";
|
||||
public static final String AUTHORITY_DATA_ID_POSTFIX = "-AUTHORITY_FLOW";
|
||||
// 系统规则
|
||||
public static final String SYSTEM_DATA_ID_PREFIX = "system-";
|
||||
public static final String SYSTEM_DATA_ID_POSTFIX = "-SYSTEM";
|
||||
// api分组
|
||||
public static final String API_GROUP_DATA_ID_PREFIX = "api-group-";
|
||||
public static final String API_DEFINITION_DATA_ID_POSTFIX = "-API_DEFINITION";
|
||||
// 网关流控规则
|
||||
public static final String GATEWAY_FLOW_DATA_ID_PREFIX = "gateway-flow-";
|
||||
public static final String GATEWAY_FLOW_DATA_ID_POSTFIX = "-GATEWAY_FLOW";
|
||||
|
||||
public static final String CLUSTER_MAP_DATA_ID_POSTFIX = "-cluster-map";
|
||||
|
||||
|
|
@ -57,34 +59,33 @@ public final class NacosConfigUtil {
|
|||
}
|
||||
|
||||
|
||||
public static String getApiGroupDataId(String appName) {
|
||||
return String.format("%s%s%s", NacosConfigUtil.API_GROUP_DATA_ID_PREFIX, appName, NacosConfigUtil.GLOBAL_DATA_ID_POSTFIX);
|
||||
public static String getApiDefinitionDataId(String appName) {
|
||||
return String.format("%s-%s%s", API_DEFINITION_DATA_ID_POSTFIX, appName, GLOBAL_DATA_ID_POSTFIX);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static String getGatewayFlowDataId(String appName) {
|
||||
return String.format("%s%s%s", NacosConfigUtil.GATEWAY_FLOW_DATA_ID_PREFIX, appName, NacosConfigUtil.GLOBAL_DATA_ID_POSTFIX);
|
||||
return String.format("%s-%s%s", GATEWAY_FLOW_DATA_ID_POSTFIX, appName, GLOBAL_DATA_ID_POSTFIX);
|
||||
}
|
||||
|
||||
|
||||
public static String getSystemRuleDataId(String appName) {
|
||||
return String.format("%s%s%s", NacosConfigUtil.SYSTEM_DATA_ID_PREFIX, appName, NacosConfigUtil.GLOBAL_DATA_ID_POSTFIX);
|
||||
return String.format("%s-%s%s", appName, SYSTEM_DATA_ID_POSTFIX, 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);
|
||||
return String.format("%s-%s%s", appName, AUTHORITY_DATA_ID_POSTFIX, 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);
|
||||
return String.format("%s-%s%s", appName, DEGRADE_DATA_ID_POSTFIX, 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);
|
||||
return String.format("%s-%s%s", appName, PARAM_FLOW_DATA_ID_POSTFIX, 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);
|
||||
return String.format("%s-%s%s", appName, FLOW_DATA_ID_POSTFIX, GLOBAL_DATA_ID_POSTFIX);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,40 +13,39 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.param;
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.publisher;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
|
||||
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.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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<List<ParamFlowRuleEntity>> {
|
||||
@Component
|
||||
public class ApiDefinitionNacosPublisher implements DynamicRulePublisher<List<ApiDefinitionEntity>> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ApiDefinitionNacosPublisher.class);
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<List<ParamFlowRuleEntity>, String> converter;
|
||||
private Converter<List<ApiDefinitionEntity>, String> converter;
|
||||
|
||||
@Override
|
||||
public void publish(String app, List<ParamFlowRuleEntity> rules) throws Exception {
|
||||
public void publish(String app, List<ApiDefinitionEntity> 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());
|
||||
String dataId = NacosConfigUtil.getApiDefinitionDataId(app);
|
||||
boolean status = configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules), ConfigType.JSON.getType());
|
||||
logger.info("配置文件:[{}],推送结果:[{}]", dataId, status);
|
||||
}
|
||||
}
|
||||
|
|
@ -13,27 +13,28 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.auth;
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.publisher;
|
||||
|
||||
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.slots.block.authority.AuthorityRule;
|
||||
import com.alibaba.csp.sentinel.util.AssertUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.config.ConfigType;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author Eric Zhao
|
||||
* @since 1.4.0
|
||||
*/
|
||||
@Component("authorityRuleNacosPublisher")
|
||||
@Component
|
||||
public class AuthorityRuleNacosPublisher implements DynamicRulePublisher<List<AuthorityRuleEntity>> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(AuthorityRuleNacosPublisher.class);
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
|
|
@ -47,7 +48,15 @@ public class AuthorityRuleNacosPublisher implements DynamicRulePublisher<List<Au
|
|||
return;
|
||||
}
|
||||
String dataId = NacosConfigUtil.getAuthorityRuleDataId(app);
|
||||
// 发布配置到nacos
|
||||
configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules), ConfigType.JSON.getType());
|
||||
List<AuthorityRule> collect = rules.stream().map(entity -> {
|
||||
AuthorityRule rule = new AuthorityRule();
|
||||
rule.setResource(entity.getResource());
|
||||
rule.setLimitApp(entity.getLimitApp());
|
||||
rule.setStrategy(entity.getStrategy());
|
||||
return rule;
|
||||
}).collect(Collectors.toList());
|
||||
String convert = JSON.toJSONString(collect);
|
||||
boolean status = configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, convert, ConfigType.JSON.getType());
|
||||
logger.info("配置文件:[{}],推送结果:[{}]", dataId, status);
|
||||
}
|
||||
}
|
||||
|
|
@ -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.degrade;
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.publisher;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
||||
|
|
@ -22,17 +22,16 @@ 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.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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")
|
||||
@Component
|
||||
public class DegradeRuleNacosPublisher implements DynamicRulePublisher<List<DegradeRuleEntity>> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(DegradeRuleNacosPublisher.class);
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
|
|
@ -46,7 +45,7 @@ public class DegradeRuleNacosPublisher implements DynamicRulePublisher<List<Degr
|
|||
return;
|
||||
}
|
||||
String dataId = NacosConfigUtil.getDegradeRuleDataId(app);
|
||||
// 发布配置到nacos
|
||||
configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules), ConfigType.JSON.getType());
|
||||
boolean status = configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules), ConfigType.JSON.getType());
|
||||
logger.info("配置文件:[{}],推送结果:[{}]", dataId, status);
|
||||
}
|
||||
}
|
||||
|
|
@ -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.flow;
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.publisher;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
||||
|
|
@ -22,17 +22,16 @@ 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.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Eric Zhao
|
||||
* @since 1.4.0
|
||||
*/
|
||||
@Component("flowRuleNacosPublisher")
|
||||
@Component
|
||||
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(FlowRuleNacosPublisher.class);
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
|
|
@ -46,9 +45,7 @@ public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRul
|
|||
return;
|
||||
}
|
||||
String dataId = NacosConfigUtil.getFlowRuleDataId(app);
|
||||
// 发布配置到nacos
|
||||
configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules), ConfigType.JSON.getType());
|
||||
// 需要添加类型,默认为TEXT
|
||||
// configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules));
|
||||
boolean status = configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules), ConfigType.JSON.getType());
|
||||
logger.info("配置文件:[{}],推送结果:[{}]", dataId, status);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* 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.publisher;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity;
|
||||
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.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class GatewayRuleNacosPublisher implements DynamicRulePublisher<List<GatewayFlowRuleEntity>> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(GatewayRuleNacosPublisher.class);
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<List<GatewayFlowRuleEntity>, String> converter;
|
||||
|
||||
@Override
|
||||
public void publish(String app, List<GatewayFlowRuleEntity> rules) throws Exception {
|
||||
AssertUtil.notEmpty(app, "app name cannot be empty");
|
||||
if (rules == null) {
|
||||
return;
|
||||
}
|
||||
String dataId = NacosConfigUtil.getGatewayFlowDataId(app);
|
||||
boolean status = configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules), ConfigType.JSON.getType());
|
||||
logger.info("配置文件:[{}],推送结果:[{}]", dataId, status);
|
||||
}
|
||||
}
|
||||
|
|
@ -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.system;
|
||||
package com.alibaba.csp.sentinel.dashboard.rule.publisher;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
||||
|
|
@ -22,17 +22,16 @@ 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.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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")
|
||||
@Component
|
||||
public class SystemRuleNacosPublisher implements DynamicRulePublisher<List<SystemRuleEntity>> {
|
||||
private static final Logger logger = LoggerFactory.getLogger(SystemRuleNacosPublisher.class);
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
|
|
@ -46,7 +45,7 @@ public class SystemRuleNacosPublisher implements DynamicRulePublisher<List<Syste
|
|||
return;
|
||||
}
|
||||
String dataId = NacosConfigUtil.getSystemRuleDataId(app);
|
||||
// 发布配置到nacos
|
||||
configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules), ConfigType.JSON.getType());
|
||||
boolean status = configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules), ConfigType.JSON.getType());
|
||||
logger.info("配置文件:[{}],推送结果:[{}]", dataId, status);
|
||||
}
|
||||
}
|
||||
|
|
@ -23,6 +23,6 @@ sentinel.dashboard.version=@project.version@
|
|||
#nacos.config.server-addr=http://127.0.0.1:8848
|
||||
nacos.config.server-addr=http://172.16.8.70:8848
|
||||
nacos.config.namespace=namespace-sentinel
|
||||
nacos.config.username=nacos
|
||||
nacos.config.password=nacos
|
||||
nacos.config.username=
|
||||
nacos.config.password=
|
||||
##############################################################
|
||||
|
|
|
|||
Loading…
Reference in New Issue