当前位置:   article > 正文

Gateway网关限流_gatewayflowrule

gatewayflowrule

网关是所有请求的公共入口,所以可以在网关进行限流,而且限流的方式也很多,我们本次采用Sentinel组件来实现网关的限流。Sentinel支持对SpringCloud Gateway、Zuul等主流网关进行限流。

从Sentinel1.6.0版本开始,Sentinel提供了SpringCloud Gateway的适配模块,可以提供两种资源维度的限流:

  • route维度:即在Spring配置文件中配置的路由条目,资源名为对应的routeId
  • 自定义API维度:用户可以利用Sentinel提供的API来自定义一些API分组

目录

1 route维度限流

1.1 导入依赖

1.2 配置路由信息

1.3 编写配置类

1.4 测试

2 自定义API分组维度 实现限流


1 route维度限流

1.1 导入依赖

  1. <!--限流-->
  2. <dependency>
  3. <groupId>com.alibaba.csp</groupId>
  4. <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
  5. </dependency>

1.2 配置路由信息

在application.yml中,添加两个路由信息,分别转发到两个微服务中。实现对id=product_route的路由进行限流。

  1. server:
  2. port: 7000
  3. spring:
  4. application:
  5. name: api-gateway
  6. cloud:
  7. nacos:
  8. discovery:
  9. server-addr: localhost:8848 # nacos服务端地址
  10. gateway:
  11. discovery:
  12. locator:
  13. enabled: true # 启用探测器,让gateway可以发现nacos中的微服务
  14. routes: # 路由数组(路由:就是当指定请求满足什么条件的时候,转发到哪个微服务)
  15. - id: product_route # 当前路由的标识,要求唯一。默认uuid
  16. uri: lb://service-product # lb指的是负载均衡(load balancing),service-product是nacos中微服务的名称
  17. order: 1 # 路由的优先级,数字越小级别越高
  18. predicates: # 断言(就是路由转发要满足的条件)
  19. - Path=/product-serv/** # 当请求路径满足Path指定的规则时,才进行路由转发
  20. filters: # 过滤器,请求在传递过程中可以通过过滤器对其进行一定的修改
  21. - StripPrefix=1 # 转发之前去掉1层路径
  22. - id: order_route # 当前路由的标识,要求唯一。默认uuid
  23. uri: lb://service-order # lb指的是负载均衡(load balancing),service-order是nacos中微服务的名称
  24. order: 1 # 路由的优先级,数字越小级别越高
  25. predicates: # 断言(就是路由转发要满足的条件)
  26. - Path=/order-serv/** # 当请求路径满足Path指定的规则时,才进行路由转发
  27. filters: # 过滤器,请求在传递过程中可以通过过滤器对其进行一定的修改
  28. - StripPrefix=1 # 转发之前去掉1层路径

1.3 编写配置类

基于Sentinel的Gateway限流是通过其提供的Filter来完成的,使用时只需注入对应的SentinelGatewayFilter实例以及SentinelGatewayBlockExceptionHandler实例即可。

  1. package cn.jack.config;
  2. import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
  3. import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
  4. import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
  5. import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
  6. import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
  7. import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
  8. import org.springframework.beans.factory.ObjectProvider;
  9. import org.springframework.cloud.gateway.filter.GlobalFilter;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.core.Ordered;
  13. import org.springframework.core.annotation.Order;
  14. import org.springframework.http.HttpStatus;
  15. import org.springframework.http.MediaType;
  16. import org.springframework.http.codec.ServerCodecConfigurer;
  17. import org.springframework.web.reactive.function.BodyInserters;
  18. import org.springframework.web.reactive.function.server.ServerResponse;
  19. import org.springframework.web.reactive.result.view.ViewResolver;
  20. import org.springframework.web.server.ServerWebExchange;
  21. import reactor.core.publisher.Mono;
  22. import javax.annotation.PostConstruct;
  23. import java.util.*;
  24. @Configuration
  25. public class GatewayConfiguration {
  26. private final List<ViewResolver> viewResolvers;
  27. private final ServerCodecConfigurer serverCodecConfigurer;
  28. public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider,
  29. ServerCodecConfigurer serverCodecConfigurer) {
  30. this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
  31. this.serverCodecConfigurer = serverCodecConfigurer;
  32. }
  33. // 配置限流的异常处理器
  34. @Bean
  35. @Order(Ordered.HIGHEST_PRECEDENCE)
  36. public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
  37. // Register the block exception handler for Spring Cloud Gateway.
  38. return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
  39. }
  40. // 初始化一个限流的过滤器
  41. @Bean
  42. @Order(Ordered.HIGHEST_PRECEDENCE)
  43. public GlobalFilter sentinelGatewayFilter() {
  44. return new SentinelGatewayFilter();
  45. }
  46. /**
  47. * 配置初始化的限流參數
  48. */
  49. @PostConstruct
  50. public void initGatewayRules() {
  51. Set<GatewayFlowRule> rules = new HashSet<GatewayFlowRule>();
  52. rules.add(
  53. new GatewayFlowRule("product_route") // 资源名称,对应路由id
  54. .setCount(1) // 限流阈值
  55. .setIntervalSec(5) // 统计时间窗口(熔断时间),单位:秒。默认是1秒
  56. );
  57. GatewayRuleManager.loadRules(rules);
  58. }
  59. /**
  60. * 自定义限流异常页面,限流返回的信息
  61. */
  62. @PostConstruct
  63. public void initBlockHandlers() {
  64. BlockRequestHandler blockRequestHandler = new BlockRequestHandler() {
  65. @Override
  66. public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
  67. Map map = new HashMap();
  68. map.put("code", 0);
  69. map.put("message", "接口被限流了");
  70. return ServerResponse.status(HttpStatus.OK)
  71. .contentType(MediaType.APPLICATION_JSON_UTF8)
  72. .body(BodyInserters.fromObject(map));
  73. }
  74. };
  75. GatewayCallbackManager.setBlockHandler(blockRequestHandler);
  76. }
  77. }

1.4 测试

通过网关访问微服务service-product,一秒内多次访问http://localhost:7000/product-serv/product/2就可以看到限流起作用了。

而另一条路由则没有影响:

2 自定义API分组维度 实现限流

自定义API分组是一种更细粒度的限流规则定义。它可以对匹配到的接口进行限流。

自定义API分组配置类完整代码如下:

  1. package cn.jack.config;
  2. import com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants;
  3. import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
  4. import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem;
  5. import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
  6. import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
  7. import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
  8. import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
  9. import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
  10. import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
  11. import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
  12. import org.springframework.beans.factory.ObjectProvider;
  13. import org.springframework.cloud.gateway.filter.GlobalFilter;
  14. import org.springframework.context.annotation.Bean;
  15. import org.springframework.context.annotation.Configuration;
  16. import org.springframework.core.Ordered;
  17. import org.springframework.core.annotation.Order;
  18. import org.springframework.http.HttpStatus;
  19. import org.springframework.http.MediaType;
  20. import org.springframework.http.codec.ServerCodecConfigurer;
  21. import org.springframework.web.reactive.function.BodyInserters;
  22. import org.springframework.web.reactive.function.server.ServerResponse;
  23. import org.springframework.web.reactive.result.view.ViewResolver;
  24. import org.springframework.web.server.ServerWebExchange;
  25. import reactor.core.publisher.Mono;
  26. import javax.annotation.PostConstruct;
  27. import java.util.*;
  28. @Configuration
  29. public class GatewayConfiguration2 {
  30. private final List<ViewResolver> viewResolvers;
  31. private final ServerCodecConfigurer serverCodecConfigurer;
  32. public GatewayConfiguration2(ObjectProvider<List<ViewResolver>> viewResolversProvider,
  33. ServerCodecConfigurer serverCodecConfigurer) {
  34. this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
  35. this.serverCodecConfigurer = serverCodecConfigurer;
  36. }
  37. // 配置限流的异常处理器
  38. @Bean
  39. @Order(Ordered.HIGHEST_PRECEDENCE)
  40. public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
  41. // Register the block exception handler for Spring Cloud Gateway.
  42. return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
  43. }
  44. // 初始化一个限流的过滤器
  45. @Bean
  46. @Order(Ordered.HIGHEST_PRECEDENCE)
  47. public GlobalFilter sentinelGatewayFilter() {
  48. return new SentinelGatewayFilter();
  49. }
  50. /**
  51. * 配置初始化的限流參數
  52. */
  53. @PostConstruct
  54. public void initGatewayRules() {
  55. Set<GatewayFlowRule> rules = new HashSet<GatewayFlowRule>();
  56. rules.add(
  57. new GatewayFlowRule("product_api_001") // 资源名称,可以是sentinel的API定义的分组
  58. .setCount(1) // 限流阈值
  59. .setIntervalSec(5) // 统计时间窗口(即时间段内达到阈值实行限流),单位:秒。默认是1秒
  60. );
  61. rules.add(
  62. new GatewayFlowRule("product_api_002") // 资源名称,可以是sentinel的API定义的分组
  63. .setCount(1) // 限流阈值
  64. .setIntervalSec(5) // 统计时间窗口(即时间段内达到阈值实行限流),单位:秒。默认是1秒
  65. );
  66. GatewayRuleManager.loadRules(rules);
  67. }
  68. // 自定义API分组
  69. @PostConstruct
  70. private void initCustomizedApis() {
  71. Set<ApiDefinition> definitions = new HashSet<>();
  72. Set predicateItemSet1 = new HashSet();
  73. predicateItemSet1.add(new ApiPathPredicateItem()
  74. .setPattern("/product-serv/product/api001/**")
  75. .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX)); // 前缀匹配
  76. ApiDefinition api1 = new ApiDefinition("product_api_001")
  77. .setPredicateItems(predicateItemSet1);
  78. Set predicateItemSet2 = new HashSet();
  79. predicateItemSet2.add(new ApiPathPredicateItem()
  80. .setPattern("/product-serv/product/api002/demo1")
  81. .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_EXACT)); // 精确匹配
  82. ApiDefinition api2 = new ApiDefinition("product_api_002")
  83. .setPredicateItems(predicateItemSet2);
  84. definitions.add(api1);
  85. definitions.add(api2);
  86. GatewayApiDefinitionManager.loadApiDefinitions(definitions);
  87. }
  88. /**
  89. * 自定义限流异常页面,限流返回的信息
  90. */
  91. @PostConstruct
  92. public void initBlockHandlers() {
  93. BlockRequestHandler blockRequestHandler = new BlockRequestHandler() {
  94. @Override
  95. public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
  96. Map map = new HashMap();
  97. map.put("code", 0);
  98. map.put("message", "接口被限流了");
  99. return ServerResponse.status(HttpStatus.OK)
  100. .contentType(MediaType.APPLICATION_JSON_UTF8)
  101. .body(BodyInserters.fromObject(map));
  102. }
  103. };
  104. GatewayCallbackManager.setBlockHandler(blockRequestHandler);
  105. }
  106. }

微服务service-product的测试接口:

  1. package cn.jack.controller;
  2. import cn.jack.domain.Product;
  3. import cn.jack.service.ProductService;
  4. import com.alibaba.fastjson.JSON;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. @RestController
  11. @Slf4j
  12. public class ProductController {
  13. @RequestMapping("/product/api001/demo1")
  14. public String demo1() {
  15. return "demo";
  16. }
  17. @RequestMapping("/product/api001/demo2")
  18. public String demo2() {
  19. return "demo2";
  20. }
  21. @RequestMapping("/product/api002/demo1")
  22. public String demo3() {
  23. return "demo3";
  24. }
  25. @RequestMapping("/product/api002/demo2")
  26. public String demo4() {
  27. return "demo4";
  28. }
  29. }

启动进行测试,会发现api001下的两个接口被限流了,而api002只有demo1接口被限流。

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/301956
推荐阅读
相关标签
  

闽ICP备14008679号