什么是SpringCloud Gateway

SpringCloud GateWay是Spring官方基于Spring5.0, SpringBoot2.0,SpringWebFlux, Project Reactor技术开发的高性能网关,旨在为微服务架构提供简单、有效且统一的API 路由管理方式。
整体工作架构如下:
image.png
核心组件说明

  • Route(路由):代表转发规则,由id, 目标url, 一组断言工厂和一组过滤器组成。如果断言为真,说明请求的URL与配置的路由匹配
  • Predict(断言):断言函数,与Java8中的Predict语义一致
  • Filter(过滤器):代表一个标准的Spring WebFilter. Gateway中有2种Filter,GlobalFilter,全局范围生效,GatewayFilter,在路由上生效。

工作原理

SpringCloud-Gateway工作原理.png

简单使用

依赖引入

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.6.6</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example.springcloud</groupId>
  12. <artifactId>gateway</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springcloud-gateway</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>2021.0.1</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-actuator</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.cloud</groupId>
  27. <artifactId>spring-cloud-starter-gateway</artifactId>
  28. </dependency>
  29. <!-- 如果需要把Gateway注册到Eureka上,引入eureka-client -->
  30. <dependency>
  31. <groupId>org.springframework.cloud</groupId>
  32. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-test</artifactId>
  37. <scope>test</scope>
  38. </dependency>
  39. </dependencies>
  40. <dependencyManagement>
  41. <dependencies>
  42. <dependency>
  43. <groupId>org.springframework.cloud</groupId>
  44. <artifactId>spring-cloud-dependencies</artifactId>
  45. <version>${spring-cloud.version}</version>
  46. <type>pom</type>
  47. <scope>import</scope>
  48. </dependency>
  49. </dependencies>
  50. </dependencyManagement>
  51. <build>
  52. <plugins>
  53. <plugin>
  54. <groupId>org.springframework.boot</groupId>
  55. <artifactId>spring-boot-maven-plugin</artifactId>
  56. </plugin>
  57. </plugins>
  58. </build>
  59. </project>

路由配置

代码方式

  1. /***
  2. * 通过代码API配置路由
  3. * @param builder
  4. * @return
  5. */
  6. @Bean
  7. public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
  8. return builder.routes().route(r ->
  9. r.path("/greet") // 满足Predict
  10. .filters(f -> f.filter(new CustomGateFilter(), 0))
  11. .uri("http://localhost:8080/greet")) // 转发到对应的URI
  12. .build();
  13. }
  14. public static class CustomGateFilter implements GatewayFilter {
  15. private static final Log log = LogFactory.getLog(GatewayFilter.class);
  16. private static final String countStartTime = "countStartTime";
  17. @Override
  18. public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
  19. exchange.getAttributes().put(countStartTime, System.currentTimeMillis());
  20. return chain.filter(exchange).then(
  21. Mono.fromRunnable(() -> {
  22. Long startTime = exchange.getAttribute(countStartTime);
  23. if (startTime != null) {
  24. log.info(exchange.getRequest().getURI().getRawPath() + ":" + (System.currentTimeMillis() - startTime));
  25. }
  26. })
  27. );
  28. }
  29. }

YML配置方式

  1. spring:
  2. application:
  3. name: spring-cloud-gateway
  4. cloud:
  5. gateway:
  6. routes:
  7. - id: taobao_route
  8. uri: http://www.taobao.com
  9. predicates:
  10. - Path=/tb
  11. discovery:
  12. locator:
  13. enabled: true
  14. lowerCaseServiceId: true