Sentinel 授权规则 (AuthorityRule)

Sentinel 是面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性。

Springboot+Dubbo+Nacos 集成 Sentinel(入门)-CSDN博客

1.授权规则介绍

授权规则,即黑白名单规则(AuthorityRule)。很多时候,我们需要根据调用方来限制资源是否通过,这时候可以使用 Sentinel 的访问控制(黑白名单)的功能。黑白名单根据资源的请求来源(origin)限制资源是否通过,若配置白名单则只有请求来源位于白名单内时才可通过;若配置黑名单则请求来源位于黑名单时不通过,其余的请求通过。

流量控制规则 (AuthorityRule)主要属性如下:

|----------|----------------------------------------------------|----------|
| Field | 说明 | 默认值 |
| resource | 资源名,即限流规则的作用对象 | |
| limitApp | 对应的黑名单/白名单,不同 origin 用 , 分隔,如 appA,appB | |
| strategy | 限制模式,AUTHORITY_WHITE 为白名单模式,AUTHORITY_BLACK 为黑名单模式 | 默认为白名单模式 |

2.资源名称

资源名,即限流规则的作用对象(可以是代码中指定名称或者指定路由)。

3.流控应用

对应的黑名单/白名单,不同 origin 用 , 分隔,如 appA,appB。

4.授权类型

限制模式,AUTHORITY_WHITE 为白名单模式,AUTHORITY_BLACK 为黑名单模式。

5.客户端添加来源解析器

不会以为完成上面配置就OK了吧?

NO NO NO! 还要在客户端添加来源解析器哈。

java 复制代码
@Component
public class MyRequestOriginParser implements RequestOriginParser {

    @Override
    public String parseOrigin(HttpServletRequest request) {
        String origin = request.getHeader("origin");
        return StringUtils.isEmpty(origin) ? "xxxx" : origin;
    }

}

特别注意:origin获取为空时要么直接报错(强权没问题),或者给默认值如上"xxxx"。否则授权失效【1.8.1版本亲测】。

6.规则持久化

Sentinel 规则持久化-CSDN博客

持久化Json数据格式

java 复制代码
[
	{
		"resource": "u-getUserInfo",
		"limitApp": "order",
		"strategy": 0
	}
]
复制代码
spring:
  application:
    name: user
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        prefix: dubbo-user-api
        file-extension: yml
    sentinel:
      transport:
        dashboard: 127.0.0.1:6780
      eager: true # 取消懒加载
      datasource:
        ds-user-authority-rule:
          nacos:
            server-addr: 127.0.0.1:8848
            dataId: user-authority-rule
            groupId: user-sentinel
            namespace: sentinel
            rule-type: AUTHORITY # AUTHORITY(授权规则) DEGRADE(熔断规则) FLOW(流控规则) GW_API_GROUP GW_FLOW PARAM_FLOW(热点规则) SYSTEM(系统规则)
相关推荐
一叶飘零_sweeeet8 小时前
高可用架构核心:限流熔断降级全解,Sentinel 与 Resilience4j 原理 + 落地实战
架构·sentinel
Fang fan4 天前
Redis基础数据结构
数据结构·数据库·redis·缓存·bootstrap·sentinel
回到原点的码农5 天前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
没有bug.的程序员6 天前
黑客僵尸网络的降维打击:Spring Cloud Gateway 自定义限流剿杀 Sentinel 内存黑洞
java·网络·spring·gateway·sentinel
xiaolingting8 天前
Gateway 网关流控与限流架构指南
spring cloud·架构·gateway·sentinel
sc_爬坑之路8 天前
redis windows环境配置读写分离:一主一从 + Sentinel 完整实战
windows·redis·sentinel
sc_爬坑之路9 天前
Linux 部署 Redis:一主一从 + Sentinel 完整实战
linux·redis·sentinel
杜子不疼.9 天前
Spring Cloud 熔断降级详解:用 “保险丝“ 类比,Sentinel 实战教程
人工智能·spring·spring cloud·sentinel
yc_xym11 天前
Redis哨兵(Sentinel)机制
数据库·redis·sentinel
Thomas.Sir11 天前
深入剖析 Sentinel:阿里开源的微服务流量防卫兵
微服务·开源·sentinel