Sentinel集成Nacos
Sentinel集成Nacos 原理
- nacos
- 作为配置中心(数据源),持久化存储规则
- sentinel-client
- 从nacos配置中心动态同步限流规则
- sentinel规则下发使用推模式
- 引入sentinel-datasource-nacos
- 实现AbstractDataSource
- 构造方法添加nacos配置监听器
- 从nacos配置中心动态同步限流规则
- sentinel-dashboard
- dashbaord的角色:只作为看板,实际规则以nacos为准
- 查询时,从nacos中取数据展示
- 提供可视化界面配置规则,crud到nacos
Sentinel集成Nacos 操作
[ 参考文献 ] : https://xie.infoq.cn/article/3a7096dc3f2b98bb5a6b983ba
规则说明
[ 参考文献 ] : https://sentinelguard.io/zh-cn/docs/basic-api-resource-rule.html
- flow:流量控制
- degrade:降级/熔断
- param-flow:热点数据
- 热点参数限流可以看做是一种特殊的流量控制,仅对包含热点参数的资源调用生效
- authority:授权(白名单/黑名单)
- system:系统保护
- gateway:网关规则
- gw-flow:流量控制
- gw-api-group:api分组
Nacos规则配置示例
基础配置
spring:
cloud:
sentinel:
enabled: true
## 随应用启动加载
eager: true
# feign集成sentinel
feign:
sentinel:
enabled: true
环境关联配置-application
spring:
cloud:
sentinel:
## dashboard传输地址
transport:
dashboard: ${sentinel.dashboard.host}:${sentinel.dashboard.port}
port: ${sentinel.port}
datasource:
flow:
nacos:
server-addr: ${nacos地址}
username: ${nacos账号}
password: ${nacos密码}
dataId: ${spring.application.name}-flow-rules
groupId: SENTINEL_GROUP
namespace: ${nacos命名空间}
# 规则类型,取值见:
# org.springframework.cloud.alibaba.sentinel.datasource.RuleType
rule-type: flow
degrade:
nacos:
server-addr: ${nacos地址}
username: ${nacos账号}
password: ${nacos密码}
dataId: ${spring.application.name}-degrade-rules
groupId: SENTINEL_GROUP
namespace: ${nacos命名空间}
rule-type: degrade
system:
nacos:
server-addr: ${nacos地址}
username: ${nacos账号}
password: ${nacos密码}
dataId: ${spring.application.name}-system-rules
groupId: SENTINEL_GROUP
namespace: ${nacos命名空间}
rule-type: system
authority:
nacos:
server-addr: ${nacos地址}
username: ${nacos账号}
password: ${nacos密码}
dataId: ${spring.application.name}-authority-rules
groupId: SENTINEL_GROUP
namespace: ${nacos命名空间}
rule-type: authority
param-flow:
nacos:
server-addr: ${nacos地址}
username: ${nacos账号}
password: ${nacos密码}
dataId: ${spring.application.name}-param-flow-rules
groupId: SENTINEL_GROUP
namespace: ${nacos命名空间}
rule-type: param-flow
环境关联配置-gateway
spring:
cloud:
sentinel:
## dashboard传输地址
transport:
dashboard: ${sentinel.dashboard.host}:${sentinel.dashboard.port}
port: ${sentinel.port}
## 规则nacos数据源
datasource:
## 网关流控
gw-flow:
nacos:
server-addr: ${nacos地址}
username: ${nacos账号}
password: ${nacos密码}
dataId: ${spring.application.name}-gw-flow-rules
groupId: SENTINEL_GROUP
namespace: ${nacos命名空间}
rule-type: GW_FLOW
## 网关路由分组
gw-api-group:
nacos:
server-addr: ${nacos地址}
username: ${nacos账号}
password: ${nacos密码}
dataId: ${spring.application.name}-gw-api-group-rules
groupId: SENTINEL_GROUP
namespace: ${nacos命名空间}
rule-type: GW_API_GROUP
## 其他规则...