网关限流
-
网关是所有请求的公共入口,所以可以在网关进行限流,而且限流的方式也很多,我们本次采用的前面学过的Sentinel组件实现网关的限流。Sentinel支持对SpringCloudGateway,Zuul等主流网关进行限流。
image - 从1.6.0版本开始,Sentinel提供了SpringCloudGateway的适配模块,可以提供两种资源维度的限流
- route维度:即在Spring配置文件中配置的路由条目,资源名为对应的routeId
- 自定义API维度:用户可以利用Sentinel提供的API来自定义一些API分组
1导入依赖
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
</dependency>
2编写配置类
- 基于Sentinel的Gateway限流是通过其提供的Filter来完成的,使用时只需注入对应的SentinelGatewayFilter实例以及SentinelGatewayBlockExceptionHandler实例即可。
网友评论