Spring Cloud网关详解(作用功能及原理使用)

Spring Cloud网关详解(作用功能及原理使用)-mikechen

Spring Cloud网关定义

Spring Cloud Gateway是一个基于Spring 和Spring Boot构建的轻量级网关服务,用于构建微服务架构中的API网关。

 

Spring Cloud网关作用

传统的单体架构中只需要开放一个服务给客户端调用,但是微服务架构中是将一个系统拆分成多个微服务。

每一个微服务都会部署到内网服务器中,或者禁止外部访问这些端口,这是对应用的一种安全保护机制。

因此,我们如果想通过互联网来访问这些服务,需要一个统一的入口:这就Gateway服务网关。

 

Spring Cloud网关功能

Spring Cloud Gateway的主要特点和功能如下:

Spring Cloud网关详解(作用功能及原理使用)-mikechen

1.动态路由

Spring Cloud Gateway支持动态路由,可以根据不同的请求路径将请求转发到不同的目标服务。

2.过滤器

Spring Cloud Gateway提供了一系列的过滤器,过滤器可以实现鉴权、请求转换、请求限流、日志记录等功能。

3.负载均衡

Spring Cloud Gateway集成了负载均衡的功能,可以根据配置的负载均衡策略将请求分发到多个实例的目标服务上,实现服务间的负载均衡。

4.集成服务发现

Spring Cloud Gateway可以与服务注册中心(如Eureka、Consul)进行集成,自动获取注册的服务信息,并进行路由转发。

5.安全认证

Spring Cloud网关提供了多种方式来实现安全认证和授权,Spring Cloud网关可以与Spring Security、OAuth 2.0等框架的集成。

 

Spring Cloud网关原理

Spring Cloud Gateway网关原理,如下图所示:

Spring Cloud网关详解(作用功能及原理使用)-mikechen

Spring Cloud Gateway的执行流程如下:

  1. 客户端发送请求: 客户端向Spring Cloud Gateway发送HTTP请求。
  2. 路由匹配: Gateway根据配置的路由规则,对请求进行路由匹配。
  3. 过滤器执行: 根据路由规则匹配成功后,请求将通过一系列的过滤器链进行处理。
  4. 调用目标服务: 经过过滤器链的处理后,Gateway将请求转发给目标服务。
  5. 目标服务处理: 目标服务接收到请求后,进行相应的业务处理,并生成响应结果。
  6. 响应返回: 目标服务将处理完成的响应结果返回给Gateway。
  7. 过滤器链反向执行: Gateway接收到目标服务的响应结果后,通过过滤器链对响应进行处理和修改。
  8. 返回响应给客户端: 经过过滤器链的处理后,Gateway将最终的响应结果返回给客户端。

 

Spring Cloud网关使用示例

下面是一个简单的Spring Cloud网关使用示例:

1.添加Spring Cloud Gateway的依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

 

2.创建路由配置

在项目的配置文件,比如:application.properties中添加路由配置,指定请求的匹配规则和目标服务的转发地址。

spring:
  cloud:
    gateway:
      routes:
        - id: user-route
          uri: http://user-service
          predicates:
            - Path=/api/user/**

 

3.启用Spring Cloud Gateway

在Spring Boot应用程序的启动类上添加@EnableGateway注解,启用Spring Cloud Gateway的功能。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.config.GatewayAutoConfiguration;
import org.springframework.cloud.gateway.config.GatewayLoadBalancerClientAutoConfiguration;
import org.springframework.cloud.gateway.config.GatewayReactiveLoadBalancerAutoConfiguration;
import org.springframework.cloud.gateway.config.GatewayReactiveSecurityAutoConfiguration;
import org.springframework.cloud.gateway.config.GatewaySecurityAutoConfiguration;

@SpringBootApplication(exclude = { GatewayAutoConfiguration.class,
    GatewayLoadBalancerClientAutoConfiguration.class,
    GatewayReactiveLoadBalancerAutoConfiguration.class,
    GatewayReactiveSecurityAutoConfiguration.class,
    GatewaySecurityAutoConfiguration.class })
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

启动应用程序,并访问配置的路由规则所匹配的URL,例如 /api/user。请求将被Spring Cloud Gateway捕获,并根据配置的路由规则转发到目标服务。

以上就是Spring Cloud网关详解,更多内容请查看:Spring Cloud教程(史上最全图文详解)

作者简介

陈睿|mikechen,10年+大厂架构经验,BAT资深面试官,就职于阿里巴巴、淘宝、百度等一线互联网大厂。

👇阅读更多mikechen架构文章👇

阿里架构 |双11秒杀 |分布式架构 |负载均衡 |单点登录 |微服务 |云原生 |高并发 |架构师

以上

关注作者「mikechen」公众号,获取更多技术干货!

后台回复架构,即可获取《阿里架构师进阶专题全部合集》,后台回复面试即可获取《史上最全阿里Java面试题总结

评论交流
    说说你的看法