共计 7240 个字符,预计需要花费 19 分钟才能阅读完成。
1、认识路由的谓词接口和谓词工厂
在 Java 8 中引入了一个函数接口 Predicate,它接收一个输入参数并返回一个布尔值结果。
Spring Cloud Gateway 通过 Predicate 接口组合简单条件来判断当前路由是否满足给定条件。该接口包含许多默认方法,用于将 Predicate 组合成复杂逻辑,以校验请求参数、判断数据是否改变、是否需要更新等。
使用路由的流程:
1、加载路由中的 Predicate
2、用 Predicate 判断路由是否可用
2、认识配置路由规则的方式
2.1、通过 Java API 的方式
直接通过 RouteLocatorBuilder 接口来构建路由规则。
public RouteLocator customRouteLocator(RouteLocatorBuilder builder){return builder.routes().route("hello", | |
r->r.path("/hello/**") | |
.uri("http://localhost:500010")) | |
.build();} |
2.2、通过配置文件的方式
可以通过 application.properties 或 application.yml 来配置路由规则。
1、application.yml
server: | |
port: 50010 | |
spring: | |
cloud: | |
gateway: | |
routes: | |
- id: hello | |
uri: http://localhost:50010 | |
predicates: | |
- Path=/hello/** |
2、application.properties
#id: 自定义路由 ID | |
spring.cloud.gateway.routes[0].id=hello | |
#uri: 目标服务地址 | |
spring.cloud.gateway.routes[0].uri=http://localhost:50010 | |
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑 | |
spring.cloud.gateway.routes[0].predicates[0]=Path=/hello/** |
3、配置文件方式构建路由
3.1、新建工程,添加依赖
<dependencies> | |
<dependency> | |
<groupId>org.springframework.cloud</groupId> | |
<artifactId>spring-cloud-starter-gateway</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
<exclusions> | |
<exclusion> | |
<groupId>org.junit.vintage</groupId> | |
<artifactId>junit-vintage-engine</artifactId> | |
</exclusion> | |
</exclusions> | |
</dependency> | |
</dependencies> |
3.2、在配置文件中配置路由
spring.application.name=gateway-java-api | |
server.port=50010 | |
spring.cloud.gateway.routes[0].id=hello | |
spring.cloud.gateway.routes[0].uri=http://localhost:50009 | |
spring.cloud.gateway.routes[0].predicates[0]=Path=/hello |
说明:
此处配置了一个路由 ID 为 ”hello” 的路由规则,其作用是:当请求访问 http://localhost:50010/hello 时,将请求转发到 http://localhost:50009/hello
3.3、启动 ” 服务提供者 ” 和网关工程
访问 http://localhost:50010/hello
4、通过 Java API 方式配置路由
4.1、修改 ” 服务提供者 ” 的 HelloController
public class HelloController { ("${provider.name}") | |
private String name; | |
"/hello") | (|
public String hello(){return name; | |
} | |
"/hello2") | (|
public String hello2(){return name; | |
} | |
} |
4.2、修改网关工程的 application.properties
spring.application.name=gateway-java-api | |
server.port=50010 | |
#id: 自定义路由 ID | |
#spring.cloud.gateway.routes[0].id=hello | |
#uri: 目标服务地址 | |
#spring.cloud.gateway.routes[0].uri=http://localhost:50020 | |
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑 | |
#spring.cloud.gateway.routes[0].predicates[0]=Path=/hello |
4.3、添加 Java API 方式配置
public class GatewayJavaApiApplication {public static void main(String[] args) {SpringApplication.run(GatewayJavaApiApplication.class, args); | |
} | |
public RouteLocator customRouteLocator(RouteLocatorBuilder builder){// 配置路由 id 为 hell02,转发到 http://localhost:50021/hello | |
return builder.routes().route("hello2", | |
r->r.path("/hello") | |
.uri("http://localhost:50021")) | |
.build();} | |
} |
4.4、启动 ” 服务提供者 ” 和网关工程
访问 http://localhost:50010/hello2
5、Spring Cloud Gateway 的 11 种路由规则
Spring Cloud Gateway 通过 RoutePredicateFactory 创建 Predicate。Spring Cloud Gateway 预置了很多 RoutePredicateFactory,进行简单的配置即可得到想要的路由规则(Predicate)。这些路由规则会根据 HTTP 请求的不同属性来进行匹配。多个路由规则可以通过逻辑进行组合。
Spring Cloud Gateway 预置的规则如下:
谓词工厂 | 类型 | 说明 |
---|---|---|
AfterRoutePredicateFactory | datetime | 请求时间满足在配置时间之后 |
BeforeRoutePredicateFactory | datetime | 请求时间满足在配置时间之前 |
BetweenRoutePredicateFactory | datetime | 请求时间满足在配置时间之间 |
CookieRoutePredicateFactory | Cookie | 请求指定 Cookie 正则匹配指定值 |
HeaderRoutePredicateFactory | Header | 请求指定 Headers 正则匹配指定值 |
CloudFoundryRouteServiceRoutePredicateFactory | Header | 请求指定 Headers 是否包含指定名称 |
MethodRoutePredicateFactory | Method | 请求 Method 匹配配置的 Methods |
PathRoutePredicateFactory | Path | 请求路径正则匹配指定值 |
QueryRoutePredicateFacotry | Queryparam | 请求查询参数正则匹配指定值 |
请求 Method 匹配配置的 Methods | Remoteaddr | 请求远程地址匹配配置指定值 |
HostRoutePredicateFactory | Host | 请求 Host 匹配指定值 |
5.1、Path 路由谓词工厂
spring.cloud.gateway.routes[0].id=hello | |
spring.cloud.gateway.routes[0].uri=http://localhost:50020 | |
spring.cloud.gateway.routes[0].predicates[0]=Path=/hello |
5.2、After 路由谓词工厂
spring.cloud.gateway.routes[2].id=after_route | |
spring.cloud.gateway.routes[2].uri=http://localhost:50020 | |
spring.cloud.gateway.routes[2].predicates[0]=After= 时间 |
5.3、Before 路由谓词工厂
spring.cloud.gateway.routes[1].id=before_route | |
spring.cloud.gateway.routes[1].uri=http://localhost:50020 | |
spring.cloud.gateway.routes[1].predicates[0]=Before= 时间 |
5.4、Between 路由谓词工厂
spring.cloud.gateway.routes[3].id=between_route | |
spring.cloud.gateway.routes[3].uri=http://localhost:50020 | |
spring.cloud.gateway.routes[3].predicates[0]=Between= 时间, 时间 |
5.5、Cookie 路由谓词工厂
Cookie 路由谓词工厂采用两个参数:Cookie 名称和正则表达式。此谓词工厂匹配具有给定名称的 Cookie,值与正则表达式匹配。
spring.cloud.gateway.routes[4].id=cookie_route | |
spring.cloud.gateway.routes[4].uri=http://localhost:50020 | |
spring.cloud.gateway.routes[4].predicates[0]=Cookie=name,long |
5.6、Header 路由谓词工厂
Header 路由谓词工厂采用两个参数:Header 名称和正则表达式。此谓词工厂用正则表达式匹配 Header。
spring.cloud.gateway.routes[5].id=header_route | |
spring.cloud.gateway.routes[5].uri=http://localhost:50020 | |
spring.cloud.gateway.routes[5].predicates[0]=Header=X-Request-Id,\d+ |
5.7、Host 路由谓词工厂
spring.cloud.gateway.routes[6].id=host_route | |
spring.cloud.gateway.routes[6].uri=http://localhost:50020 | |
spring.cloud.gateway.routes[6].predicates[0]=Host=**.tyschool.com |
5.8、Method 路由谓词工厂
spring.cloud.gateway.routes[7].id=method_route | |
spring.cloud.gateway.routes[7].uri=http://localhost:50020 | |
spring.cloud.gateway.routes[7].predicates[0]=Method=GET |
5.9、Query 路由谓词工厂
Query 路由谓词工厂包含 1 个必需的参数和 1 个可选的正则表达式。
spring.cloud.gateway.routes[8].id=query_route | |
spring.cloud.gateway.routes[8].uri=http://localhost:50020 | |
spring.cloud.gateway.routes[8].predicates[0]=Query=name | |
5.10、RemoteAddr 路由谓词工厂
RemoteAddr 路由谓词工厂采用 CIDR 符号 (IPv4 划 IPv6) 字符串列表(最小值为 1),例如:192.168.0.1/16
spring.cloud.gateway.routes[9].id=ip_route | |
spring.cloud.gateway.routes[9].uri=http://localhost:50020 | |
spring.cloud.gateway.routes[9].predicates[0]=RemoteAddr=192.168.1.1/255 |
5.11、Weight 路由谓词工厂
Weight 路由谓词工厂是路由权重的,在配置时需要指定分组和权重。
spring.cloud.gateway.routes[10].id=weight_route1 | |
spring.cloud.gateway.routes[10].uri=http://localhost:50020 | |
spring.cloud.gateway.routes[10].predicates[0]=Path=/hello | |
spring.cloud.gateway.routes[10].predicates[1]=Weight=Weight,4 | |
spring.cloud.gateway.routes[11].id=weight_route2 | |
spring.cloud.gateway.routes[11].uri=http://localhost:50021 | |
spring.cloud.gateway.routes[11].predicates[0]=Path=/hello | |
spring.cloud.gateway.routes[11].predicates[1]=Weight=Weight,6 |
