spring cloud官网:-【从入门到精通Spring Cloud】统一服务入口Spring Cloud Gateway - SpringApplication
文章目录
1. 什么是API⽹关2. Spring Cloud Gateway 快速上手✏️2.1 创建网关项目✏️2.2 引入网关依赖✏️2.3 编写启动类✏️2.4 添加Gateway路由配置
3. Predicate接口✏️3.1 Predicate代码演示✏️3.2 Predicate其他方法
4. Route Predicate Factories5. Gateway Filter Factories✏️5.1 GatewayFilter✏️5.2 GlobalFilter
6. 过滤器执⾏顺序
特此注明 : Designed By :长安城没有风 Version:1.0 Time:2025.09.21 Location:辽宁 · 大连
1. 什么是API⽹关
API⽹关(简称⽹关)也是⼀个服务,通常是后端服务的唯⼀⼊⼝,它的定义类似设计模式中的Facade模式(⻔⾯模式,也称外观模式)。它就类似整个微服务架构的⻔⾯,所有的外部客⼾端访问,都需要经过它来进⾏调度和过滤。 、
例如企业管理:外部⼈员去公司办理业务,公司需要先核实对⽅的⾝份再去进⾏办理。最开始只有⼀个员⼯,这个员⼯核实之后直接办理即可(单体架构)。随着公司的发展,划分了多个部⻔,每个部⻔负责的事情不同,每个部⻔都需要先核实对⽅的⾝份再进⾏办理(微服务架构)。这个流程不仅办事效率低,还增加了员工的工作流程。所以我们对此改进,设⽴前台,统⼀由前台来进⾏⾝份的校验,前台⾝份校验通过后,其他部⻔就设置信任,直接办理。前台就类似API⽹关。
网关核心功能
权限控制:作为微服务的⼊⼝,对用户进⾏权限校验,如果校验失败则进⾏拦截。动态路由:⼀切请求先经过⽹关,但⽹关不处理业务,⽽是根据某种规则,把请求转发到某个微服务。负载均衡:当路由的⽬标服务有多个时,还需要做负载均衡。限流 : 请求流量过⾼时,按照⽹关中配置微服务能够接受的流量进⾏放⾏,避免服务压⼒过⼤。
2. Spring Cloud Gateway 快速上手
✏️2.1 创建网关项目
✏️2.2 引入网关依赖
org.springframework.cloud
spring-cloud-starter-gateway
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.cloud
spring-cloud-starter-loadbalancer
✏️2.3 编写启动类
package com.SpringDemo.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class,args);
}
}
✏️2.4 添加Gateway路由配置
server:
port: 10010 #网关端口
spring:
application:
name: gateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
gateway:
routes: #网关路由配置
- id: product-service #路由ID, ⾃定义, 唯⼀即可
uri: lb://product-service #⽬标服务地址
predicates: #路由条件
- Path=/product/**
- id: order-service
uri: lb://order-service #⽬标服务地址
predicates: #路由条件
- Path=/order/**
配置字段说明
id : ⾃定义路由ID,保持唯⼀。uri : ⽬标服务地址,⽀持普通URI 及 lb://应⽤注册服务名称。lb表⽰负载均衡,使⽤ lb:// ⽅式表⽰从注册中⼼获取服务地址。predicates : 路由条件,根据匹配结果决定是否执⾏该请求路由,上述代码中,我们把符合Path规则的⼀切请求,都代理到uri参数指定的地址。
3. Predicate接口
Predicate是Java 8提供的⼀个函数式编程接⼝,它接收⼀个参数并返回⼀个布尔值,⽤于条件过滤,请求参数的校验。
@FunctionalInterface
public interface Predicate{
boolean test(T t);
//...
}
✏️3.1 Predicate代码演示
@SpringBootTest
public class GatewayApplicationTests {
@Test
//判断某个字符串是否以a开头,lambda表达式
public void test1() {
Predicatepredicate = (s) ->s.startsWith("a");
System.out.println(predicate.test("abc"));
System.out.println(predicate.test("bcd"));
}
@Test
//判断某个字符串是否以a开头,匿名内部类
public void test2() {
Predicatepredicate = new Predicate() {
@Override
public boolean test(String s) {
return s.startsWith("a");
}
};
System.out.println(predicate.test("abc"));
System.out.println(predicate.test("bcd"));
}
}
✏️3.2 Predicate其他方法 拼多多帮助力入口
方法名称方法说明isEqual(Object targetRef)⽐较两个对象是否相等,参数可以为Nulland(Predicate other)短路与操作,返回⼀个组成Predicateor(Predicate other)短路或操作,返回⼀个组成Predicatenegate()返回表⽰此Predicate逻辑否定的Predicate
4. Route Predicate Factories
Route Predicate Factories (路由断⾔⼯⼚,也称为路由谓词⼯⼚,此处谓词表⽰⼀个函数),在Spring Cloud Gateway中,Predicate提供了路由规则的匹配机制。我们在配置⽂件中写的断⾔规则只是字符串,这些字符串会被Route Predicate Factory读取并处理,转变为路由判断的条件。⽐如前⾯配置的 Path=/product/, 就是通过Path属性来匹配URL前缀是 /product 的请求。Spring Cloud Gateway 默认提供了很多Route Predicate Factory,这些Predicate会分别匹配HTTP请求的不同属性,并且多个Predicate可以通过and逻辑进⾏组合。更多断言请查询官方文档
名称说明After匹配指定⽇期之后的请求Before匹配指定⽇期之前的请求Between匹配两个指定时间之间的请求datetime2 的参数必须在datetime1 之后Cookie请求中包含指定Cookie, 且该Cookie值符合指定的正则表达式Header请求中包含指定Header, 且该Header值符合指定的正则表达式Host请求必须是访问某个host(根据请求中的Host字段进⾏匹配)Method匹配指定的请求⽅式Path匹配指定规则的路径RemoteAddr请求者的IP必须为指定范围
5. Gateway Filter Factories
路由(Routing):核心是 “判断数据往哪走”,在数据被允许通过后,根据目标地址等信息,计算并选择最优的转发路径。过滤(Filtering):核心是 “判断是否允许数据通过”,基于预设规则(如 IP 地址、端口、协议、内容特征等)对数据包或请求进行检查,决定 “放行” 或 “阻断”。
Filter分为两种类型: Pre类型和Post类型
Pre类型过滤器: 路由处理之前执⾏(请求转发到后端服务之前执⾏),在Pre 类型过滤器中可以做鉴权,限流等。Post类型过滤器: 请求执⾏完成后,将结果返回给客⼾端之前执⾏。
✏️5.1 GatewayFilter
GatewayFilter: 应⽤到单个路由或者⼀个分组的路由上。GatewayFilter 同 Predicate 类似,都是在配置⽂件 application.yml 中配置,每个过滤器的逻辑都是固定的。⽐如 AddRequestParameterGatewayFilterFactory 只需要在配置⽂件中写 AddRequestParameter ,就可以为所有的请求添加⼀个参数,我们先通过⼀个例⼦来演⽰GatewayFilter如何使⽤。
在application.yml中添加filter
server:
port: 10010 #网关端口
spring:
application:
name: gateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
gateway:
routes: #网关路由配置
- id: product-service #路由ID, ⾃定义, 唯⼀即可
uri: lb://product-service #⽬标服务地址
predicates: #路由条件
- Path=/product/**
fliters:
- AddRequestParameter=userName, test
- id: order-service
uri: lb://order-service #⽬标服务地址
predicates: #路由条件
- Path=/order/**
GatewayFilter说明 Spring Cloud Gateway提供了的Filter⾮常多, 下⾯列出⼀些常⻅过滤器的说明.更多过滤请查询官方文档
✏️5.2 GlobalFilter
GlobalFilter是Spring Cloud Gateway中的全局过滤器,它和GatewayFilter的作⽤是相同的GlobalFilter 会应⽤到所有的路由请求上。详细内容查看官方文档
6. 过滤器执⾏顺序
⼀个项⽬中,既有GatewayFilter,⼜有 GlobalFilter时,执⾏的先后顺序是什么呢?
请求路由后,⽹关会把当前项⽬中的GatewayFilter和GlobalFilter合并到⼀个过滤器链(集合)中,并进⾏排序,依次执⾏过滤器。
每⼀个过滤器都必须指定⼀个int类型的order值,默认值为0,表⽰该过滤的优先级,order值越⼩,优先级越⾼,执⾏顺序越靠前。
Filter通过实现Order接⼝或者添加@Order注解来指定order值。Spring Cloud Gateway提供的Filter由Spring指定,用户也可以⾃定义Filter,由用户指定。当过滤器的order值⼀样时,会按照 defaultFilter > GatewayFilter > GlobalFilter的顺序执⾏。