Sentinel Dashboard 规则 持久化到Nacos

2022-05-11  本文已影响0人  Java编程日记

本篇文章基于sentinel1.8.4版本进行改造的。本篇主要记录改造步骤

1.下载源码
https://github.com/alibaba/Sentinel
2.打开下载的sentinel,到sentinel-dashboard 修改pom.xml
注释掉scope

<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>

</dependency>
3.找到sentinel-dashboard/src/test/java/com/alibaba/csp/sentinel/dashboard/rule/nacos目录将整个目录拷贝到sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos

4.修改 NacosConfig 就是我们上面一步复制的nacos文件夹下面的 ,这里主要是配置Nacos相关
@Configuration
public class NacosConfig {

@Value("${nacos.address}")
private String address;

@Value("${nacos.namespace}")
private String namespace;

@Value("${nacos.username}")
private String username;

@Value("${nacos.password}")
private String password;
@Bean
public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
    return JSON::toJSONString;
}


@Bean
public Converter<List<AuthorityRuleEntity>, String> authorityRuleEntityEncoder() {
    return JSON::toJSONString;
}

@Bean
public Converter<List<DegradeRuleEntity>, String> degradeRuleEntityEncoder() {
    return JSON::toJSONString;
}

@Bean
public Converter<List<ParamFlowRuleEntity>, String> paramFlowRuleEntityEncoder() {
    return JSON::toJSONString;
}

@Bean
public Converter<List<SystemRuleEntity>, String> systemRuleEntityEncoder() {
    return JSON::toJSONString;
}
@Bean
public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
    return s -> JSON.parseArray(s, FlowRuleEntity.class);
}

@Bean
public ConfigService nacosConfigService() throws Exception {
    Properties properties = new Properties();
    properties.put(PropertyKeyConst.SERVER_ADDR, address);
    properties.put(PropertyKeyConst.NAMESPACE, namespace);
    properties.put(PropertyKeyConst.USERNAME, username);
    properties.put(PropertyKeyConst.PASSWORD, password);
    return ConfigFactory.createConfigService(properties);
}

}
在application.properties加上

nacos.address=http://nacos.xxx.com:8848
nacos.namespace=dev
nacos.username=nacos
nacos.password=nacos
这里 后期启动的时候 可以加上-Dnacos.address=xxxx 进行灵活配置

5.修改流控规则FlowControllerV1
@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;

@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
修改publishRules方法,推送规则到nacos:

private CompletableFuture<Void> publishRules(String app, String ip, Integer port) {
List<FlowRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
try {
rulePublisher.publish(app, rules);
} catch (Exception e) {
e.printStackTrace();
}
return sentinelApiClient.setFlowRuleOfMachineAsync(app, ip, port, rules);
}
到这里 流控的改造已经完成。我们如果想让所有规则都能推送到nacos 还需要增加其它规则的配置

6.其它规则修改
复制 FlowRuleNacosProvider 和FlowRuleNacosPublisher 修改为其它规则 如:DegradeRuleNacosProvider 和DegradeRuleNacosPublisher

@Component("degradeRuleNacosProvider")
public class DegradeRuleNacosProvider implements DynamicRuleProvider<List<DegradeRuleEntity>> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<String, List<DegradeRuleEntity>> converter;

@Override
public List<DegradeRuleEntity> getRules(String appName) throws Exception {
    String rules = configService.getConfig(appName + NacosConfigUtil.DEGRADE_DATA_ID_POSTFIX,
        NacosConfigUtil.GROUP_ID, 3000);
    if (StringUtil.isEmpty(rules)) {
        return new ArrayList<>();
    }
    return converter.convert(rules);
}

}

==============================================

@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;
    }
    configService.publishConfig(app + NacosConfigUtil.DEGRADE_DATA_ID_POSTFIX,
        NacosConfigUtil.GROUP_ID, converter.convert(rules));
}

}
修改DegradeController,注入创建的provider和publisher

@Autowired
@Qualifier("degradeRuleNacosProvider")
private DynamicRuleProvider<List<DegradeRuleEntity>> provider;

@Autowired
@Qualifier("degradeRuleNacosPublisher")
private DynamicRulePublisher<List<DegradeRuleEntity>> publisher;

// 修改下面方法

private boolean publishRules(String app, String ip, Integer port) {
List<DegradeRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
try {
publisher.publish(app, rules);
} catch (Exception e) {
e.printStackTrace();
}
return sentinelApiClient.setDegradeRuleOfMachine(app, ip, port, rules);
}
NacosConfigUtil 增加其它规则长了后缀

public static final String FLOW_DATA_ID_POSTFIX = "-flow-rules";
public static final String DEGRADE_DATA_ID_POSTFIX = "-degrade-rules";
public static final String SYSTEM_DATA_ID_POSTFIX = "-system-rules";
public static final String PARAM_FLOW_DATA_ID_POSTFIX = "-param-flow-rules";
public static final String AUTHORITY_DATA_ID_POSTFIX = "-authority-rules";
其余的就不再罗列

附,项目中使用配置
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8182
datasource:
flow:
nacos:
server-addr: http://nacos.xxx.com:8848
dataId: {spring.application.name}-flow-rules groupId: SENTINEL_GROUP rule-type: flow degrade: nacos: server-addr: http://nacos.xxx.com:8848 dataId:{spring.application.name}-degrade-rules
groupId: SENTINEL_GROUP
rule-type: degrade
system:
nacos:
server-addr: http://nacos.xxx.com:8848
dataId: {spring.application.name}-system-rules groupId: SENTINEL_GROUP rule-type: system authority: nacos: server-addr: http://nacos.xxx.com:8848 dataId:{spring.application.name}-authority-rules
groupId: SENTINEL_GROUP
rule-type: authority
param-flow:
nacos:
server-addr: http://nacos.xxx.com:8848
dataId: ${spring.application.name}-param-flow-rules
groupId: SENTINEL_GROUP
rule-type: param-flow

上一篇下一篇

猜你喜欢

热点阅读