博客
关于我
Spring Cloud Gateway - 快速开始
阅读量:458 次
发布时间:2019-03-06

本文共 3774 字,大约阅读时间需要 12 分钟。

Spring Cloud Gateway 工作原理及配置实践

Spring Cloud Gateway 的工作原理

Spring Cloud Gateway 是一款功能强大且灵活的网关解决方案,主要用于对内外部服务进行路由转发和请求过滤。在实际应用中,客户端向网关发送请求时,网关会根据预定义的路由规则判断请求是否需要转发给目标服务。具体来说:

  • 路由匹配:当客户端请求到达网关时,网关会根据路由条件(如路径、主机名等)判断请求是否需要转发给目标服务。
  • 过滤器链:如果请求匹配了定义的路由,网关会执行一系列过滤器。过滤器被划分为“pre”和“post”两部分:
    • pre过滤器:执行在请求转发之前的逻辑,例如身份验证、权限校验等。
    • post过滤器:执行在请求转发之后的逻辑,例如记录日志、修改响应头等。
  • 请求转发:经过过滤器链处理后,网关会将请求转发给目标服务进行进一步处理。
  • 如何启动 Spring Cloud Gateway

    在开始使用 Spring Cloud Gateway 之前,需要先完成以下操作:

  • 创建 Maven 项目并添加依赖

    • 新建一个 Maven 工程,添加必要的依赖项。建议将 spring-cloud-gateway 和 spring-cloud-starter-gateway 作为主要依赖。
    • 项目根目录下的 pom.xml 文件应包含以下内容:
      org.springframework.cloud
      spring-cloud-gateway
      2.0.0.RELEASE
      import
      org.springframework.cloud
      spring-cloud-starter-gateway
      2.0.0.RELEASE
  • 创建启动类

    • Application.java 中定义启动类,确保类注解 @SpringBootApplication 存在。
    import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}
  • 运行应用

    • 通过 mvn spring-boot:run 启动应用,默认情况下会在 http://localhost:8080 上运行。
    • 访问 http://localhost:8080/ 会提示 404 错误,日志中会显示类似的错误信息。
  • 配置服务路由:配置文件方式

    通过配置文件实现路由配置是一个直观且常用的方法。假设你有本地运行的两个 Spring Boot 服务(如服务A在 http://localhost:8081 和服务B在 http://localhost:8082),可以按照以下步骤配置路由:

  • 添加配置文件

    • resources 目录下创建 application.yml 文件,添加如下路由配置:
      spring:    cloud:        gateway:            routes:                - id: host_route                    uri: http://localhost:8081                    predicates:                        - Path=/a/**                    filters:                        - StripPrefix=1                - id: host_route                    uri: http://localhost:8082                    predicates:                        - Path=/b/**                    filters:                        - StripPrefix=1
  • 重启 Gateway 服务

    • 退出当前运行的 Gateway 服务,重新启动应用。
  • 测试路由配置

    • 访问 http://localhost:8080/a/ 应能成功路由到 http://localhost:8081/
    • 访问 http://localhost:8080/b/ 同样应能成功路由到 http://localhost:8082/
    • 更复杂的路径如 http://localhost:8080/a/user/all 也会正确路由到目标服务。
  • 配置服务路由:编码方式

    除了配置文件方式,还可以通过编码实现路由配置,这种方法适合需要自定义路由逻辑的场景:

  • 删除配置文件

    • application.yml 中删除相关路由配置。
  • 修改启动类

    • Application.java 中添加自定义路由配置,确保类路径正确:
      import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.gateway.filter.factory.StripPrefixGatewayFilterFactory;import org.springframework.cloud.gateway.route.RouteLocator;import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;import org.springframework.context.annotation.Bean;@SpringBootApplicationpublic class Application {    @Bean    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {        StripPrefixGatewayFilterFactory.Config config = new StripPrefixGatewayFilterFactory.Config();        config.setParts(1);        return builder.routes()                .route("host_route", r -> r.path("/a/**").filters(f -> f.stripPrefix(1)).uri("http://localhost:8081"))                .route("host_route", r -> r.path("/b/**").filters(f -> f.stripPrefix(1)).uri("http://localhost:8082"))                .build();    }    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}
  • 其他功能

    Spring Cloud Gateway 提供了丰富的路由规则和过滤器选项,例如:

    • 时间规则:基于时间的路由控制。
    • 主机规则:根据主机名或域名进行路由。
    • 标头规则:通过自定义标头进行路由控制。
    • 过滤器:包括但不限于 AddRequestHeaderAddRequestParameterAddResponseHeader 等,帮助开发者实现更复杂的网关逻辑。

    通过合理配置,可以在短时间内搭建一个功能强大的网关服务,满足各种微服务架构的需求。

    转载地址:http://cpjuz.baihongyu.com/

    你可能感兴趣的文章
    python判断字符串包含中文_Python 判断字符串是否包含中文
    查看>>
    python删除第一行_Python 乱码指北:一行删掉根目录
    查看>>
    Python删除列表元素的三种方法
    查看>>
    python初步学习-python数据类型-集合(set)
    查看>>
    python列表生成字典_Python中将字典转换为列表的方法
    查看>>
    python列表对应元素合并为列表及判断一个列表是几维
    查看>>
    python列表去重复后按照顺序_从包含不可共元素的Python列表中删除重复元素,同时保留顺序?...
    查看>>
    python列表前几个_python之列表
    查看>>
    python列表元组
    查看>>
    Python列表/元组/字典和集合使用
    查看>>
    python列表 行列选择_python_pandas_dataframe_行列选择_切片操作
    查看>>
    python列表
    查看>>
    python列出当前目录、子目录和文件的脚本
    查看>>
    python+flask计算机毕业设计骨科门诊患者档案管理系统(程序+开题+论文)
    查看>>
    python+flask计算机毕业设计高校体测管理系统的设计与实现(程序+开题+论文)
    查看>>
    Python切片对象和__getitem__
    查看>>
    python+flask计算机毕业设计高校体育竞赛成绩管理系统的设计与实现(程序+开题+论文)
    查看>>
    python+flask计算机毕业设计高校体育赛事系统(程序+开题+论文)
    查看>>
    python+flask计算机毕业设计高校体育运动会比赛系统(程序+开题+论文)
    查看>>
    python+flask计算机毕业设计高校危化品管理系统(程序+开题+论文)
    查看>>