Sentinel-1.8.1 持久化到Nacos中

什么是Sentinel

随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式服务架构的轻量级流量控制框架,主要以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度来帮助您保护服务的稳定性。

下载Sentinel 图形化界面

使用java -jar (jar包的名称) 启动  一定要加扩展名称
默认账户密码都是 sentinel  

默认的首页都是这样什么都没有

在这里插入图片描述
添加yml配置文件

 sentinel:
      transport:
        port: 8719
        dashboard: localhost:8080

然后在当前项目中引入maven依赖(我项目中使用了nacos,feign,)

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
<!--        sentinel  哨兵 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>0.9.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>

引入依赖以后在对应的Controller接口位置加上

@SentinelResource(value="hi")

然后访问这个接口,在对应的Sentinel控制台中就能看到这个接口访问的情况
在这里插入图片描述
在流控按钮这添加流控规则QPS 和单机阈值 每秒的访问量
在这里插入图片描述
然后访问接口如果一秒内可以访问两次就会触发阈值
在这里插入图片描述

如果项目重新启动以后刚刚添加的流控规则就消失了。所以需要把流控规则持久化到nacos中。这样每次启动项目就不用重新配置流控规则了。

把Sentinel持久化到nacos 中

前提需要把nacos 配置好。
先添加配置文件

spring:
  cloud:
    sentinel:
      transport:
        port: 8719
        dashboard: localhost:8080
      datasource:
        ds:
          nacos:
            server-addr: 192.168.43.205:8848
            dataId: ${
    
    spring.application.name}
            namespace: d3502814-1c5b-4b23-8963-247d62189c7c 
            rule-type: flow

猜你喜欢

转载自blog.csdn.net/weixin_44647098/article/details/114275988