• Sentinel使用Nacos存储规则


    动态配置

    在这里插入图片描述
    推荐使用psuh模式,结合nacos使用

    组件版本推荐

    在这里插入图片描述
    这里使用:
    springboot 2.3.2.RELEASE
    springcloud Hoxton.SR8
    spring alibaba cluod 2.2.5.RELEASE
    sentinel-datasource-nacos 1.8.0
    sentinel 1.8.5

    sentinel整合nacos

    注解配置、控制台配置都无法解决配置丢失问题,通过Nacos配置中心解决该问题
    下载源码:https://github.com/alibaba/Sentinel/archive/refs/tags/1.8.5.zip
    在这里插入图片描述

    1. 修改pom.xml
      在这里插入图片描述
      将test这块注释掉

    2. 将test路径下的nacos文件夹拷贝到rule下
      在这里插入图片描述

    3. 在配置文件中增加nacos配置
      在这里插入图片描述

    4. 修改NacosConfig的配置
      在这里插入图片描述

    5. NacosConfigUtil注意groupid和后缀
      在这里插入图片描述

    6. 修改规则方法的引用,新增nacos的方法
      在这里插入图片描述

        @Autowired
        @Qualifier("flowRuleNacosProvider")
        private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
        @Autowired
        @Qualifier("flowRuleNacosPublisher")
        private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    1. 修改sidebar.html文件

    在这里插入图片描述

    <li ui-sref-active="active" ng-if="entry.appType==0">
    	<a ui-sref="dashboard.flowV1({app: entry.app})">
    	<i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则 V1</a>
    </li>
    
    <li ui-sref-active="active" ng-if="!entry.isGateway">
    	<a ui-sref="dashboard.flow({app: entry.app})">
    	<i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则 Nacos</a>
    </li>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 修改 FlowServiceV1 为FlowServiceV2
      在这里插入图片描述

    • 修改flow_v2.html
      在这里插入图片描述

    • 打包
      在这里插入图片描述

    客户端集成

    • pom.xml
    	<!--sentinel持久化 -->
    	<dependency>
    		<groupId>com.alibaba.csp</groupId>
    		<artifactId>sentinel-datasource-nacos</artifactId>
    	</dependency>
    
    	<!-- nacos服务注册与发现 -->
    	<dependency>
    		<groupId>com.alibaba.cloud</groupId>
    		<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    	</dependency>
    
    	<!-- nacos配置 -->
    	<dependency>
    		<groupId>com.alibaba.cloud</groupId>
    		<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    	</dependency>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 配置
    server:
      port: 8806
    
    spring:
      application:
        name: sentinel-user-nacos-push  #微服务名称
      #配置nacos注册中心地址
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
    
        sentinel:
          transport:
            # 添加sentinel的控制台地址
            dashboard: 127.0.0.1:8080
            # 指定应用与Sentinel控制台交互的端口,应用本地会起一个该端口占用的HttpServer
            #port: 8719
            clientIp: localhost
          datasource:
    #        ds1:   #名称自定义,唯一
    #          nacos:
    #            server-addr: 127.0.0.1:8848
    #            dataId: ${spring.application.name}-flow-rules
    #            groupId: SENTINEL_GROUP
    #            data-type: json
    #            rule-type: flow
            flow-rules:
              nacos:
                namespace: dddddd
                server-addr: 127.0.0.1:8848
                dataId: ${spring.application.name}-flow-rules
                groupId: SENTINEL_GROUP# 注意groupId对应Sentinel Dashboard中的定义
                data-type: json
                rule-type: flow
            degrade-rules:
              nacos:
                server-addr: 127.0.0.1:8848
                dataId: ${spring.application.name}-degrade-rules
                groupId: SENTINEL_GROUP
                data-type: json
                rule-type: degrade
            param-flow-rules:
              nacos:
                server-addr: 127.0.0.1:8848
                dataId: ${spring.application.name}-param-flow-rules
                groupId: SENTINEL_GROUP
                data-type: json
                rule-type: param-flow
            authority-rules:
              nacos:
                server-addr: 127.0.0.1:8848
                dataId: ${spring.application.name}-authority-rules
                groupId: SENTINEL_GROUP
                data-type: json
                rule-type: authority
            system-rules:
              nacos:
                server-addr: 127.0.0.1:8848
                dataId: ${spring.application.name}-system-rules
                groupId: SENTINEL_GROUP
                data-type: json
                rule-type: system
    
      main:
        allow-bean-definition-overriding: true
    
    #暴露actuator端点   http://localhost:8800/actuator/sentinel
    management:
      endpoints:
        web:
          exposure:
            include: '*'
    
    #feign:
    #  sentinel:
    #    enabled: true   #开启sentinel对feign的支持 默认false
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 在配置中心创建流控规则文件:sentinel-user-nacos-push-flow-rules
    [
        {
            "clusterConfig":{
                "acquireRefuseStrategy":0,
                "clientOfflineTime":2000,
                "fallbackToLocalWhenFail":true,
                "resourceTimeout":2000,
                "resourceTimeoutStrategy":0,
                "sampleCount":10,
                "strategy":0,
                "thresholdType":0,
                "windowIntervalMs":1000
            },
            "clusterMode":false,
            "controlBehavior":0,
            "count":1,
            "grade":1,
            "limitApp":"default",
            "maxQueueingTimeMs":500,
            "resource":"/test1",
            "strategy":0,
            "warmUpPeriodSec":10
        }
    ]
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    注意:“limitApp”:“default”,这个参数默认为default

    • 验证nacos里面是不是有
      在这里插入图片描述
    • sentinel 控制台是不是也有该规则

    由于懒加载的方式,需要先访问服务才能显示
    在这里插入图片描述
    资源包:sentinel-dashboard-1.8.5.jar

  • 相关阅读:
    Google Play如何做ASO优化?影响搜索排名的主要因素.
    使用GDIView工具排查GDI对象泄漏问题(常用分析工具)
    systrace使用注意事项
    源码分析:Actor模型架构
    mac下修改vscode的代码提示键
    【lc刷题 day2】树的子结构 二叉树的镜像 对称的二叉树 顺时针打印矩阵
    asp毕业设计——基于asp+sqlserver的个人网站建设设计与实现(毕业论文+程序源码)——个人网站建设
    learnopengl 中 pbr的球体算法
    【SSTI模块注入】SSTI+Flask+Python(上):基础知识
    mysql死锁查看
  • 原文地址:https://blog.csdn.net/yy1209357299/article/details/126775755