微服务限流Sentinel讲解(五)

简介: 微服务限流Sentinel讲解(五)

授权规则可以对请求方来源做判断和控制。

4.1.授权规则

4.1.1.基本规则

授权规则可以对调用方的来源做控制,有白名单和黑名单两种方式。

  • 白名单:来源(origin)在白名单内的调用者允许访问
  • 黑名单:来源(origin)在黑名单内的调用者不允许访问

点击左侧菜单的授权,可以看到授权规则:

  • 资源名:就是受保护的资源,例如/order/{orderId}
  • 流控应用:是来源者的名单,
  • 如果是勾选白名单,则名单中的来源被许可访问。
  • 如果是勾选黑名单,则名单中的来源被禁止访问。

比如:

我们允许请求从gateway到order-service,不允许浏览器访问order-service,那么白名单中就要填写网关的来源名称(origin)

4.1.2.如何获取origin

Sentinel是通过RequestOriginParser这个接口的parseOrigin来获取请求的来源的。

public interface RequestOriginParser {
     /**
      * 从请求request对象中获取origin,获取方式自定义
      */
     String parseOrigin(HttpServletRequest request);
 }

这个方法的作用就是从request对象中,获取请求者的origin值并返回。

默认情况下,sentinel不管请求者从哪里来,返回值永远是default,也就是说一切请求的来源都被认为是一样的值default。

因此,我们需要自定义这个接口的实现,让不同的请求,返回不同的origin

例如order-service服务中,我们定义一个RequestOriginParser的实现类:

package cn.itcast.order.sentinel;
 import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
 import org.springframework.stereotype.Component;
 import org.springframework.util.StringUtils;
 import javax.servlet.http.HttpServletRequest;
 @Component
 public class HeaderOriginParser implements RequestOriginParser {
     @Override
     public String parseOrigin(HttpServletRequest request) {
         // 1.获取请求头
         String origin = request.getHeader("origin");
         // 2.非空判断
         if (StringUtils.isEmpty(origin)) {
             origin = "blank";
         }
         return origin;
     }
 }

我们会尝试从request-header中获取origin值。

4.1.3.给网关添加请求头

既然获取请求origin的方式是从reques-header中获取origin值,我们必须让所有从gateway路由到微服务的请求都带上origin头

这个需要利用之前学习的一个GatewayFilter来实现,AddRequestHeaderGatewayFilter。

修改gateway服务中的application.yml,添加一个defaultFilter:

spring:
   cloud:
     gateway:
       default-filters:
         - AddRequestHeader=origin,gateway
       routes:
        # ...略

这样,从gateway路由的所有请求都会带上origin头,值为gateway。而从其它地方到达微服务的请求则没有这个头。

4.1.4.配置授权规则

接下来,我们添加一个授权规则,放行origin值为gateway的请求。

现在,我们直接跳过网关,访问order-service服务:

通过网关访问:

4.2.自定义异常结果

默认情况下,发生限流、降级、授权拦截时,都会抛出异常到调用方。异常结果都是flow limmiting(限流)。这样不够友好,无法得知是限流还是降级还是授权拦截。

4.2.1.异常类型

而如果要自定义异常时的返回结果,需要实现BlockExceptionHandler接口:

public interface BlockExceptionHandler {
     /**
      * 处理请求被限流、降级、授权拦截时抛出的异常:BlockException
      */
     void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception;
 }

这个方法有三个参数:

  • HttpServletRequest request:request对象
  • HttpServletResponse response:response对象
  • BlockException e:被sentinel拦截时抛出的异常

这里的BlockException包含多个不同的子类:

异常 说明
FlowException 限流异常
ParamFlowException 热点参数限流的异常
DegradeException 降级异常
AuthorityException 授权规则异常
SystemBlockException 系统规则异常

4.2.2.自定义异常处理

下面,我们就在order-service定义一个自定义异常处理类:

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
 import com.alibaba.csp.sentinel.slots.block.BlockException;
 import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
 import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
 import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
 import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
 import org.springframework.stereotype.Component;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 @Component
 public class SentinelExceptionHandler implements BlockExceptionHandler {
     @Override
     public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
         String msg = "未知异常";
         int status = 429;
         if (e instanceof FlowException) {
             msg = "请求被限流了";
         } else if (e instanceof ParamFlowException) {
             msg = "请求被热点参数限流";
         } else if (e instanceof DegradeException) {
             msg = "请求被降级了";
         } else if (e instanceof AuthorityException) {
             msg = "没有权限访问";
             status = 401;
         }
         response.setContentType("application/json;charset=utf-8");
         response.setStatus(status);
         response.getWriter().println("{\"msg\": " + msg + ", \"status\": " + status + "}");
     }
 }

重启测试,在不同场景下,会返回不同的异常消息.

限流:

授权拦截时:

5.规则持久化

现在,sentinel的所有规则都是内存存储,重启后所有规则都会丢失。在生产环境下,我们必须确保这些规则的持久化,避免丢失。

5.1.规则管理模式

规则是否能持久化,取决于规则管理模式,sentinel支持三种规则管理模式:

  • 原始模式:Sentinel的默认模式,将规则保存在内存,重启服务会丢失。
  • pull模式
  • push模式

5.1.1.pull模式

pull模式:控制台将配置的规则推送到Sentinel客户端,而客户端会将配置规则保存在本地文件或数据库中。以后会定时去本地文件或数据库中查询,更新本地规则。

5.1.2.push模式

push模式:控制台将配置规则推送到远程配置中心,例如Nacos。Sentinel客户端监听Nacos,获取配置变更的推送消息,完成本地配置更新。

如果你能看到这时,这个sentinel的示例己经讲解完毕,我将案例代码放到gitee上供各位下载:

https://gitee.com/bertoon/sentinel-sample-code.git

相关文章
|
2天前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
|
19天前
|
SpringCloudAlibaba 监控 Java
SpringCloud Alibaba微服务-- Sentinel的使用(保姆级)
SpringCloud Alibaba微服务-- Sentinel的使用(保姆级)
|
1月前
|
Java Nacos Sentinel
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(九)Nacos+Sentinel+Seata
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(九)Nacos+Sentinel+Seata
206 0
|
1月前
|
SpringCloudAlibaba 监控 Java
SpringCloud Alibaba Sentinel实现熔断与限流--学习笔记
SpringCloud Alibaba Sentinel实现熔断与限流--学习笔记
24 0
|
2月前
|
监控 算法 Java
sentinel 服务限流工作原理
sentinel 服务限流工作原理
|
2月前
|
监控 Java API
Sentinel熔断限流真的太丝滑了
Sentinel熔断限流真的太丝滑了
100 0
|
2月前
|
监控 开发者 Sentinel
Sentinel:微服务守护神的崛起
Sentinel:微服务守护神的崛起
33 0
|
3天前
|
Java API Nacos
第十二章 Spring Cloud Alibaba Sentinel
第十二章 Spring Cloud Alibaba Sentinel
14 0
|
1月前
|
SpringCloudAlibaba Sentinel 索引
【九】SpringCloud Alibaba之整合Sentinel(实现热点控制)
【九】SpringCloud Alibaba之整合Sentinel(实现热点控制)
34 1
|
1月前
|
SQL SpringCloudAlibaba Sentinel
【八】SpringCloud Alibaba之整合Sentinel(实现流量控制3)
【八】SpringCloud Alibaba之整合Sentinel(实现流量控制3)
33 2