sentinel整合nacos持久化

yml网关配置,与nacos整合持久化sentinel

yml 复制代码
server:
  port: 9999

spring:
  application:
    name: demo-gateway
  main:
    allow-circular-references: true
  config:
    import:
      - optional:nacos:${spring.application.name}-@profile.name@.yaml
  cloud:
    nacos:
      config:
        server-addr: @config.server-addr@
        group: @config.group@
        namespace: @config.namespace@
        username: @config.username@
        password: @config.password@
      discovery:
        server-addr: ${spring.cloud.nacos.config.server-addr}
        group: @config.group@
        namespace: @config.namespace@
        username: @config.username@
        password: @config.password@
    gateway:
      discovery:
        locator:
          enabled: true
      globalcors:
        cors-configurations:
          '[/**]':
            allowCredentials: true
            #springboot2.4后需用allowedOriginPatterns
            allowedOriginPatterns: "*"
            allowedMethods: "*"
            allowedHeaders: "*"
    #Sentinel配置
    sentinel:
      transport:
        dashboard: 192.168.11.50:8858
      # 支持链路限流
      web-context-unify: false
      filter:
        enabled: false
      # 取消Sentinel控制台懒加载
      eager: false
      datasource:
        #流控规则
        flow:  # 指定数据源名称
          # 指定nacos数据源
          nacos:
            server-addr: @config.server-addr@
            # 指定配置文件
            dataId: ${spring.application.name}-flow-rules
            # 指定分组
            groupId: SENTINEL_GROUP
            # 指定配置文件规则类型
            rule-type: flow
            # 指定配置文件数据格式
            data-type: json
        #降级规则
        degrade:
          nacos:
            server-addr: @config.server-addr@
            dataId: ${spring.application.name}-degrade-rules
            groupId: SENTINEL_GROUP
            rule-type: degrade
            data-type: json
        #系统规则
        system:
          nacos:
            server-addr: @config.server-addr@
            dataId: ${spring.application.name}-system-rules
            groupId: SENTINEL_GROUP
            rule-type: system
            data-type: json
        #授权规则
        authority:
          nacos:
            server-addr: @config.server-addr@
            dataId: ${spring.application.name}-authority-rules
            groupId: SENTINEL_GROUP
            rule-type: authority
            data-type: json
        #热点参数
        param-flow:
          nacos:
            server-addr: @config.server-addr@
            dataId: ${spring.application.name}-param-rules
            groupId: SENTINEL_GROUP
            rule-type: param-flow
            data-type: json
        #网关流控规则
        gw-flow:
          nacos:
            server-addr: @config.server-addr@
            dataId: ${spring.application.name}-flow-rules
            groupId: SENTINEL_GROUP
            rule-type: gw-flow
            data-type: json
        #API流控规则
        gw-api-group:
          nacos:
            server-addr: @config.server-addr@
            dataId: ${spring.application.name}-api-rules
            groupId: SENTINEL_GROUP
            rule-type: gw-api-group
            data-type: json

在nacos创建sentinel相应的规则

流控规则示例

bash 复制代码
{
  "resource": "your_resource_name",
  "limitApp": "default",
  "grade": 1,
  "count": 10,
  "strategy": 0,
  "controlBehavior": 0,
  "clusterMode": false
}

降级规则示例

bash 复制代码
{
  "resource": "your_resource_name",
  "grade": 1,
  "count": 10,
  "timeWindow": 10,
  "minRequestAmount": 5,
  "statIntervalMs": 5000
}

系统规则示例

bash 复制代码
{
  "highestCpuUsage": 0.8,
  "highestMemoryUsage": 0.8,
  "qps": 1000
}

授权规则示例

bash 复制代码
{
  "resource": "your_resource_name",
  "limitApp": "default",
  "strategy": 0,
  "resourceMode": 0,
  "warmUpPeriodSec": 10,
  "maxQueueingTimeMs": 500
}

热点参数流控规则示例

bash 复制代码
{
  "resource": "your_resource_name",
  "paramIdx": 0,
  "count": 10,
  "grade": 1,
  "paramFlowItemList": [
    {
      "object": "param_value_1",
      "count": 5
    },
    {
      "object": "param_value_2",
      "count": 5
    }
  ]
}

网关流控规则示例

bash 复制代码
{
  "resource": "your_resource_name",
  "limitApp": "default",
  "grade": 1,
  "count": 10,
  "strategy": 0,
  "controlBehavior": 0,
  "clusterMode": false
}

API 流控规则示例

bash 复制代码
{
  "resource": "your_resource_name",
  "limitApp": "default",
  "grade": 1,
  "count": 10,
  "strategy": 0,
  "controlBehavior": 0,
  "clusterMode": false
}

注意:这是模糊流控规则,其他模糊同理

bash 复制代码
{
  "resource": "/api/**", // 匹配所有以 /api/ 开头的路径,包括 /api/user、/api/order 等
  "limitApp": "default",
  "grade": 1,
  "count": 10,
  "strategy": 0,
  "controlBehavior": 0,
  "clusterMode": false
}
相关推荐
WaaTong1 分钟前
Java反射
java·开发语言·反射
齐 飞26 分钟前
MongoDB笔记01-概念与安装
前端·数据库·笔记·后端·mongodb
云空26 分钟前
《Python 与 SQLite:强大的数据库组合》
数据库·python·sqlite
暮毅31 分钟前
10.Node.js连接MongoDb
数据库·mongodb·node.js
wowocpp34 分钟前
ubuntu 22.04 server 格式化 磁盘 为 ext4 并 自动挂载 LTS
服务器·数据库·ubuntu
九圣残炎34 分钟前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目
java·算法·leetcode
wclass-zhengge37 分钟前
Netty篇(入门编程)
java·linux·服务器
成富1 小时前
文本转SQL(Text-to-SQL),场景介绍与 Spring AI 实现
数据库·人工智能·sql·spring·oracle
songqq271 小时前
SQL题:使用hive查询各类型专利top 10申请人,以及对应的专利申请数
数据库·sql
计算机学长felix1 小时前
基于SpringBoot的“校园交友网站”的设计与实现(源码+数据库+文档+PPT)
数据库·spring boot·毕业设计·交友