SpringCloud Gateway网关
2.相关概念
- Route(路由):路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由;
- Predicate(断言):指的是Java 8 的 Function Predicate。SpringCloud断言的输入类型是Spring框架中的ServerWebExchange(服务网络交换器,存放着重要的请求-响应属性、请求实例和响应实例等等)。 这使开发人员可以匹配HTTP请求中的所有内容,例如请求头或请求参数。如果请求与断言相匹配,则进行路由;
- Filter(过滤器):指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前后对请求进行修改。
3.路由配置
3.1环境搭建
- 导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
- 编写配置
server:
port: 9000
service-url:
user-service: http://localhost:8001
spring:
cloud:
gateway:
routes:
- id: path_route #路由的ID
uri: ${service-url.user-service}/user/{id} #匹配后路由地址
predicates: # 断言,路径相匹配的进行路由
- Path=/user/{id}
- 启动eureka-server,user-service和api-gateway服务,并调用该地址测试:http://localhost:9000/user/1
- 我们发现该请求被路由到了user-service的该路径上:http://localhost:8081/user/1
3.2路由规则
Spring Cloud Gateway包括许多内置的Route Predicate工厂。 所有这些Predicate都与HTTP请求的不同属性匹配。 多个Route Predicate工厂可以进行组合,下面我们来介绍下一些常用的Route Predicate。
After Route Predicate
在指定时间之后的请求会匹配该路由。
spring:
cloud:
gateway:
routes:
- id: after_route
uri: ${service-url.user-service}
predicates:
- After=2020-09-20T10:30:00+08:00[Asia/Shanghai]
Before Route Predicate
在指定时间之前的请求会匹配该路由。
spring:
cloud:
gateway:
routes:
- id: before_route
uri: ${service-url.user-service}
predicates:
- Before=2020-09-20T16:30:00+08:00[Asia/Shanghai]
Method Route Predicate
发送指定方法的请求会匹配该路由。
spring:
cloud:
gateway:
routes:
- id: method_route
uri: ${service-url.user-service}
predicates:
- Method=GET
Path Route Predicate
发送指定路径的请求会匹配该路由。
spring:
cloud:
gateway:
routes:
- id: path_route
uri: ${service-url.user-service}/user/{id}
predicates:
- Path=/user/{id}
发送/user/1
路径请求可以匹配该路由
http://localhost:9201/user/1
发送/abc/1
路径请求无法匹配该路由
http://localhost:9201/abc/1
3.3动态路由
使用Zuul作为网关结合注册中心进行使用时,默认情况下Zuul会根据注册中心注册的服务列表,以服务名为路径创建动态路由,Gateway同样也实现了该功能。下面我们演示下Gateway结合注册中心如何使用默认的动态路由和过滤器
- pom.xml中添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 配置yml
方式一
server:
port: 9000
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
#配置路由:路由ID,路由到微服务的uri,断言(判断条件)
- id: path_route
uri: lb://user-service #服务的ID,uri的协议为lb,这样才能启用Gateway的负载均衡功能
predicates:
- Path=/user/**
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
方式二
server:
port: 9000
spring:
application:
name: api-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #开启从注册中心动态创建路由的功能
lower-case-service-id: true #使用小写服务名,默认是大写
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
logging:
level:
org.springframework.cloud.gateway: debug
4.Route Filter过滤器
路由过滤器可用于修改进入的HTTP请求和返回的HTTP响应,路由过滤器只能指定路由进行使用。Spring Cloud Gateway 内置了多种路由过滤器,他们都由GatewayFilter的工厂类来产生,下面我们介绍下常用路由过滤器的用法。
- 过滤器的生命周期: SpringCloud Gateway过滤器有两个 pre 和 post
- PRE:在请求被路由之前调用,可以利用这种过滤器实现身份验证,在集群中选择请求的微服务,纪律调试信息等。
- POST:在请求被路由到微服务以后执行,这种过滤器可以为响应添加标准的HTTP Header、收集统计信息和指标、将响应从微服务发送给客户端。
- 过滤器的类型:从作用的范围分为两种GatewayFilter 和 GlobalFilter
- GatewayFilter:应用在单个路由和分组路由上
- GlobalFilter:应用到所有的路由上
4.1局部过滤器
局部过滤器(GatewayFilter)是针对单个路由的过滤器,可以对访问的URL进行过滤,进行切面处理。在SpringCloud Gateway中内置了多种不同类型的局部过滤器
4.1.1AddRequestParameter GatewayFilter
给请求添加参数的过滤器。
spring:
cloud:
gateway:
routes:
- id: add_request_parameter_route
uri: http://localhost:8081
filters:
- AddRequestParameter=username, hello
predicates:
- Method=GET
以上配置会对GET请求添加username=hello
的请求参数,通过工具进行如下测试
http://localhost:9000/user/getByUsername
相当于发起该请求:
http://localhost:8081/user/getByUsername?username=hello
4.1.2 StripPrefix GatewayFilter
对指定数量的路径前缀进行去除的过滤器
spring:
cloud:
gateway:
routes:
- id: strip_prefix_route
uri: http://localhost:8081
predicates:
- Path=/user-service/**
filters:
- StripPrefix=2
以上配置会把以/user-service/
开头的请求的路径去除两位,通过工具进行如下测试
http://localhost:9000/user-service/a/user/1
相当于发起该请求:
http://localhost:8081/user/1
4.1.3 PrefixPath GatewayFilter
与StripPrefix过滤器恰好相反,会对原有路径进行增加操作的过滤器。
spring:
cloud:
gateway:
routes:
- id: prefix_path_route
uri: http://localhost:8081
predicates:
- Method=GET
filters:
- PrefixPath=/user
以上配置会对所有GET请求添加/user
路径前缀,通过工具进行如下测试
http://localhost:9000/1
相当于发起该请求:
http://localhost:8081/user/1
4.1.4 Hystrix GatewayFilter
Hystrix 过滤器允许你将断路器功能添加到网关路由中,使你的服务免受级联故障的影响,并提供服务降级处理。
- 要开启断路器功能,我们需要在pom.xml中添加Hystrix的相关依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- 然后添加相关服务降级的处理类:
@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;
}
}
- 在application.yml中添加相关配置,当路由出错时会转发到服务降级处理的控制器上:
spring:
cloud:
gateway:
routes:
- id: hystrix_route
uri: http://localhost:8081
predicates:
- Method=GET
filters:
- name: Hystrix
args:
name: fallbackcmd
fallbackUri: forward:/fallback
- 关闭user-service,调用该地址进行测试:http://localhost:9000/user/1 ,发现已经返回了服务降级的处理信息
4.1.6 Retry GatewayFilter
对路由请求进行重试的过滤器,可以根据路由请求返回的HTTP状态码来确定是否进行重试。
- 修改配置文件:
spring:
cloud:
gateway:
routes:
- id: retry_route
uri: http://localhost:8081
predicates:
- Method=GET
filters:
- name: Retry
args:
retries: 1 #需要进行重试的次数
statuses: BAD_GATEWAY #返回哪个状态码需要进行重试,返回状态码为5XX进行重试
backoff:
firstBackoff: 10ms
maxBackoff: 50ms
factor: 2
basedOnPreviousValue: false
- 当调用返回500时会进行重试,访问测试地址:http://localhost:9000/user/111
- 可以发现user-service控制台报错2次,说明进行了一次重试。
5.统一鉴权
全局过滤器
全局过滤器(GlobalFilter)作用于所有路由,SpringCloud Gateway提供了GlobalFilter接口,可以自定义过滤器;通过全局过滤器可以实现对权限的统一校验、安全验证等功能。
以下代码演示全局过滤器的实现
@Component
public class LoginFilter implements GlobalFilter, Ordered {
/**
* 执行过滤器的业务逻辑
* @param exchange
* @param chain
* @return
*/
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
System.out.println("执行了全局过滤器");
String token = exchange.getRequest().getQueryParams().getFirst("token");
if(token == null){
System.out.println("请登录");
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
return exchange.getResponse().setComplete();//请求结束
}
return chain.filter(exchange);//继续执行后续的过滤器
}
/**
* 定义过滤器的执行顺序
* 值越小优先级越高
* @return
*/
@Override
public int getOrder() {
return 0;
}
}