feat(sentinel): 配置nacos地址并优化流控规则推送逻辑

- 修改nacos配置中心地址为172.16.8.70:8848
- 注入FlowRuleNacosPublisher实现流控规则发布功能
- 重构publishRules方法增加异常处理和日志记录
- 调整流控规则dataId格式从后缀改为前缀方式
- 更新Nacos配置发布方法参数支持JSON类型
- 修复代码缩进和格式问题提升可读性
This commit is contained in:
mshe 2025-11-20 20:24:50 +08:00
parent 01e806b87b
commit bbe93b0bf3
5 changed files with 28 additions and 13 deletions

View File

@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit;
import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement;
import com.alibaba.csp.sentinel.dashboard.rule.nacos.FlowRuleNacosPublisher;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
@ -64,6 +65,10 @@ public class FlowControllerV1 {
@Autowired
private SentinelApiClient sentinelApiClient;
// 注入发布者
@Autowired
private FlowRuleNacosPublisher publisher;
@GetMapping("/rules")
@AuthAction(PrivilegeType.READ_RULE)
public Result<List<FlowRuleEntity>> apiQueryMachineRules(@RequestParam String app,
@ -169,10 +174,10 @@ public class FlowControllerV1 {
@PutMapping("/save.json")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<FlowRuleEntity> apiUpdateFlowRule(Long id, String app,
String limitApp, String resource, Integer grade,
Double count, Integer strategy, String refResource,
Integer controlBehavior, Integer warmUpPeriodSec,
Integer maxQueueingTimeMs) {
String limitApp, String resource, Integer grade,
Double count, Integer strategy, String refResource,
Integer controlBehavior, Integer warmUpPeriodSec,
Integer maxQueueingTimeMs) {
if (id == null) {
return Result.ofFail(-1, "id can't be null");
}
@ -241,7 +246,7 @@ public class FlowControllerV1 {
} catch (Throwable t) {
Throwable e = t instanceof ExecutionException ? t.getCause() : t;
logger.error("Error when updating flow rules, app={}, ip={}, ruleId={}", entity.getApp(),
entity.getIp(), id, e);
entity.getIp(), id, e);
return Result.ofFail(-1, e.getMessage());
}
}
@ -269,13 +274,20 @@ public class FlowControllerV1 {
} catch (Throwable t) {
Throwable e = t instanceof ExecutionException ? t.getCause() : t;
logger.error("Error when deleting flow rules, app={}, ip={}, id={}", oldEntity.getApp(),
oldEntity.getIp(), id, e);
oldEntity.getIp(), id, e);
return Result.ofFail(-1, e.getMessage());
}
}
private CompletableFuture<Void> publishRules(String app, String ip, Integer port) {
List<FlowRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
try {
publisher.publish(app, rules);
logger.info("限流规则推送到nacos完成,app=[{}], ip=[{}]", app, ip);
} catch (Exception e) {
logger.error("限流规则发布失败, app=[{}], ip=[{}]", app, ip, e);
throw new RuntimeException(e);
}
return sentinelApiClient.setFlowRuleOfMachineAsync(app, ip, port, rules);
}
}

View File

@ -47,8 +47,9 @@ public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleE
*/
@Override
public List<FlowRuleEntity> getRules(String appName) throws Exception {
String rules = configService.getConfig(appName + NacosConfigUtil.FLOW_DATA_ID_POSTFIX,
NacosConfigUtil.GROUP_ID, 3000);
String dataId = String.format("%s%s", NacosConfigUtil.FLOW_DATA_ID_PREFIX, appName);
String rules = configService.getConfig(dataId, NacosConfigUtil.GROUP_ID, 3000);
if (StringUtil.isEmpty(rules)) {
return new ArrayList<>();
}

View File

@ -44,9 +44,9 @@ public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRul
if (rules == null) {
return;
}
String dataId = String.format("%s%s", NacosConfigUtil.FLOW_DATA_ID_PREFIX, app);
// 发布配置到nacos
configService.publishConfig(app + NacosConfigUtil.FLOW_DATA_ID_POSTFIX,
NacosConfigUtil.GROUP_ID, converter.convert(rules));
configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules),ConfigType.JSON.getType());
// NacosConfigUtil.GROUP_ID, converter.convert(rules), ConfigType.JSON.getType());
}
}

View File

@ -23,7 +23,8 @@ public final class NacosConfigUtil {
public static final String GROUP_ID = "SENTINEL_GROUP";
public static final String FLOW_DATA_ID_POSTFIX = "-flow-rules";
// 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 CLUSTER_MAP_DATA_ID_POSTFIX = "-cluster-map";

View File

@ -25,7 +25,8 @@ sentinel.dashboard.version=@project.version@
##############################################################
# nacos config
nacos.config.server-addr=http://127.0.0.1:8848
#nacos.config.server-addr=http://127.0.0.1:8848
nacos.config.server-addr=http://172.16.8.70:8848
nacos.config.namespace=
nacos.config.username=nacos
nacos.config.password=nacos