Spring Cloud Gateway 是基于 Netty,跟 Servlet 不兼容,所以你的工程中不能出现 Servlet 的组件 。

    1、pom.xml

    注意,一定不能出现 spring web 的依赖,因为 Gateway 与 Servlet 不兼容。

    1. <dependency>
    2. <groupId>org.springframework.cloud</groupId>
    3. <artifactId>spring-cloud-starter-gateway</artifactId>
    4. </dependency>

    2、application.yml

    1. server:
    2. port: 8010
    3. spring:
    4. application:
    5. name: gateway
    6. cloud:
    7. gateway:
    8. discovery:
    9. locator:
    10. enabled: true
    11. routes:
    12. - id: provider_route
    13. uri: http://localhost:8081
    14. predicates:
    15. - Path=/provider/**
    16. filters:
    17. - StripPrefix=1

    上面这种做法其实没有用到 nacos ,现在我们让 gateway 直接去 nacos 中发现服务,配置更加简单了。

    1、pom.xml 引入 nacos

    1. <dependency>
    2. <groupId>org.springframework.cloud</groupId>
    3. <artifactId>spring-cloud-starter-gateway</artifactId>
    4. </dependency>
    5. <dependency>
    6. <groupId>com.alibaba.cloud</groupId>
    7. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    8. </dependency>

    2、application.yml

    1. server:
    2. port: 8010
    3. spring:
    4. application:
    5. name: gateway
    6. cloud:
    7. gateway:
    8. discovery:
    9. locator:
    10. enabled: true

    Gateway 限流

    基于路由限流

    1、pom.xml

    1. <dependency>
    2. <groupId>org.springframework.cloud</groupId>
    3. <artifactId>spring-cloud-starter-gateway</artifactId>
    4. </dependency>
    5. <dependency>
    6. <groupId>com.alibaba.csp</groupId>
    7. <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
    8. </dependency>

    2、配置类

    1. package com.godfrey.configuration;
    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. /**
    25. * @author godfrey
    26. * @since 2020-12-27
    27. */
    28. @Configuration
    29. public class GatewayConfiguration {
    30. private final List<ViewResolver> viewResolvers;
    31. private final ServerCodecConfigurer serverCodecConfigurer;
    32. public GatewayConfiguration(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. return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
    42. }
    43. //配置初始化的限流参数
    44. @PostConstruct
    45. public void initGatewayRules(){
    46. Set<GatewayFlowRule> rules = new HashSet<>();
    47. rules.add(
    48. new GatewayFlowRule("provider_route")
    49. .setCount(1)
    50. .setIntervalSec(1)
    51. );
    52. GatewayRuleManager.loadRules(rules);
    53. }
    54. //初始化限流过滤器
    55. @Bean
    56. @Order(Ordered.HIGHEST_PRECEDENCE)
    57. public GlobalFilter sentinelGatewayFilter() {
    58. return new SentinelGatewayFilter();
    59. }
    60. //自定义限流异常页面
    61. @PostConstruct
    62. public void initBlockHandlers(){
    63. BlockRequestHandler blockRequestHandler = new BlockRequestHandler() {
    64. @Override
    65. public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
    66. Map map = new HashMap();
    67. map.put("code",0);
    68. map.put("msg","被限流了");
    69. return ServerResponse.status(HttpStatus.OK)
    70. .contentType(MediaType.APPLICATION_JSON)
    71. .body(BodyInserters.fromObject(map));
    72. }
    73. };
    74. GatewayCallbackManager.setBlockHandler(blockRequestHandler);
    75. }
    76. }

    3、application.yml

    1. server:
    2. port: 8010
    3. spring:
    4. application:
    5. name: gateway
    6. cloud:
    7. gateway:
    8. discovery:
    9. locator:
    10. enabled: true
    11. routes:
    12. - id: provider_route
    13. uri: http://localhost:8081
    14. predicates:
    15. - Path=/provider/**
    16. filters:
    17. - StripPrefix=1

    基于 API 分组限流

    1、修改配置类,添加基于 API 分组限流的方法,修改初始化的限流参数

    1. package com.godfrey.configuration;
    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.ApiPredicateItem;
    6. import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
    7. import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
    8. import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
    9. import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
    10. import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
    11. import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
    12. import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
    13. import org.springframework.beans.factory.ObjectProvider;
    14. import org.springframework.cloud.gateway.filter.GlobalFilter;
    15. import org.springframework.context.annotation.Bean;
    16. import org.springframework.context.annotation.Configuration;
    17. import org.springframework.core.Ordered;
    18. import org.springframework.core.annotation.Order;
    19. import org.springframework.http.HttpStatus;
    20. import org.springframework.http.MediaType;
    21. import org.springframework.http.codec.ServerCodecConfigurer;
    22. import org.springframework.web.reactive.function.BodyInserters;
    23. import org.springframework.web.reactive.function.server.ServerResponse;
    24. import org.springframework.web.reactive.result.view.ViewResolver;
    25. import org.springframework.web.server.ServerWebExchange;
    26. import reactor.core.publisher.Mono;
    27. import javax.annotation.PostConstruct;
    28. import java.util.*;
    29. /**
    30. * @author godfrey
    31. * @since 2020-12-27
    32. */
    33. @Configuration
    34. public class GatewayConfiguration {
    35. private final List<ViewResolver> viewResolvers;
    36. private final ServerCodecConfigurer serverCodecConfigurer;
    37. public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider,
    38. ServerCodecConfigurer serverCodecConfigurer) {
    39. this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
    40. this.serverCodecConfigurer = serverCodecConfigurer;
    41. }
    42. //配置限流的异常处理
    43. @Bean
    44. @Order(Ordered.HIGHEST_PRECEDENCE)
    45. public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
    46. return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
    47. }
    48. //配置初始化的限流参数
    49. @PostConstruct
    50. public void initGatewayRules(){
    51. Set<GatewayFlowRule> rules = new HashSet<>();
    52. rules.add(new GatewayFlowRule("provider_api1").setCount(1).setIntervalSec(1));
    53. rules.add(new GatewayFlowRule("provider_api2").setCount(1).setIntervalSec(1));
    54. GatewayRuleManager.loadRules(rules);
    55. }
    56. //初始化限流过滤器
    57. @Bean
    58. @Order(Ordered.HIGHEST_PRECEDENCE)
    59. public GlobalFilter sentinelGatewayFilter() {
    60. return new SentinelGatewayFilter();
    61. }
    62. //自定义限流异常页面
    63. @PostConstruct
    64. public void initBlockHandlers(){
    65. BlockRequestHandler blockRequestHandler = new BlockRequestHandler() {
    66. @Override
    67. public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
    68. Map map = new HashMap();
    69. map.put("code",0);
    70. map.put("msg","被限流了");
    71. return ServerResponse.status(HttpStatus.OK)
    72. .contentType(MediaType.APPLICATION_JSON)
    73. .body(BodyInserters.fromObject(map));
    74. }
    75. };
    76. GatewayCallbackManager.setBlockHandler(blockRequestHandler);
    77. }
    78. //自定义API分组
    79. @PostConstruct
    80. private void initCustomizedApis(){
    81. Set<ApiDefinition> definitions = new HashSet<>();
    82. ApiDefinition api1 = new ApiDefinition("provider_api1")
    83. .setPredicateItems(new HashSet<ApiPredicateItem>(){{
    84. add(new ApiPathPredicateItem().setPattern("/provider/api1/**")
    85. .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
    86. }});
    87. ApiDefinition api2 = new ApiDefinition("provider_api2")
    88. .setPredicateItems(new HashSet<ApiPredicateItem>(){{
    89. add(new ApiPathPredicateItem().setPattern("/provider/api2/demo1"));
    90. }});
    91. definitions.add(api1);
    92. definitions.add(api2);
    93. GatewayApiDefinitionManager.loadApiDefinitions(definitions);
    94. }
    95. }

    2、Controller 添加方法

    1. @GetMapping("/api1/demo1")
    2. public String demo1() {
    3. return "demo";
    4. }
    5. @GetMapping("/api1/demo2")
    6. public String demo2() {
    7. return "demo";
    8. }
    9. @GetMapping("/api2/demo1")
    10. public String demo3() {
    11. return "demo";
    12. }
    13. @GetMapping("/api2/demo2")
    14. public String demo4() {
    15. return "demo";
    16. }

    也可以基于 Nacos 服务发现组件进行限流

    1. server:
    2. port: 8010
    3. spring:
    4. application:
    5. name: gateway
    6. cloud:
    7. gateway:
    8. discovery:
    9. locator:
    10. enabled: true

    API 分组代码修改,改为 discovery 中的服务名。

    1. ApiDefinition api2 = new ApiDefinition("provider_api2")
    2. .setPredicateItems(new HashSet<ApiPredicateItem>(){{
    3. add(new ApiPathPredicateItem().setPattern("/p1/api2/demo1"));
    4. }});