feat(sentinel): 实现Nacos规则推送功能并优化包结构
- 新增AuthorityRule、DegradeRule、ParamFlowRule和SystemRule的Nacos推送实现 - 重构FlowRule相关类到独立的flow包下 - 统一Nacos配置工具类,支持多种规则类型的dataId生成 - 移除对nacos-api的直接依赖声明 - 为各类规则增加Nacos配置的编解码器 - 在控制器中集成Nacos推送逻辑,增强规则发布的可靠性
This commit is contained in:
parent
bbe93b0bf3
commit
35dbe68c4d
|
|
@ -105,11 +105,11 @@
|
||||||
<groupId>com.alibaba.csp</groupId>
|
<groupId>com.alibaba.csp</groupId>
|
||||||
<artifactId>sentinel-datasource-nacos</artifactId>
|
<artifactId>sentinel-datasource-nacos</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<!-- <dependency>-->
|
||||||
<groupId>com.alibaba.nacos</groupId>
|
<!-- <groupId>com.alibaba.nacos</groupId>-->
|
||||||
<artifactId>nacos-api</artifactId>
|
<!-- <artifactId>nacos-api</artifactId>-->
|
||||||
<version>2.5.1</version>
|
<!-- <version>2.5.1</version>-->
|
||||||
</dependency>
|
<!-- </dependency>-->
|
||||||
<!-- for Apollo rule publisher sample -->
|
<!-- for Apollo rule publisher sample -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.ctrip.framework.apollo</groupId>
|
<groupId>com.ctrip.framework.apollo</groupId>
|
||||||
|
|
|
||||||
|
|
@ -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.AppManagement;
|
||||||
import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
|
import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
|
||||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
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.slots.block.RuleConstant;
|
||||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||||
|
|
||||||
|
|
@ -59,6 +61,8 @@ public class AuthorityRuleController {
|
||||||
private RuleRepository<AuthorityRuleEntity, Long> repository;
|
private RuleRepository<AuthorityRuleEntity, Long> repository;
|
||||||
@Autowired
|
@Autowired
|
||||||
private AppManagement appManagement;
|
private AppManagement appManagement;
|
||||||
|
@Autowired
|
||||||
|
private AuthorityRuleNacosPublisher publisher;
|
||||||
|
|
||||||
@GetMapping("/rules")
|
@GetMapping("/rules")
|
||||||
@AuthAction(PrivilegeType.READ_RULE)
|
@AuthAction(PrivilegeType.READ_RULE)
|
||||||
|
|
@ -192,6 +196,13 @@ public class AuthorityRuleController {
|
||||||
|
|
||||||
private boolean publishRules(String app, String ip, Integer port) {
|
private boolean publishRules(String app, String ip, Integer port) {
|
||||||
List<AuthorityRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
|
List<AuthorityRuleEntity> 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);
|
return sentinelApiClient.setAuthorityRuleOfMachine(app, ip, port, rules);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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.discovery.MachineInfo;
|
||||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
||||||
import com.alibaba.csp.sentinel.dashboard.repository.rule.RuleRepository;
|
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.RuleConstant;
|
||||||
import com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.CircuitBreakerStrategy;
|
import com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.CircuitBreakerStrategy;
|
||||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||||
|
|
@ -61,7 +63,8 @@ public class DegradeController {
|
||||||
private SentinelApiClient sentinelApiClient;
|
private SentinelApiClient sentinelApiClient;
|
||||||
@Autowired
|
@Autowired
|
||||||
private AppManagement appManagement;
|
private AppManagement appManagement;
|
||||||
|
@Autowired
|
||||||
|
private DegradeRuleNacosPublisher publisher;
|
||||||
@GetMapping("/rules.json")
|
@GetMapping("/rules.json")
|
||||||
@AuthAction(PrivilegeType.READ_RULE)
|
@AuthAction(PrivilegeType.READ_RULE)
|
||||||
public Result<List<DegradeRuleEntity>> apiQueryMachineRules(String app, String ip, Integer port) {
|
public Result<List<DegradeRuleEntity>> apiQueryMachineRules(String app, String ip, Integer port) {
|
||||||
|
|
@ -169,6 +172,13 @@ public class DegradeController {
|
||||||
|
|
||||||
private boolean publishRules(String app, String ip, Integer port) {
|
private boolean publishRules(String app, String ip, Integer port) {
|
||||||
List<DegradeRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
|
List<DegradeRuleEntity> 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);
|
return sentinelApiClient.setDegradeRuleOfMachine(app, ip, port, rules);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ import java.util.concurrent.TimeUnit;
|
||||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
|
import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
|
||||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
||||||
import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement;
|
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.util.StringUtil;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
|
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
|
||||||
|
|
|
||||||
|
|
@ -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.discovery.MachineInfo;
|
||||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService;
|
import com.alibaba.csp.sentinel.dashboard.auth.AuthService;
|
||||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
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.slots.block.RuleConstant;
|
||||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.SentinelVersion;
|
import com.alibaba.csp.sentinel.dashboard.datasource.entity.SentinelVersion;
|
||||||
|
|
@ -65,7 +67,8 @@ public class ParamFlowRuleController {
|
||||||
private AppManagement appManagement;
|
private AppManagement appManagement;
|
||||||
@Autowired
|
@Autowired
|
||||||
private RuleRepository<ParamFlowRuleEntity, Long> repository;
|
private RuleRepository<ParamFlowRuleEntity, Long> repository;
|
||||||
|
@Autowired
|
||||||
|
private ParamRuleNacosPublisher publisher;
|
||||||
private boolean checkIfSupported(String app, String ip, int port) {
|
private boolean checkIfSupported(String app, String ip, int port) {
|
||||||
try {
|
try {
|
||||||
return Optional.ofNullable(appManagement.getDetailApp(app))
|
return Optional.ofNullable(appManagement.getDetailApp(app))
|
||||||
|
|
@ -260,6 +263,13 @@ public class ParamFlowRuleController {
|
||||||
|
|
||||||
private CompletableFuture<Void> publishRules(String app, String ip, Integer port) {
|
private CompletableFuture<Void> publishRules(String app, String ip, Integer port) {
|
||||||
List<ParamFlowRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
|
List<ParamFlowRuleEntity> 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);
|
return sentinelApiClient.setParamFlowRuleOfMachine(app, ip, port, rules);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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.auth.AuthService.PrivilegeType;
|
||||||
import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement;
|
import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement;
|
||||||
import com.alibaba.csp.sentinel.dashboard.repository.rule.RuleRepository;
|
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.util.StringUtil;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity;
|
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity;
|
||||||
|
|
@ -51,7 +53,8 @@ public class SystemController {
|
||||||
private SentinelApiClient sentinelApiClient;
|
private SentinelApiClient sentinelApiClient;
|
||||||
@Autowired
|
@Autowired
|
||||||
private AppManagement appManagement;
|
private AppManagement appManagement;
|
||||||
|
@Autowired
|
||||||
|
private SystemRuleNacosPublisher publisher;
|
||||||
private <R> Result<R> checkBasicParams(String app, String ip, Integer port) {
|
private <R> Result<R> checkBasicParams(String app, String ip, Integer port) {
|
||||||
if (StringUtil.isEmpty(app)) {
|
if (StringUtil.isEmpty(app)) {
|
||||||
return Result.ofFail(-1, "app can't be null or empty");
|
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) {
|
private boolean publishRules(String app, String ip, Integer port) {
|
||||||
List<SystemRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
|
List<SystemRuleEntity> 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);
|
return sentinelApiClient.setSystemRuleOfMachine(app, ip, port, 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.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<List<AuthorityRuleEntity>> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SentinelApiClient sentinelApiClient;
|
||||||
|
@Autowired
|
||||||
|
private AppManagement appManagement;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<AuthorityRuleEntity> 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.fetchAuthorityRulesOfMachine(machine.getApp(), machine.getIp(), machine.getPort());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<List<AuthorityRuleEntity>> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SentinelApiClient sentinelApiClient;
|
||||||
|
@Autowired
|
||||||
|
private AppManagement appManagement;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void publish(String app, List<AuthorityRuleEntity> 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.setAuthorityRuleOfMachine(app, machine.getIp(), machine.getPort(), rules);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<List<AuthorityRuleEntity>> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ConfigService configService;
|
||||||
|
@Autowired
|
||||||
|
private Converter<String, List<AuthorityRuleEntity>> converter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取规则
|
||||||
|
*
|
||||||
|
* @degrade appName 应用名称
|
||||||
|
* @return 规则列表
|
||||||
|
* @throws Exception 异常
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<AuthorityRuleEntity> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<List<AuthorityRuleEntity>> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ConfigService configService;
|
||||||
|
@Autowired
|
||||||
|
private Converter<List<AuthorityRuleEntity>, String> converter;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void publish(String app, List<AuthorityRuleEntity> 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<List<DegradeRuleEntity>> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SentinelApiClient sentinelApiClient;
|
||||||
|
@Autowired
|
||||||
|
private AppManagement appManagement;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<DegradeRuleEntity> 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.fetchDegradeRuleOfMachine(machine.getApp(), machine.getIp(), machine.getPort());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<List<DegradeRuleEntity>> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SentinelApiClient sentinelApiClient;
|
||||||
|
@Autowired
|
||||||
|
private AppManagement appManagement;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void publish(String app, List<DegradeRuleEntity> 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.setDegradeRuleOfMachine(app, machine.getIp(), machine.getPort(), rules);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<List<DegradeRuleEntity>> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ConfigService configService;
|
||||||
|
@Autowired
|
||||||
|
private Converter<String, List<DegradeRuleEntity>> converter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取规则
|
||||||
|
*
|
||||||
|
* @degrade appName 应用名称
|
||||||
|
* @return 规则列表
|
||||||
|
* @throws Exception 异常
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<DegradeRuleEntity> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<List<DegradeRuleEntity>> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ConfigService configService;
|
||||||
|
@Autowired
|
||||||
|
private Converter<List<DegradeRuleEntity>, String> converter;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void publish(String app, List<DegradeRuleEntity> 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -13,7 +13,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* 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.ArrayList;
|
||||||
import java.util.List;
|
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.client.SentinelApiClient;
|
||||||
import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement;
|
import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement;
|
||||||
import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
|
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.util.StringUtil;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
|
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
|
||||||
|
|
@ -13,7 +13,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* 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.List;
|
||||||
import java.util.Set;
|
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.client.SentinelApiClient;
|
||||||
import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement;
|
import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement;
|
||||||
import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
|
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.util.StringUtil;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
|
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
|
||||||
|
|
@ -13,16 +13,18 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* 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.datasource.entity.rule.FlowRuleEntity;
|
||||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
|
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.datasource.Converter;
|
||||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||||
import com.alibaba.nacos.api.config.ConfigService;
|
import com.alibaba.nacos.api.config.ConfigService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
@ -47,8 +49,7 @@ public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleE
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<FlowRuleEntity> getRules(String appName) throws Exception {
|
public List<FlowRuleEntity> 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);
|
String rules = configService.getConfig(dataId, NacosConfigUtil.GROUP_ID, 3000);
|
||||||
if (StringUtil.isEmpty(rules)) {
|
if (StringUtil.isEmpty(rules)) {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
|
|
@ -13,10 +13,11 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* 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.datasource.entity.rule.FlowRuleEntity;
|
||||||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
|
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.datasource.Converter;
|
||||||
import com.alibaba.csp.sentinel.util.AssertUtil;
|
import com.alibaba.csp.sentinel.util.AssertUtil;
|
||||||
import com.alibaba.nacos.api.config.ConfigService;
|
import com.alibaba.nacos.api.config.ConfigService;
|
||||||
|
|
@ -44,9 +45,10 @@ public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRul
|
||||||
if (rules == null) {
|
if (rules == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String dataId = String.format("%s%s", NacosConfigUtil.FLOW_DATA_ID_PREFIX, app);
|
String dataId = NacosConfigUtil.getFlowRuleDataId(app);
|
||||||
// 发布配置到nacos
|
// 发布配置到nacos
|
||||||
configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules),ConfigType.JSON.getType());
|
configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules), ConfigType.JSON.getType());
|
||||||
// NacosConfigUtil.GROUP_ID, converter.convert(rules), ConfigType.JSON.getType());
|
// 需要添加类型,默认为TEXT
|
||||||
|
// configService.publishConfig(dataId, NacosConfigUtil.GROUP_ID, converter.convert(rules));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package com.alibaba.csp.sentinel.dashboard.rule.nacos;
|
package com.alibaba.csp.sentinel.dashboard.rule.nacos;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
|
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.*;
|
||||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.nacos.api.PropertyKeyConst;
|
import com.alibaba.nacos.api.PropertyKeyConst;
|
||||||
|
|
@ -37,26 +37,73 @@ public class NacosConfig {
|
||||||
// ----------------------- 读取配置文件中设置的值 start -----------------------
|
// ----------------------- 读取配置文件中设置的值 start -----------------------
|
||||||
@Value("${nacos.config.server-addr:http://127.0.0.1:8848}")
|
@Value("${nacos.config.server-addr:http://127.0.0.1:8848}")
|
||||||
private String nacosServerAddr;
|
private String nacosServerAddr;
|
||||||
|
|
||||||
@Value("${nacos.config.namespace}")
|
@Value("${nacos.config.namespace}")
|
||||||
private String nacosNamespace;
|
private String nacosNamespace;
|
||||||
|
|
||||||
@Value("${nacos.config.username:nacos}")
|
@Value("${nacos.config.username:nacos}")
|
||||||
private String nacosUsername;
|
private String nacosUsername;
|
||||||
|
|
||||||
@Value("${nacos.config.password:nacos}")
|
@Value("${nacos.config.password:nacos}")
|
||||||
private String nacosPassword;
|
private String nacosPassword;
|
||||||
// ----------------------- 读取配置文件中设置的值 end -----------------------
|
// ----------------------- 读取配置文件中设置的值 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
|
@Bean
|
||||||
public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
|
public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
|
||||||
return JSON::toJSONString;
|
return JSON::toJSONString;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
|
public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
|
||||||
return s -> JSON.parseArray(s, FlowRuleEntity.class);
|
return s -> JSON.parseArray(s, FlowRuleEntity.class);
|
||||||
}
|
}
|
||||||
|
/***************** Flow End ******************/
|
||||||
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ConfigService nacosConfigService() throws Exception {
|
public ConfigService nacosConfigService() throws Exception {
|
||||||
|
|
|
||||||
|
|
@ -22,10 +22,14 @@ package com.alibaba.csp.sentinel.dashboard.rule.nacos;
|
||||||
public final class NacosConfigUtil {
|
public final class NacosConfigUtil {
|
||||||
|
|
||||||
public static final String GROUP_ID = "SENTINEL_GROUP";
|
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 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";
|
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_FLOW_CONFIG_DATA_ID_POSTFIX = "-cs-flow-config";
|
||||||
public static final String SERVER_NAMESPACE_SET_DATA_ID_POSTFIX = "-cs-namespace-set";
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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<List<ParamFlowRuleEntity>> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SentinelApiClient sentinelApiClient;
|
||||||
|
@Autowired
|
||||||
|
private AppManagement appManagement;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<ParamFlowRuleEntity> 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.fetchFlowRuleOfMachine(machine.getApp(), machine.getIp(), machine.getPort());
|
||||||
|
return sentinelApiClient.fetchParamFlowRulesOfMachine(machine.getApp(), machine.getIp(), machine.getPort())
|
||||||
|
// .thenApply(repository::saveAll)
|
||||||
|
// .thenApply(Result::ofSuccess)
|
||||||
|
// .get()
|
||||||
|
.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<List<ParamFlowRuleEntity>> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SentinelApiClient sentinelApiClient;
|
||||||
|
@Autowired
|
||||||
|
private AppManagement appManagement;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void publish(String app, List<ParamFlowRuleEntity> 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.setParamFlowRuleOfMachine(app, machine.getIp(), machine.getPort(), rules);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<List<ParamFlowRuleEntity>> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ConfigService configService;
|
||||||
|
@Autowired
|
||||||
|
private Converter<String, List<ParamFlowRuleEntity>> converter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取规则
|
||||||
|
*
|
||||||
|
* @param appName 应用名称
|
||||||
|
* @return 规则列表
|
||||||
|
* @throws Exception 异常
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<ParamFlowRuleEntity> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<List<ParamFlowRuleEntity>> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ConfigService configService;
|
||||||
|
@Autowired
|
||||||
|
private Converter<List<ParamFlowRuleEntity>, String> converter;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void publish(String app, List<ParamFlowRuleEntity> 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<List<SystemRuleEntity>> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SentinelApiClient sentinelApiClient;
|
||||||
|
@Autowired
|
||||||
|
private AppManagement appManagement;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<SystemRuleEntity> 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.fetchSystemRuleOfMachine(machine.getApp(), machine.getIp(), machine.getPort());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<List<SystemRuleEntity>> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SentinelApiClient sentinelApiClient;
|
||||||
|
@Autowired
|
||||||
|
private AppManagement appManagement;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void publish(String app, List<SystemRuleEntity> 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.setSystemRuleOfMachine(app, machine.getIp(), machine.getPort(), rules);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<List<SystemRuleEntity>> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ConfigService configService;
|
||||||
|
@Autowired
|
||||||
|
private Converter<String, List<SystemRuleEntity>> converter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取规则
|
||||||
|
*
|
||||||
|
* @degrade appName 应用名称
|
||||||
|
* @return 规则列表
|
||||||
|
* @throws Exception 异常
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<SystemRuleEntity> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<List<SystemRuleEntity>> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ConfigService configService;
|
||||||
|
@Autowired
|
||||||
|
private Converter<List<SystemRuleEntity>, String> converter;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void publish(String app, List<SystemRuleEntity> 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue