• Sentinel1.8.6集成nacos


    代码:https://gitee.com/gsls200808/sentinel-dashboard-nacos

    jar包:sentinel-dashboard-nacos 发行版 - Gitee.com

    代码如果看不到可能需要登录。

    官方参考文档:

    动态规则扩展 · 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

    以授权规则为例

    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> ruleProvider;
    4. @Autowired
    5. @Qualifier("flowRuleDefaultPublisher")
    6. private DynamicRulePublisher> rulePublisher;

    改为

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

    controller目录的其他controller包括AuthorityRuleController DegradeController FlowControllerV1 ParamFlowRuleController SystemController

    做如下更改(以DegradeController为例)

    1. @Autowired
    2. private SentinelApiClient sentinelApiClient;

    改成

    1. @Autowired
    2. @Qualifier("degradeRuleNacosProvider")
    3. private DynamicRuleProvider> ruleProvider;
    4. @Autowired
    5. @Qualifier("degradeRuleNacosPublisher")
    6. private DynamicRulePublisher> 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: 192.168.11.5: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: 192.168.11.5:8848
    17. dataId: ${spring.application.name}-degrade-rules
    18. groupId: SENTINEL_GROUP
    19. rule-type: degrade
    20. system:
    21. nacos:
    22. server-addr: 192.168.11.5:8848
    23. dataId: ${spring.application.name}-system-rules
    24. groupId: SENTINEL_GROUP
    25. rule-type: system
    26. authority:
    27. nacos:
    28. server-addr: 192.168.11.5:8848
    29. dataId: ${spring.application.name}-authority-rules
    30. groupId: SENTINEL_GROUP
    31. rule-type: authority
    32. param-flow:
    33. nacos:
    34. server-addr: 192.168.11.5:8848
    35. dataId: ${spring.application.name}-param-flow-rules
    36. groupId: SENTINEL_GROUP
    37. rule-type: param-flow

    7.测试验证

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

  • 相关阅读:
    springboot实现ACL+RBAC权限体系
    sprinboot 引入 Elasticsearch 依赖包
    牛客刷题——前端面试【六】谈一谈 v-if、v-show、v-for(区别、注意事项)
    centos7搭建EFK日志收集系统
    mybatis学习(19):模糊查询#
    4.力扣c++刷题-->删除有序数组中的重复项 II
    【数据结构】带头双向循环链表
    【web-攻击会话管理】(4.1)会话状态:状态要求、会话替代方案
    跟着CTF-wiki学pwn——ret2libc1
    asp核酸检测预登记系统源码
  • 原文地址:https://blog.csdn.net/gsls200808/article/details/132721043