Spring Cloud Gateway可扩展的微服务网关使用教程
创始人
2025-07-06 02:21:25
0

Spring Cloud Gateway 是一个基于 Spring Boot 2.x 的可扩展的微服务网关,它提供了一种简单且灵活的方式来构建微服务架构中的 API 网关。Spring Cloud Gateway 专注于提供 API 网关所需的核心功能,如路由、断路器、限流等,同时支持自定义扩展点,以便用户能够根据自身需求进行定制。

下面我们将通过一个简单的示例来详细介绍 Spring Cloud Gateway 的使用。

添加依赖

首先,在我们的项目中添加 Spring Cloud Gateway 的依赖。在 pom.xml 文件中添加如下依赖:


    org.springframework.cloud
    spring-cloud-starter-gateway
    3.1.3

这里我们使用的是 Spring Cloud Gateway 的 3.1.3 版本。

配置路由规则

在 src/main/resources 目录下创建一个 application.yml 文件,用于配置路由规则。例如,我们定义两个服务 service-a 和 service-b,并设置相应的路由规则:

spring:
  cloud:
    gateway:
      routes:
        - id: route_a
          uri: http://service-a/api
          predicates:
            - Path=/api/a/**
        - id: route_b
          uri: http://service-b/api
          predicates:
            - Path=/api/b/**

在这个例子中,我们定义了两个路由规则。route_a 规则将 /api/a/** 路径的请求转发到 http://service-a/api,route_b 规则将 /api/b/** 路径的请求转发到 http://service-b/api。

自定义扩展点

Spring Cloud Gateway 提供了许多内置的扩展点,允许用户根据需要进行定制。例如,我们可以实现org.springframework.cloud.gateway.handler.predicate.PredicateFactory 接口来定义新的路由规则条件。

这里我们创建一个自定义的路由规则条件 MyCustomPredicate,用于判断请求是否满足某些特定的条件:

package com.example.myservice.gateway;

import org.springframework.cloud.gateway.handler.predicate.PredicateFactory;
import org.springframework.cloud.gateway.handler.predicate.RoutePredicate;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.gateway.route.builder.routes.RouteLocator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Configuration
public class MyGatewayConfig {
    @Bean
    public RoutePredicate myCustomPredicate(PredicateFactory predicateFactory) {
        return predicateFactory.fromState(ServerWebExchange::getRequest, ServerHttpRequest::getURI)
                ::equals(ServerHttpRequest::getURI) // 这里简单判断请求的 URI 是否与目标 URI 相等
                .then(Mono::just); // 如果相等,返回 Mono 类型的 true
    }
}

在这个例子中,我们定义了一个 MyCustomPredicate 类,实现了 RoutePredicate 接口。在 myCustomPredicate 方法中,我们通过ServerWebExchange::getRequest 和 ServerHttpRequest::getURI 方法获取请求的信息,并进行简单的判断。如果请求的 URI 与目标 URI 相等,返回 Mono 类型的 true。这样,我们就可以将这个条件应用于路由规则中。

启动网关

在完成上述配置后,我们可以启动 Spring Cloud Gateway 网关。启动方法与普通的 Spring Boot 应用类似,只需运行 mvn spring-boot:run 命令即可。

Spring Cloud Gateway 启动后,会监听默认的端口 8080。如果需要修改端口号,可以在 application.yml 文件中设置 server.port 属性。

路由测试

我们可以通过发送 HTTP 请求来测试路由规则是否生效。例如,可以借助 Postman 或curl命令来进行测试。

对于上述示例中的路由规则,我们可以分别发送以下请求:

  • 请求 route_a 路由规则:
curl -X GET http://localhost:8080/api/a/hello
  • 请求 route_b 路由规则:
curl -X GET http://localhost:8080/api/b/hello

如果一切正常,你应该能够分别获得来自 service-a 和 service-b 的响应结果。

自定义扩展点使用

在上述示例中,我们创建了一个自定义的路由规则条件 MyCustomPredicate。要使用这个条件,我们需要在 application.yml 文件中添加以下配置:

spring:
  cloud:
    gateway:
      routes:
        - id: route_with_custom_predicate
          uri: http://service-a/api
          predicates:
            - MyCustomPredicate=true

在这个例子中,我们创建了一个新的路由规则route_with_custom_predicate,并添加了 MyCustomPredicate=true 条件。这意味着只有当请求满足 MyCustomPredicate 条件时,才会转发请求到目标服务。

日志与监控

Spring Cloud Gateway 还提供了丰富的日志和监控功能。你可以通过配置 logging.level.* 和 management.endpoint.* 等属性来启用并定制日志和监控行为。例如,在 application.yml 文件中添加以下配置:

logging:
  level:
    root: INFO
management:
  endpoint:
    health:
      show-details: always

这样,你就能在日志中看到更详细的路由、断路器、限流等信息,并可以通过 /health 接口查看网关的健康状态。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...