Sentinel使用Nacos存储规则——普通服务

本文介绍了如何在SpringBoot 2.3.2和Spring Cloud Hoxton环境中,利用Sentinel 1.8.5与Nacos 1.8.0配合,通过Nacos配置中心解决规则配置丢失问题。包括修改pom.xml、配置NacosDataSource、调整UI和代码实现,以及客户端集成步骤。
摘要由CSDN通过智能技术生成

动态配置

在这里插入图片描述
推荐使用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. 修改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>

  • 修改 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>

  • 配置
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



  • 在配置中心创建流控规则文件: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
    }
]


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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值