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(系统规则)
相关推荐
@HNUSTer2 天前
基于 GEE 平台用 Sentinel-1 SAR 数据实现山区潜在滑坡检测
云计算·sentinel·数据集·遥感大数据·gee·云平台·sar
还是鼠鼠4 天前
《黑马商城》微服务保护-详细介绍【简单易懂注释版】
java·spring boot·spring·spring cloud·sentinel·maven
月夕·花晨8 天前
Gateway-过滤器
java·分布式·spring·spring cloud·微服务·gateway·sentinel
非凡ghost9 天前
Hard Disk Sentinel(固态硬盘监控) 多语便携版
windows·sentinel·软件需求
柳贯一(逆流河版)12 天前
Sentinel 深度解析:限流与熔断降级的微服务稳定性保障实践
微服务·架构·sentinel
耳东哇12 天前
sentinel docker gateway k8s 集群 主从
docker·gateway·sentinel
遥感之家13 天前
AWS下载sentinel-2原始影像
云计算·sentinel·aws
月夕·花晨14 天前
Gateway-断言
java·开发语言·分布式·spring cloud·微服务·nacos·sentinel
齐 飞15 天前
Spring Cloud Alibaba快速入门-Sentinel熔断规则
spring boot·spring cloud·sentinel
齐 飞17 天前
Spring Cloud Alibaba快速入门-Sentinel流量控制(FlowRule)
spring cloud·微服务·sentinel