-
Route(路由):路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由;
-
Predicate(断言):指的是Java 8 的 Function Predicate。 输入类型是Spring框架中的ServerWebExchange。 这使开发人员可以匹配HTTP请求中的所有内容,例如请求头或请求参数。如果请求与断言相匹配,则进行路由;
-
Filter(过滤器):指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前后对请求进行修改。
这里我们创建一个api-gateway模块来演示Gateway的常用功能。
在pom.xml中添加相关依赖
org.springframework.cloud
spring-cloud-starter-gateway
两种不同的配置路由方式
Gateway 提供了两种不同的方式用于配置路由,一种是通过yml文件来配置,另一种是通过Java Bean来配置,下面我们分别介绍下。
使用yml配置
在application.yml中进行配置:
server:
port: 9201
service-url:
user-service: http://localhost:8201
spring:
cloud:
gateway:
routes:
路由的ID
- id: path_route
匹配后路由地址
uri: ${service-url.user-service}/user/{id}
predicates:
断言,路径相匹配的进行路由
- Path=/user/{id}
启动eureka-server、user-service和api-gateway服务,并调用该地址测试:http://localhost:9201/user/1
我们发现该请求被路由到了user-service的该路径上:http://localhost:8201/user/1
使用Java Bean配置
添加相关配置类,并配置一个RouteLocator对象:
@Configuration
public class GatewayConfig {
@Bean
public RouteLocator customerRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route(“path_route2”, r -> r.path(“/user/getByUsername”)
.uri(“http://localhost:8201/user/getByUsername”))
.build();
}
}
重新启动api-gateway服务,并调用该地址测试:http://localhost:9201/user/getByUsername?username=jourwon
我们发现该请求被路由到了user-service的该路径上:http://localhost:8201/user/getByUsername?username=jourwon
Spring Cloud Gateway将路由匹配作为Spring WebFlux HandlerMapping基础架构的一部分。 Spring Cloud Gateway包括许多内置的Route Predicate工厂。 所有这些Predicate都与HTTP请求的不同属性匹配。 多个Route Predicate工厂可以进行组合,下面我们来介绍下一些常用的Route Predicate。
注意:Predicate中提到的配置都在application-predicate.yml文件中进行修改,并用该配置启动api-gateway服务。
After Route Predicate
在指定时间之后的请求会匹配该路由。
spring:
cloud:
gateway:
routes:
- id: after_route
uri: ${service-url.user-service}
predicates:
- After=2019-12-29T18:30:00+08:00[Asia/Shanghai]
Before Route Predicate
在指定时间之前的请求会匹配该路由。
spring:
cloud:
gateway:
routes:
- id: before_route
uri: ${service-url.user-service}
predicates:
- Before=2019-12-29T18:30:00+08:00[Asia/Shanghai]
Between Route Predicate
在指定时间区间内的请求会匹配该路由。
spring:
cloud:
gateway:
routes:
- id: before_route
uri: ${service-url.user-service}
predicates:
- Between=2019-12-29T18:30:00+08:00[Asia/Shanghai], 2019-12-30T18:30:00+08:00[Asia/Shanghai]
Cookie Route Predicate
带有指定Cookie的请求会匹配该路由。
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: ${service-url.user-service}
predicates:
- Cookie=username,jourwon
使用curl工具发送带有cookie为username=jourwon
的请求可以匹配该路由。
curl http://localhost:9201/user/1 --cookie “username=jourwon”
Header Route Predicate
带有指定请求头的请求会匹配该路由。
spring:
cloud:
gateway:
routes:
- id: header_route
uri: ${service-url.user-service}
predicates:
- Header=X-Request-Id, \d+
使用curl工具发送带有请求头为X-Request-Id:123
的请求可以匹配该路由。
curl http://localhost:9201/user/1 -H “X-Request-Id:123”
Host Route Predicate
带有指定Host的请求会匹配该路由。
spring:
cloud:
gateway:
routes:
- id: host_route
uri: ${service-url.user-service}
predicates:
- Host=**.jourwon.com
使用curl工具发送带有请求头为Host:www.jourwon.com
的请求可以匹配该路由。
curl http://localhost:9201/user/1 -H “Host:www.jourwon.com”
Method Route Predicate
发送指定方法的请求会匹配该路由。
spring:
cloud:
gateway:
routes:
- id: method_route
uri: ${service-url.user-service}
predicates:
- Method=GET
使用curl工具发送GET请求可以匹配该路由。
curl http://localhost:9201/user/1
使用curl工具发送POST请求无法匹配该路由。
curl -X POST http://localhost:9201/user/1
Path Route Predicate
发送指定路径的请求会匹配该路由。
spring:
cloud:
gateway:
routes:
- id: path_route
uri: ${service-url.user-service}/user/{id}
predicates:
- Path=/user/{id}
使用curl工具发送/user/1
路径请求可以匹配该路由。
curl http://localhost:9201/user/1
使用curl工具发送/abc/1
路径请求无法匹配该路由。
curl http://localhost:9201/abc/1
Query Route Predicate
带指定查询参数的请求可以匹配该路由。
spring:
cloud:
gateway:
routes:
- id: query_route
uri: ${service-url.user-service}/user/getByUsername
predicates:
- Query=username
使用curl工具发送带username=jourwon
查询参数的请求可以匹配该路由。
curl http://localhost:9201/user/getByUsername?username=jourwon
使用curl工具发送带不带查询参数的请求无法匹配该路由。
curl http://localhost:9201/user/getByUsername
RemoteAddr Route Predicate
从指定远程地址发起的请求可以匹配该路由。
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: ${service-url.user-service}
predicates:
- RemoteAddr=192.168.1.1/24
使用curl工具从192.168.1.1发起请求可以匹配该路由。
curl http://localhost:9201/user/1
Weight Route Predicate
使用权重来路由相应请求,以下表示有80%的请求会被路由到localhost:8201,20%会被路由到localhost:8202。
spring:
cloud:
gateway:
routes:
- id: weight_high
uri: http://localhost:8201
predicates:
-
Weight=group1, 8
-
id: weight_low
uri: http://localhost:8202
predicates:
- Weight=group1, 2
路由过滤器可用于修改进入的HTTP请求和返回的HTTP响应,路由过滤器只能指定路由进行使用。Spring Cloud Gateway 内置了多种路由过滤器,他们都由GatewayFilter的工厂类来产生,下面我们介绍下常用路由过滤器的用法。
AddRequestParameter GatewayFilter
给请求添加参数的过滤器。
spring:
cloud:
gateway:
routes:
- id: add_request_parameter_route
uri: http://localhost:8201
filters:
- AddRequestParameter=username, jourwon
predicates:
- Method=GET
以上配置会对GET请求添加username=jourwon
的请求参数,通过curl工具使用以下命令进行测试。
curl http://localhost:9201/user/getByUsername
相当于发起该请求:
curl http://localhost:8201/user/getByUsername?username=jourwon
StripPrefix GatewayFilter
对指定数量的路径前缀进行去除的过滤器。
spring:
cloud:
gateway:
routes:
- id: strip_prefix_route
uri: http://localhost:8201
predicates:
- Path=/user-service/**
filters:
- StripPrefix=2
以上配置会把以/user-service/
开头的请求的路径去除两位,通过curl工具使用以下命令进行测试。
curl http://localhost:9201/user-service/a/user/1
相当于发起该请求:
curl http://localhost:8201/user/1
PrefixPath GatewayFilter
与StripPrefix过滤器恰好相反,会对原有路径进行增加操作的过滤器。
spring:
cloud:
gateway:
routes:
- id: prefix_path_route
uri: http://localhost:8201
predicates:
- Method=GET
filters:
- PrefixPath=/user
以上配置会对所有GET请求添加/user
路径前缀,通过curl工具使用以下命令进行测试。
curl http://localhost:9201/1
相当于发起该请求:
curl http://localhost:8201/user/1
Hystrix GatewayFilter
Hystrix 过滤器允许你将断路器功能添加到网关路由中,使你的服务免受级联故障的影响,并提供服务降级处理。
要开启断路器功能,我们需要在pom.xml中添加Hystrix的相关依赖:
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
然后添加相关服务降级的处理类:
@RestController
public class FallbackController {
@GetMapping(“/fallback”)
public Object fallback() {
Map<String,Object> result = new HashMap<>();
result.put(“data”,null);
result.put(“message”,“Get request fallback!”);
result.put(“code”,500);
return result;
}
}
最后
这份《“java高分面试指南”-25分类227页1000+题50w+字解析》同样可分享给有需要的朋友,感兴趣的伙伴们可挑战一下自我,在不看答案解析的情况,测试测试自己的解题水平,这样也能达到事半功倍的效果!(好东西要大家一起看才香)
9201/1
相当于发起该请求:
curl http://localhost:8201/user/1
Hystrix GatewayFilter
Hystrix 过滤器允许你将断路器功能添加到网关路由中,使你的服务免受级联故障的影响,并提供服务降级处理。
要开启断路器功能,我们需要在pom.xml中添加Hystrix的相关依赖:
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
然后添加相关服务降级的处理类:
@RestController
public class FallbackController {
@GetMapping(“/fallback”)
public Object fallback() {
Map<String,Object> result = new HashMap<>();
result.put(“data”,null);
result.put(“message”,“Get request fallback!”);
result.put(“code”,500);
return result;
}
}
最后
这份《“java高分面试指南”-25分类227页1000+题50w+字解析》同样可分享给有需要的朋友,感兴趣的伙伴们可挑战一下自我,在不看答案解析的情况,测试测试自己的解题水平,这样也能达到事半功倍的效果!(好东西要大家一起看才香)
[外链图片转存中…(img-wgixdeAp-1714454956749)]
[外链图片转存中…(img-YCWV1zMA-1714454956750)]