【Spring实战】31 Spring Boot3 集成 Gateway 微服务网关
作者:mmseoamin日期:2024-02-04

文章目录

    • 1. 定义
    • 2. 功能
    • 3. 示例代码
      • 1) 创建一个业务服务
      • 2)创建一个网关服务
      • 3)启动服务
      • 4)验证
      • 4. 代码参考
      • 结语

        1. 定义

        Spring Cloud Gateway 是一个基于 Spring Framework 的开源网关服务,用于构建微服务架构中的 API 网关。它提供了一种灵活的方式来路由请求、过滤请求以及对请求进行各种操作,从而实现对微服务的集中控制、安全性、监控等功能。

        2. 功能

        Spring Cloud Gateway 提供了丰富的功能,包括但不限于:

        • 动态路由: 根据配置动态地将请求路由到不同的微服务实例
        • 过滤器: 实现对请求和响应的各种操作,例如认证、授权、请求转发、限流等
        • 集成负载均衡: 通过集成负载均衡器,将请求分发到多个服务实例,提高系统的性能和可用性
        • 断路器支持: 处理微服务中的故障和延迟,防止故障扩散
        • 统一认证和授权: 通过集成 Spring Security 等机制,实现对微服务的统一认证和授权管理
        • 监控和日志: 提供监控和日志功能,帮助理解网关的运行状况,分析请求流量

          3. 示例代码

          下面是一个简单的 Spring Boot 项目,演示如何集成 Spring Cloud Gateway。

          【Spring实战】31 Spring Boot3 集成 Gateway 微服务网关,在这里插入图片描述,第1张

          1) 创建一个业务服务

          首先,我们需要提前使用 Spring boot 创建一个普通的业务服务,并且创建一个 REST 接口,调用 /hello 返回一个 Hello world

          pom.xml

                  
                      org.springframework.boot
                      spring-boot-starter-web
                  
          

          application.yml

          server:
            port: 9501
            servlet:
              context-path: /account
          

          访问 API - HelloController.java

          package com.cheney.koala.account.controller;
          import org.springframework.web.bind.annotation.GetMapping;
          import org.springframework.web.bind.annotation.RestController;
          @RestController
          public class HelloController {
              @GetMapping("hello")
              public String hello() {
                  return "Hello world";
              }
          }
          

          启动类 - KoalaAccountApplication.java

          package com.cheney.koala.account;
          import org.springframework.boot.SpringApplication;
          import org.springframework.boot.autoconfigure.SpringBootApplication;
          @SpringBootApplication
          public class KoalaAccountApplication {
              public static void main(String[] args) {
                  SpringApplication.run(KoalaAccountApplication.class, args);
              }
          }
          

          2)创建一个网关服务

          然后,我们需要再使用 Spring boot 创建一个网关服务,并且配置一下路由转发

          pom.xml

                      
                          org.springframework.cloud
                          spring-cloud-dependencies
                          2022.0.0
                          pom
                          import
                      
                      
                          org.springframework.cloud
                          spring-cloud-starter-gateway
                      
          

          application.yml

          server:
            port: 9500
          spring:
            cloud:
              gateway:
                routes:
                  - id: account
                    uri: http://127.0.0.1:9501
                    predicates:
                      - Path=/account/**
          

          启动类 - KoalaGatewayApplication.java

          package com.cheney.koala.gateway;
          import org.springframework.boot.SpringApplication;
          import org.springframework.boot.autoconfigure.SpringBootApplication;
          @SpringBootApplication
          public class KoalaGatewayApplication {
              public static void main(String[] args) {
                  SpringApplication.run(KoalaGatewayApplication.class, args);
              }
          }
          

          这个简单的示例配置了一个路由,将以 “/account/**” 开头的请求转发到 “http://127.0.0.1:9501”。

          3)启动服务

          分别启动两个服务(业务服务和网关服务)

          启动业务服务

          【Spring实战】31 Spring Boot3 集成 Gateway 微服务网关,在这里插入图片描述,第2张

          启动网关服务

          【Spring实战】31 Spring Boot3 集成 Gateway 微服务网关,在这里插入图片描述,第3张

          4)验证

          首先,我们先直接访问业务服务,看一下效果

          http://localhost:9501/account/hello

          【Spring实战】31 Spring Boot3 集成 Gateway 微服务网关,在这里插入图片描述,第4张

          然后,我们再通过网关服务访问,看一下效果

          http://localhost:9500/account/hello

          【Spring实战】31 Spring Boot3 集成 Gateway 微服务网关,在这里插入图片描述,第5张

          通过这个简单的示例,你可以快速了解 Spring Cloud Gateway 的基本用法,以及如何配置和运行一个最最基本的网关服务。

          4. 代码参考

          https://gitee.com/cheney09/koala-system

          结语

          Spring Cloud Gateway 提供了一个强大而灵活的工具,用于构建微服务架构中的 API 网关。通过合理配置,你可以实现路由、过滤、负载均衡等功能,为微服务架构提供了更好的可维护性和可扩展性。在实际项目中,可以根据具体需求进一步定制和优化配置,以满足项目的特定要求。希望这篇博客能够帮助你入门 Spring Cloud Gateway。