当前位置:   article > 正文

sentinel规则持久化-规则同步nacos-最标准配置_sentinel-datasource-nacos

sentinel-datasource-nacos

官方参考文档:

动态规则扩展 · alibaba/Sentinel Wiki · GitHub

需要修改的代码如下:

为了便于后续版本集成nacos,简单讲一下集成思路

1.更改pom

修改sentinel-datasource-nacos的范围

  1. <dependency>
  2. <groupId>com.alibaba.csp</groupId>
  3. <artifactId>sentinel-datasource-nacos</artifactId>
  4. <scope>test</scope>
  5. </dependency>

改为

  1. <dependency>
  2. <groupId>com.alibaba.csp</groupId>
  3. <artifactId>sentinel-datasource-nacos</artifactId>
  4. <!--<scope>test</scope>-->
  5. </dependency>

2.拷贝示例

将test目录下的com.alibaba.csp.sentinel.dashboard.rule.nacos包下的内容拷贝到src的 com.alibaba.csp.sentinel.dashboard.rule的目录

test目录只包含限流,其他规则参照创建即可。

创建时注意修改常量,并且在NacosConfig实现各种converter

注意:授权规则和热点规则需要特殊处理,否则nacos配置不生效。

因为授权规则Entity比流控规则Entity多包了一层。

  1. public class FlowRuleEntity implements RuleEntity
  2. public class AuthorityRuleEntity extends AbstractRuleEntity<AuthorityRule>

以授权规则为例

AuthorityRuleNacosProvider.java

  1. /*
  2. * Copyright 1999-2018 Alibaba Group Holding Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.alibaba.csp.sentinel.dashboard.rule.nacos.authority;
  17. import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity;
  18. import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
  19. import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil;
  20. import com.alibaba.csp.sentinel.datasource.Converter;
  21. import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
  22. import com.alibaba.csp.sentinel.util.StringUtil;
  23. import com.alibaba.fastjson.JSON;
  24. import com.alibaba.fastjson.JSONArray;
  25. import com.alibaba.fastjson.JSONObject;
  26. import com.alibaba.nacos.api.config.ConfigService;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.stereotype.Component;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. /**
  32. * @author Eric Zhao
  33. * @since 1.4.0
  34. */
  35. @Component("authorityRuleNacosProvider")
  36. public class AuthorityRuleNacosProvider implements DynamicRuleProvider<List<AuthorityRuleEntity>> {
  37. @Autowired
  38. private ConfigService configService;
  39. @Autowired
  40. private Converter<String, List<AuthorityRuleEntity>> converter;
  41. @Override
  42. public List<AuthorityRuleEntity> getRules(String appName) throws Exception {
  43. String rules = configService.getConfig(appName + NacosConfigUtil.AUTHORITY_DATA_ID_POSTFIX,
  44. NacosConfigUtil.GROUP_ID, 3000);
  45. if (StringUtil.isEmpty(rules)) {
  46. return new ArrayList<>();
  47. }
  48. return converter.convert(this.parseRules(rules));
  49. }
  50. private String parseRules(String rules) {
  51. JSONArray newRuleJsons = new JSONArray();
  52. JSONArray ruleJsons = JSONArray.parseArray(rules);
  53. for (int i = 0; i < ruleJsons.size(); i++) {
  54. JSONObject ruleJson = ruleJsons.getJSONObject(i);
  55. AuthorityRuleEntity ruleEntity = JSON.parseObject(ruleJson.toJSONString(), AuthorityRuleEntity.class);
  56. JSONObject newRuleJson = JSON.parseObject(JSON.toJSONString(ruleEntity));
  57. AuthorityRule rule = JSON.parseObject(ruleJson.toJSONString(), AuthorityRule.class);
  58. newRuleJson.put("rule", rule);
  59. newRuleJsons.add(newRuleJson);
  60. }
  61. return newRuleJsons.toJSONString();
  62. }
  63. }

AuthorityRuleNacosPublisher.java

  1. /*
  2. * Copyright 1999-2018 Alibaba Group Holding Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.alibaba.csp.sentinel.dashboard.rule.nacos.authority;
  17. import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity;
  18. import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
  19. import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil;
  20. import com.alibaba.csp.sentinel.datasource.Converter;
  21. import com.alibaba.csp.sentinel.util.AssertUtil;
  22. import com.alibaba.fastjson.JSONArray;
  23. import com.alibaba.fastjson.JSONObject;
  24. import com.alibaba.nacos.api.config.ConfigService;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.stereotype.Component;
  27. import java.util.List;
  28. /**
  29. * @author Eric Zhao
  30. * @since 1.4.0
  31. */
  32. @Component("authorityRuleNacosPublisher")
  33. public class AuthorityRuleNacosPublisher implements DynamicRulePublisher<List<AuthorityRuleEntity>> {
  34. @Autowired
  35. private ConfigService configService;
  36. @Autowired
  37. private Converter<List<AuthorityRuleEntity>, String> converter;
  38. @Override
  39. public void publish(String app, List<AuthorityRuleEntity> rules) throws Exception {
  40. AssertUtil.notEmpty(app, "app name cannot be empty");
  41. if (rules == null) {
  42. return;
  43. }
  44. configService.publishConfig(app + NacosConfigUtil.AUTHORITY_DATA_ID_POSTFIX,
  45. NacosConfigUtil.GROUP_ID, this.parseRules(converter.convert(rules)));
  46. }
  47. private String parseRules(String rules) {
  48. JSONArray oldRuleJsons = JSONArray.parseArray(rules);
  49. for (int i = 0; i < oldRuleJsons.size(); i++) {
  50. JSONObject oldRuleJson = oldRuleJsons.getJSONObject(i);
  51. JSONObject ruleJson = oldRuleJson.getJSONObject("rule");
  52. oldRuleJson.putAll(ruleJson);
  53. oldRuleJson.remove("rule");
  54. }
  55. return oldRuleJsons.toJSONString();
  56. }
  57. }

热点规则同理

3.修改controller

v2目录的FlowControllerV2

  1. @Autowired
  2. @Qualifier("flowRuleDefaultProvider")
  3. private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
  4. @Autowired
  5. @Qualifier("flowRuleDefaultPublisher")
  6. private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;

改为

  1. @Autowired
  2. @Qualifier("flowRuleNacosProvider")
  3. private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
  4. @Autowired
  5. @Qualifier("flowRuleNacosPublisher")
  6. private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;

controller目录的其他controller包括

AuthorityRuleController DegradeController FlowControllerV1 ParamFlowRuleController SystemController

做如下更改(以DegradeController为例)

  1. @Autowired
  2. private SentinelApiClient sentinelApiClient;

改成

  1. @Autowired
  2. @Qualifier("degradeRuleNacosProvider")
  3. private DynamicRuleProvider<List<DegradeRuleEntity>> ruleProvider;
  4. @Autowired
  5. @Qualifier("degradeRuleNacosPublisher")
  6. private DynamicRulePublisher<List<DegradeRuleEntity>> rulePublisher;

将原有publishRules方法删除,统一改成

  1. private void publishRules(/*@NonNull*/ String app) throws Exception {
  2. List<DegradeRuleEntity> rules = repository.findAllByApp(app);
  3. rulePublisher.publish(app, rules);
  4. }

之后解决报错的地方即可。

获取所有rules的地方

 List<DegradeRuleEntity> rules = sentinelApiClient.fetchDegradeRuleOfMachine(app, ip, port);

改成

List<DegradeRuleEntity> rules = ruleProvider.getRules(app);

原有调用publishRules方法的地方,删除掉

在上一个try catch方法里加上

publishRules(entity.getApp());

这里的entity.getApp()也有可能是oldEntity.getApp()/app等变量。根据删除的publishRules代码片段推测即可。

4.修改前端文件

文件路径:src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html

 <a ui-sref="dashboard.flowV1({app: entry.app})">

改成

 <a ui-sref="dashboard.flow({app: entry.app})">

5.最后打包即可

执行命令打包成jar

mvn clean package

运行方法与官方jar一致,不做赘述

6.微服务程序集成

pom.xml添加

  1. <dependency>
  2. <groupId>com.alibaba.csp</groupId>
  3. <artifactId>sentinel-datasource-nacos</artifactId>
  4. </dependency>

配置文件application.yml添加

  1. spring:
  2. cloud:
  3. sentinel:
  4. datasource:
  5. # 名称随意
  6. flow:
  7. nacos:
  8. server-addr: localhost:8848
  9. dataId: ${spring.application.name}-flow-rules
  10. groupId: SENTINEL_GROUP
  11. # 规则类型,取值见:
  12. # org.springframework.cloud.alibaba.sentinel.datasource.RuleType
  13. rule-type: flow
  14. degrade:
  15. nacos:
  16. server-addr: localhost:8848
  17. dataId: ${spring.application.name}-degrade-rules
  18. groupId: SENTINEL_GROUP
  19. rule-type: degrade
  20. system:
  21. nacos:
  22. server-addr: localhost:8848
  23. dataId: ${spring.application.name}-system-rules
  24. groupId: SENTINEL_GROUP
  25. rule-type: system
  26. authority:
  27. nacos:
  28. server-addr: localhost:8848
  29. dataId: ${spring.application.name}-authority-rules
  30. groupId: SENTINEL_GROUP
  31. rule-type: authority
  32. param-flow:
  33. nacos:
  34. server-addr: localhost:8848
  35. dataId: ${spring.application.name}-param-flow-rules
  36. groupId: SENTINEL_GROUP
  37. rule-type: param-flow

7.测试验证

在sentinel控制台界面添加几个流控规则后尝试关闭微服务和sentinel,然后重新打开sentinel和微服务,看流控规则是否还在。

8.最后把配置好的代码发给大家,大家可以自行下载,里边有运行访问流程

https://download.csdn.net/download/qq_34091529/88482757

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/512586
推荐阅读
相关标签
  

闽ICP备14008679号