SpringCloud组件之网关Spring Cloud Gateway(Hoxton版本)

    技术2025-06-02  22

    目录

     

    1.Spring Cloud Gateway简介

    1.1 相关特性

    1.2 相关概念

    2.创建Spring Cloud Gateway工程

    2.1在pom中添加相关依赖

    2.2 spring cloud gateway路由配置有两种方法

    2.2.1配置文件方式

    2.2.2 JAVA BEAN配置如下

    2.3启动程序,进行路由测试

    3.项目git地址

    4.参考


    1.Spring Cloud Gateway简介

    Spring Cloud Gateway 为 SpringBoot 应用提供了API网关支持,具有强大的智能路由与过滤器功能,本文将对其用法进行详细介绍。   Spring Cloud Gateway是SpringCloud新推出的网关框架,比较于上一代Zuul,功能和性能有很大的提升。Zuul1.x采用的是阻塞多线程方式,也就是一个线程处理一个连接请求,高并发情况下性能较差,即使是Zuul2.x虽然做到了非阻塞,但是面对连续跳票,看起来Zuul要被抛弃了。取而代之的是Spring Cloud Gateway,Spring Cloud Gateway是基于Webflux,是一个非阻塞异步的框架,性能上有很大提升,而且包含了Zuul的所有功能,可以从Zuul无缝切换到Spring Cloud Gateway。   Spring Cloud Gateway是在Spring生态系统之上构建的API网关服务,基于Spring 5,Spring Boot 2和 Project Reactor等技术。Gateway旨在提供一种简单而有效的方式来对API进行路由,以及提供一些强大的过滤器功能, 例如:熔断、限流、重试等。

     

    官网Gateway的工作原理图如下:

     

     

    1.1 相关特性

    Spring Cloud Gateway 具有如下特性:

    基于Spring Framework 5, Project Reactor 和 Spring Boot 2.0 进行构建;动态路由:能够匹配任何请求属性;可以对路由指定 Predicate(断言)和 Filter(过滤器);集成Hystrix的断路器功能;集成 Spring Cloud 服务发现功能;易于编写的 Predicate(断言)和 Filter(过滤器);请求限流功能;支持路径重写。

    1.2 相关概念

    Route(路由):路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由;Predicate(断言):指的是Java 8 的 Function Predicate。 输入类型是Spring框架中的ServerWebExchange。 这使开发人员可以匹配HTTP请求中的所有内容,例如请求头或请求参数。如果请求与断言相匹配,则进行路由;Filter(过滤器):指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前后对请求进行修改。

    2.创建Spring Cloud Gateway工程

    2.1在pom中添加相关依赖

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-cloud-demo</artifactId> <groupId>com.hxmec</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-cloud-gateway</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--使用Eureka注册中心--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- SpringCloud Hystrix 微服务容错监控组件:断路器,依赖隔离,服务降级,服务监控 依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!-- 健康检查 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 复制代码

    创建启动类GateWayApplication,启动类代码如下:

    @SpringBootApplication @EnableDiscoveryClient public class GateWayApplication { public static void main(String[] args) { SpringApplication.run(GateWayApplication.class, args); } } 复制代码

    2.2 spring cloud gateway路由配置有两种方法

    配置文件方式Java Bean方式

    2.2.1配置文件方式

    增加配置文件bootstrap.yml,内容如下:

    server: port: 9100 eureka: instance: #每隔5s发送一次心跳 lease-renewal-interval-in-seconds: 5 #告知服务端10秒还未收到心跳的话,就将该服务移除列表 lease-expiration-duration-in-seconds: 10 #健康检查路径 health-check-url-path: /actuator/health client: #默认为30秒 registry-fetch-interval-seconds: 5 serviceUrl: #eureka注册中心地址 defaultZone: http://localhost:8888/eureka/ spring: application: name: api-gateway cloud: gateway: discovery: locator: #开启从注册中心动态创建路由的功能 enabled: true #使用小写服务名,默认是大写 lower-case-service-id: true routes: - id: eureka-client-provider #路由的ID uri: lb://eureka-client-provider predicates: - Path=/provider/** # 路由规则 filters: - StripPrefix=1 - id: eureka-client-consumer #路由的ID uri: lb://eureka-client-consumer predicates: - Path=/consumer/** # 路由规则 filters: - StripPrefix=1 复制代码

    2.2.2 JAVA BEAN配置如下

    增加一个JAVA类GatewayConfig,用于配置路由

    @Configuration public class GatewayConfig { @Bean public RouteLocator customerRouteLocator(RouteLocatorBuilder builder) { return builder.routes () .route (r -> r.path ("/provider/**") .filters (f -> f.stripPrefix (1)) .uri ("lb://eureka-client-provider") .id ("eureka-client-provider") ) .route (r -> r.path ("/consumer/**") .filters (f -> f.stripPrefix (1)) .uri ("lb://eureka-client-consumer") .id ("eureka-client-consumer") ) .build (); } } 复制代码

    2.3启动程序,进行路由测试

    启动此前文中的spring-cloud-eureka-server,spring-cloud-eureka-client-provider,spring-cloud-eureka-client-consumer,以及本文总创建的spring-cloud-gateway工程。 测试地址如下:

    http://localhost:9100/provider/demo/hello

    http://localhost:9100/consumer/test/callHello

     

    3.项目git地址

    GitHub https://github.com/ty1972873004/spring-cloud-demo

    4.参考

    【官方文档】https://cloud.spring.io/spring-cloud-gateway/2.2.x/reference/html/#the-weight-route-predicate-factory
    Processed: 0.010, SQL: 9