SpringCloud Alibaba Sentinel规则持久化实践总结

默认情况下,一旦我们重启应用,sentinel规则将消失,生产环境需要将配置规则进行持久化。这里我们实践将Sentinel持久化到Nacos中。

① pom依赖

我们引入sentinel-datasource-nacos:

xml 复制代码
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 持久化用到 -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

② yml配置

yml 复制代码
server:
  port: 8401

spring:
  application:
    name: cloud-alibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        # 服务注册中心地址
        server-addr: localhost:8848
    sentinel:
      transport:
        # 配置sentinel dashboard地址
        dashboard: localhost:8080
        # 默认 8719端口,假如被占用从8719开始+1扫描直到直到未被占用的端口
        port: 8719
      datasource: # 规则持久化
        ds1:
          nacos:
            server-addr: localhost:8848 #nacos地址
            dataId: cloud-alibaba-sentinel-service  #微服务名称
            groupId: DEFAULT_GROUP  #默认分组
            data-type: json #数据格式
            rule-type: flow #流控规则
management:
  endpoints:
    web:
      exposure:
        include: '*'

③ Nacos添加配置

java 复制代码
resource:资源名称;
limitApp:来源应用;
grade:阈值类型,0表示线程数,1表示QPS;
count:单机阈值;
strategy:流控模式,0表示直接,1表示关联,2表示链路;
controlBehavior:流控效果,0表示快速失败,1表示Warm Up,2表示排队等待;
clusterMode:是否集群。

其实就是Sentinel控制台如下配置:

④ 测试

从Sentinel控制台删除规则,停掉服务实例8401,此时再刷新Sentinel控制台,规则是空的。

在将规则配置进Nacos后,启动8401,刷新Sentinel控制台,规则仍旧是空的。

浏览器访问 http://localhost:8401/rateLimit/byUrl ,刷新Sentinel控制台,规则出现:

(这里要稍等一会)多次请求访问 http://localhost:8401/rateLimit/byUrl ,出现流控管控情况:

相关推荐
空谷有来人5 小时前
springcloud中配置ci/cd集成docker的配置文件
spring cloud·ci/cd·docker
Pluchon20 小时前
Java个人综合项目——萌部落社区V2.0
java·python·spring·spring cloud·postman·idea
豆瓣鸡1 天前
Spring Cloud Gateway 核心原理:从请求转发到微服务流量治理
网关·spring cloud·微服务
过往的时光1 天前
Spring Boot、Spring Cloud 与 Spring Cloud Alibaba 版本对照终极指南
java·spring boot·spring cloud
乐观的Terry2 天前
11、发布系统-用户认证与权限体系
java·spring boot·spring·spring cloud·mybatis
xiaoxiangsiyan2 天前
LNMP + Redis Sentinel 高可用架构部署手册(续)
运维·网络·数据库·redis·缓存·架构·sentinel
敲代码的嘎仔2 天前
从零搭建微服务:Spring Cloud Alibaba 全家桶踩坑实录(Nacos + OpenFeign + Gateway + Sentinel)
java·spring boot·spring·spring cloud·微服务·gateway·sentinel
Jul1en_3 天前
【Java 脚手架】封装通用工具类-1
java·spring boot·分布式·spring·spring cloud
小罗水5 天前
第14章 Sentinel 限流与 AI 降级
人工智能·sentinel
he___H5 天前
Sentinel使用实操
java·开发语言·sentinel